3261: Maximum XOR and
Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2637 Solved: 1078
[Submit][Status][Discuss]
Description
Given a sequence of non-negative integers {a}, initial length N.
There are M operations, and there are two types of operations:
1. Ax: Add operation, which means adding a number x at the end of the sequence, and the length of the sequence is N+1.
2. Qlrx: Inquiry operation, you need to find a position p, satisfying l<=p<=r, so that:
a[p] xor a[p+1] xor ... xor a[N] xor x is the largest, and the output is the largest.
Input
The first line contains two integers N, M with the meanings shown in the problem description. The second line contains N non-negative integers representing the initial sequence A . Next M lines, each line describes an operation, the format is as described in the title.
Output
Assuming that there are T query operations, the output should have T lines, each with an integer representing the answer to the query.
Sample Input
5 5
2 6 4 3 6
A 1
Q 3 5 4
A 4
Q 5 7 0
Q 3 6 6
For test points 1-2, N,M<=5.
For test points 3-7, N,M<=80000.
For test points 8-10, N,M<=300000.
Among them, test points 1, 3, 5, 7, 9 guarantee no modification operation.
0<=a[i]<=10^7.
2 6 4 3 6
A 1
Q 3 5 4
A 4
Q 5 7 0
Q 3 6 6
For test points 1-2, N,M<=5.
For test points 3-7, N,M<=80000.
For test points 8-10, N,M<=300000.
Among them, test points 1, 3, 5, 7, 9 guarantee no modification operation.
0<=a[i]<=10^7.
Sample Output
4
5
6
5
6
Just change the suffix query to the total XOR prefix, and then write a persistent trie.
#include<bits/stdc++.h> #define ll long long using namespace std; const int maxn=300005; struct node{ int Sum node *ch[2]; }nil[maxn*73],*rot[maxn*4],*cnt; int now,Xor,N,L,R,X,M,ci[33],ans; char C; node *update(node *u,int x){ node * ret = ++ cnt; * ret = * u, ret-> Sum ++; if(x<0) return ret; if(Xor&ci[x]) ret->ch[1]=update(ret->ch[1],x-1); else ret->ch[0]=update(ret->ch[0],x-1); return ret; } void query(node *l,node *r,int x){ if(x<0) return; int u=(ci[x]&X)?0:1; if(r->ch[u]->Sum-l->ch[u]->Sum) ans+=ci[x],query(l->ch[u],r->ch[u],x-1); else query(l->ch[u^1],r->ch[u^1],x-1); } inline void solve(){ while(M--){ C=getchar(); while(C!='A'&&C!='Q') C=getchar(); if(C=='A') N++,scanf("%d",&now),Xor^=now,rot[N]=update(rot[N-1],25); else{ scanf("%d%d%d",&L,&R,&X); X ^ = Xor, ans = 0; query(rot[L-1],rot[R],25); printf("%d\n",ans); } } } int main(){ ci [0] = 1; for(int i=1;i<=25;i++) ci[i]=ci[i-1]<<1; scanf("%d%d",&N,&M); nil->Sum=0,N++; nil->ch[0]=nil->ch[1]=cnt=rot[0]=nil; rot[1]=update(rot[0],25); for(int i=2;i<=N;i++){ scanf("%d",&now),Xor^=now; rot[i]=update(rot[i-1],25); } solve(); return 0; }