CodeForces 587 E.Duff as a Queen(线性基+线段树+树状数组)
2018-01-11 21:59
369 查看
Description
给出一个长度为n的序列ai,有两种操作
1 l r k: 表示将al,al+1,...,ar每一个数都异或上k
2 l r: 查询al,al+1,...,ar这些数选取子集异或可以得到的不同数字个数
Input
第一行两整数n,q表示序列长度和操作数,之后输出n个整数ai表示该序列,最后q行每行一个操作
(1≤n≤2⋅105,1≤q≤4⋅104,0≤ai,k≤109)
Output
对于每个查询操作,输出结果
Sample Input
5 5
1 2 3 4 2
2 1 5
1 2 2 8
2 1 5
1 1 3 10
2 2 2
Sample Output
8
16
1
Solution
对于查询,只要知道该区间数字的线性基,设其维数为m,则答案即为2m,问题在于区间更新之后线性基完全改变,故考虑b序列,其中bi=ai^ai+1,一个显然的结论是al,al+1,...,ar所能异或得到的数字集合和al,bl,bl+1,...,br−1所能异或得到的数字集合相等,而对a序列的[l,r]区间更新对b序列的影响只有bl−1,br,故只要用线段树维护b序列的区间线性基,支持单点修改区间查询即可,然后用树状数组维护对a序列的修改(类似区间加的前缀和优化操作),每次查询只需要在线段树中查询b序列属于区间[l,r−1]的线性基,然后在树状数组中求一个前缀和得到对al的累计修改值异或原先的值得到最新的al值将其加到线性基中即可
Code
给出一个长度为n的序列ai,有两种操作
1 l r k: 表示将al,al+1,...,ar每一个数都异或上k
2 l r: 查询al,al+1,...,ar这些数选取子集异或可以得到的不同数字个数
Input
第一行两整数n,q表示序列长度和操作数,之后输出n个整数ai表示该序列,最后q行每行一个操作
(1≤n≤2⋅105,1≤q≤4⋅104,0≤ai,k≤109)
Output
对于每个查询操作,输出结果
Sample Input
5 5
1 2 3 4 2
2 1 5
1 2 2 8
2 1 5
1 1 3 10
2 2 2
Sample Output
8
16
1
Solution
对于查询,只要知道该区间数字的线性基,设其维数为m,则答案即为2m,问题在于区间更新之后线性基完全改变,故考虑b序列,其中bi=ai^ai+1,一个显然的结论是al,al+1,...,ar所能异或得到的数字集合和al,bl,bl+1,...,br−1所能异或得到的数字集合相等,而对a序列的[l,r]区间更新对b序列的影响只有bl−1,br,故只要用线段树维护b序列的区间线性基,支持单点修改区间查询即可,然后用树状数组维护对a序列的修改(类似区间加的前缀和优化操作),每次查询只需要在线段树中查询b序列属于区间[l,r−1]的线性基,然后在树状数组中求一个前缀和得到对al的累计修改值异或原先的值得到最新的al值将其加到线性基中即可
Code
#include<cstdio> #include<cstring> using namespace std; namespace fastIO { #define BUF_SIZE 100000 //fread -> read bool IOerror=0; inline char nc() { static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE; if(p1==pend) { p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin); if(pend==p1) { IOerror=1; return -1; } } return *p1++; } inline bool blank(char ch) { return ch==' '||ch=='\n'||ch=='\r'||ch=='\t'; } inline void read(int &x) { char ch; while(blank(ch=nc())); if(IOerror)return; for(x=ch-'0';(ch=nc())>='0'&&ch<='9';x=x*10+ch-'0'); } #undef BUF_SIZE }; using namespace fastIO; const int maxn=200005; #define ls (t<<1) #define rs ((t<<1)|1) int base[maxn<<2][30],num[maxn<<2],n,q,a[maxn],b[maxn],A[66],B[31]; void Unite(int x,int y,int z) { int res=0; for(int i=1;i<=num[x];i++)A[res++]=base[x][i]; for(int i=1;i<=num[y];i++)A[res++]=base[y][i]; memset(B,0,sizeof(B)); for(int i=0;i<res;i++) for(int j=29;j>=0;j--) if(A[i]>>j&1) { if(!B[j]) { B[j]=A[i]; break; } else A[i]^=B[j]; } num[z]=0; for(int i=0;i<=29;i++) if(B[i])base[z][++num[z]]=B[i]; } void build(int l,int r,int t) { if(l==r) { if(b[l])base[t][++num[t]]=b[l]; return ; } int mid=(l+r)/2; build(l,mid,ls);build(mid+1,r,rs); Unite(ls,rs,t); } void update(int x,int l,int r,int t,int val) { if(l==r) { base[t][1]^=val; num[t]=(base[t][1]>0); return ; } int mid=(l+r)/2; if(x<=mid)update(x,l,mid,ls,val); else update(x,mid+1,r,rs,val); Unite(ls,rs,t); } void query(int L,int R,int l,int r,int t) { if(L>R)return ; if(L<=l&&r<=R) { Unite(0,t,0); return ; } int mid=(l+r)/2; if(L<=mid)query(L,R,l,mid,ls); if(R>mid)query(L,R,mid+1,r,rs); } #define lowbit(x) (x&(-x)) int bit[maxn]; void add(int x,int val) { while(x<=n) { bit[x]^=val; x+=lowbit(x); } } int sum(int x) { int ans=0; while(x) { ans^=bit[x]; x-=lowbit(x); } return ans; } int main() { read(n);read(q); //scanf("%d%d",&n,&q); for(int i=1;i<=n;i++)read(a[i]);//scanf("%d",&a[i]); for(int i=1;i<n;i++)b[i]=a[i]^a[i+1]; if(n>1)build(1,n-1,1); while(q--) { int op,l,r,k; read(op);read(l);read(r); //scanf("%d%d%d",&op,&l,&r); if(op==1) { read(k);//scanf("%d",&k); if(l>1)update(l-1,1,n-1,1,k); if(r<n)update(r,1,n-1,1,k); add(l,k);add(r+1,k); } else { num[0]=0; int temp=sum(l)^a[l]; if(temp)base[0][++num[0]]=temp; if(n>1)query(l,r-1,1,n-1,1); printf("%d\n",1<<num[0]); } } }
相关文章推荐
- Codeforces 587 E. Duff as a Queen
- codeforces 587 B. Duff in Beach
- Codeforces Round #326 (Div. 1) E. Duff as a Queen
- 【Codeforces587E】Duff as a Queen
- Code Forces 587 A. Duff and Weight Lifting
- Duff in the Army codeforces 588E 树上主席树+lca
- codeforces 701 D. As Fast As Possible
- CodeForces 588 C. Duff and Weight Lifting
- Codeforces 588 A. Duff and Meat
- Code Forces 587 A. Duff and Weight Lifting(贪心)
- codeforces-587A-Duff and Weight Lifting
- Codeforces 588 C. Duff and Weight Lifting
- CodeForces 588C - Duff and Weight Lifting(思维)
- Codeforces 558 A Duff and Meat
- 【45.61%】【codeforces 701D】As Fast As Possible
- codeforces - 326B - Duff in Love
- CodeForces 700A As Fast As Possible
- Codeforces 588 C Duff and Weight Lifting【思维】
- Codeforces, 587F: Duff is Mad
- Codeforces 700A As Fast As Possible(机智)