区间颜色数用扫描线转成 区间 ,查询历史异或和。需要实现数据结构,支持:
-
区间 。
-
全局 。
-
查询 区间异或和。
看着就不像 polylog 于是分块。一操作散块重构整块打标记 累计到 重构,需预处理 表示第 块中 。二操作容易维护整块答案 ,并记每个 的更新次数奇偶性 以重构 ,三操作需重构散块以查询。
问题变为如何快速重构,即快速用 更新 并计算 。先讨论 ,将 看成 其中 为 的进位,拆位变成 ,双指针即可 。
如何优化?因为 考虑将 分为 。对于 直接求出 ,。
对于 ,有 。那么 必须从 开始连续进位,于是 是一段前缀且贡献到的 是一段后缀(要触发进位),容易做到 。
对于 将上述做法转置即可,于是可以做到 。
点击查看参考代码
#include<bits/stdc++.h>
#define TIME chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count()
#define rep(i,l,r) for(int qwp=(r),i=(l);i<=qwp;i++)
#define per(i,r,l) for(int qwp=(l),i=(r);i>=qwp;i--)
#define pb push_back
using namespace std;namespace c0dE1ng{
constexpr int N=4e5+5,B=9,L=1<<B,C=N/L+5;
int lb(int x){return x&(-x);}
struct DS1{
int n,blk,bl[C],br[C],col[N];
int a[N],b[N],sb[N],z[C],f[C][L];bool c[C][L];
int cnt[B][L],res[B][L],suf[L];
void updb(int x){
per(w,B-1,0){
rep(i,0,(1<<w)-1)cnt[w][i]=res[w][i]=0;
if(w==B-1)rep(j,0,L-1)cnt[w][j&((1<<w)-1)]+=c[x][j];
else rep(i,0,(1<<w)-1)cnt[w][i]=cnt[w+1][i]+cnt[w+1][i|1<<w];
}
rep(w,0,B-1){
per(i,(1<<w)-2,0)cnt[w][i]+=cnt[w][i+1];
rep(i,0,(1<<w)-1)res[w][i]=cnt[w][(1<<w)-i]&1?1<<w:0;
}
rep(w,0,B-1){
if(w==B-1)rep(i,bl[x],br[x])b[i]^=res[w][a[i]&((1<<w)-1)];
else rep(i,0,(1<<w)-1)res[w+1][i]^=res[w][i],res[w+1][i|1<<w]^=res[w][i];
}
rep(j,0,L-1)suf[j]=c[x][j];per(j,L-2,0)suf[j]+=suf[j+1];
rep(i,bl[x],br[x]){int v=(1<<B)-(a[i]&((1<<B)-1));if(v<L&&(suf[v]&1))b[i]^=lb(~(a[i]>>B))*2-1<<B;}
int s=0;bool w=0;rep(j,0,L-1)if(c[x][j])s^=j,w^=1,c[x][j]=0;
rep(i,bl[x],br[x]){b[i]^=s;if(w)b[i]^=a[i];}
}
void updf(int x){
rep(i,bl[x],br[x])a[i]+=z[x];z[x]=0;
rep(j,0,L-1)f[x][j]=br[x]-bl[x]+1&1?j:0;
per(w,B-1,0){
rep(i,0,(1<<w)-1)cnt[w][i]=res[w][i]=0;
if(w==B-1)rep(i,bl[x],br[x])cnt[w][a[i]&((1<<w)-1)]++;
else rep(i,0,(1<<w)-1)cnt[w][i]=cnt[w+1][i]+cnt[w+1][i|1<<w];
}
rep(w,0,B-1){
per(i,(1<<w)-2,0)cnt[w][i]+=cnt[w][i+1];
rep(i,0,(1<<w)-1)res[w][i]^=cnt[w][(1<<w)-i]&1?1<<w:0;
if(w==B-1)rep(j,0,L-1)f[x][j]^=res[w][j&((1<<w)-1)];
else rep(i,0,(1<<w)-1)res[w+1][i]^=res[w][i],res[w+1][i|1<<w]^=res[w][i];
}
rep(j,0,L-1)suf[j]=0;
rep(i,bl[x],br[x])suf[((a[i]&(L-1))^(L-1))]^=lb(~(a[i]>>B))*2-1<<B;
for(int j=0,s=0;j<L;j++)f[x][j]^=s,s^=suf[j];
int s=0;rep(i,bl[x],br[x])s^=a[i];rep(j,0,L-1)f[x][j]^=s;
}
void Init(int n_){
n=n_,blk=0;while(br[blk]!=n){
blk++,bl[blk]=br[blk-1]+1,br[blk]=min(bl[blk]+L-1,n);
rep(i,bl[blk],br[blk])col[i]=blk;updb(blk),updf(blk);
}
}
void add(int l,int r){
if(col[l]==col[r]){updb(col[l]);rep(i,l,r)a[i]++;updf(col[l]);return ;}
updb(col[l]);rep(i,l,br[col[l]])a[i]++;updf(col[l]);
updb(col[r]);rep(i,bl[col[r]],r)a[i]++;updf(col[r]);
rep(i,col[l]+1,col[r]-1)if(++z[i]>=L)updb(i),updf(i);
}
void upd(){
rep(i,1,blk)sb[i]^=f[i][z[i]],c[i][z[i]]^=1;
}
int Q(int l,int r){
if(col[l]==col[r]){updb(col[l]),updf(col[l]);int s=0;rep(i,l,r)s^=b[i];return s;}
updb(col[l]),updf(col[l]),updb(col[r]),updf(col[r]);
int s=0;rep(i,l,br[col[l]])s^=b[i];rep(i,bl[col[r]],r)s^=b[i];
rep(i,col[l]+1,col[r]-1)s^=sb[i];return s;
}
}qwq;
int n,q,pre[N],lst[N],ql[N],qans[N];vector<int>qs[N];
void main(){
cin.tie(0)->sync_with_stdio(0);
cin>>n>>q;rep(i,1,n){int a;cin>>a;pre[i]=exchange(lst[a],i);}
rep(i,1,q){int r;cin>>ql[i]>>r;qs[r].pb(i);}
qwq.Init(n);rep(i,1,n){
qwq.add(pre[i]+1,i),qwq.upd();
for(auto j:qs[i])qans[j]=qwq.Q(ql[j],i);
}rep(i,1,q)cout<<qans[i]<<'\n';
}
}
int main(){
auto _Tbe=TIME;c0dE1ng::main();auto _Ted=TIME;
return cerr<<"\nTIME:"<<_Ted-_Tbe<<"ms\n",0;
}
/*
ulimit -s 1048576
g++ -O2 -std=c++14 -static A.cpp -o %;size %;./% < A.in > A.out
*/如果懒得写求 可以先用剪枝暴力交一发,我的实现在 loj 和洛谷上只有 last sub 慢于正解且能过,qoj 上我交了个 hack 但是能波动过去。