题意简述
给定一个长度为 的序列 ,值域 。有 次操作,每次是下面的一种:
- 给定 。,使 。 值域 。
- 给定 。,使 。 值域 。
- 求 。
其中 表示二进制按位异或,, 和 使用二进制给出。
思路
看到操作 ,一眼线性基。
由于线性基每次是 的,所以每次操作不可能修改 个位置。考虑 的差分数组 ,显然 的线性基也是 的线性基。
这样,前两个操作就变成了 个单点修改和 次区间赋 。根据均摊分析,从非 变为 次数是 的,故修改的次数是 的。
现在我们只需要一个可删除的线性基。简单离线维护出每个非 数 出现的时间区间 。对于在 时刻的查询,只需要保留 的 , 可以按时间排序后双指针或使用 vector 等维护, 可以使用前缀线性基做到保留 的部分。
这样做的时间复杂度是 ,空间复杂度 ,可以通过。
线段树分治或许也能过。
代码
#include<bits/stdc++.h>
using namespace std;
using bs = bitset<2000>;
int n,m,q;
struct Linear_Basis
{
bs p[2000];
int t[2000];
Linear_Basis()
{
memset(t,-1,sizeof(t));
}
void insert(bs x,int ti)
{
for(int i=m-1;i>=0;i--)
{
if(!x[i])
continue;
if(t[i]==-1)
{
p[i] = x;
t[i] = ti;
return;
}
else if(t[i]<ti)
{
swap(x,p[i]);
swap(ti,t[i]);
}
x ^= p[i];
}
}
bs query(int mt)
{
bs ans;
for(int i=m-1;i>=0;i--)
if(!ans[i]&&t[i]>mt)
ans ^= p[i];
return ans;
}
}b;
struct mdf
{
bs v;
int t;
mdf(bs _v=0,int _t=0):v(_v),t(_t){}
};
vector<mdf> md[2007];
bs a[2007];
bs d[2007];
int lst[2007];
void update(int idx,bs val,int tim)
{
if(val==d[idx])
return;
if(d[idx].any())
md[lst[idx]].emplace_back(mdf(d[idx],tim));
d[idx] = val;
lst[idx] = tim;
}
bitset<2007> qr;
int op,l,r;
bs w;
signed main()
{
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
cin>>n>>m>>q;
for(int i=1;i<=n;i++)
{
cin>>a[i];
d[i] = a[i] ^ a[i-1];
}
for(int i=1;i<=q;i++)
{
cin>>op;
if(op==3)
qr[i] = true;
else
{
cin>>l>>r>>w;
if(op==1)
for(int j=l;j<=r;j++)
a[j] ^= w;
else if(op==2)
for(int j=l;j<=r;j++)
a[j] = w;
for(int j=l;j<=min(r+1,n);j++)
update(j,a[j]^a[j-1],i);
}
}
for(int i=1;i<=n;i++)
if(d[i].any())
md[lst[i]].emplace_back(mdf(d[i],q+1));
for(auto& x:md[0])
b.insert(x.v,x.t);
for(int i=1;i<=q;i++)
{
for(auto& x:md[i])
b.insert(x.v,x.t);
if(qr[i])
cout<<b.query(i).to_string().substr(2000-m)<<'\n';
}
}