题面描述
思考
本来想用二分的,
读完数据之后,又发现不用了。
然而需要开多一个数组
p
p
p记录颜色最近出现的位置,以便进行玄学操作
当达成目标( n o w = = e n d now==end now==end)时,
if(now==end)
{
while(l<i&&p[a[l].col]!=a[l].p)++l;//不影响,而且更短
ans=min(ans,(ll)a[i].p-a[l].p);
now^=b[a[l++].col];//保证now!=end
}
while(l<i&&p[a[l].col]!=a[l].p)++l;
因为 a [ l ] . c o l a[l].col a[l].col最近出现的位置不是 a [ l ] . p a[l].p a[l].p,那么证明在 a [ l ] . p a[l].p a[l].p之后还有 a [ l ] . c o l a[l].col a[l].col出现,不影响 n o w now now的结果,因此可以删去。
貌似除了这个没有什么奇奇怪怪的东西了。
AC code
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<cstring>
#define ll long long
#define gc getchar()
using namespace std;
const int N=1e6+10;
inline void qr(int &x)
{
x=0;int f=1;char c=gc;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=gc;}
while(c>='0'&&c<='9'){x=x*10+(c^48);c=gc;}
x*=f;
}
void qw(int x)
{
if(x<0)x=-x,putchar('-');
if(x/10)qw(x/10);
putchar(x%10+48);
}
ll b[61];
struct node
{
int p;int col;
}a[N];
int p[61];
int q[N];
bool cmp(node a,node b){return a.p<b.p;}
int main()
{
ll end=1;int n,m;qr(n),qr(m);
b[1]=1;for(int i=2;i<=m;i++)b[i]=b[i-1]<<1,end|=b[i];
ll now=0;
int tot=0;
for(int i=1;i<=m;i++)
{
int len;qr(len);
while(len--)qr(a[++tot].p),a[tot].col=i;
}
sort(a+1,a+n+1,cmp);
int l=1;ll ans=1e10;
for(int i=1;i<=n;i++)
{
p[a[i].col]=a[i].p;
now|=b[a[i].col];
if(now==end)
{
while(l<i&&p[a[l].col]!=a[l].p)++l;
ans=min(ans,(ll)a[i].p-a[l].p);
now^=b[a[l++].col];
}
}
qw(ans);puts("");
return 0;
}