Operation
Time limit: 5 Sec Memory Limit: 256 MBTitle Description
There is an integer sequence a of length n and there are two kinds of operations:
0 l r: select some numbers from al...ar so that their xor sum is maximum, and print the maximum value.
1 x: append x to the end of the sequence and let n=n+1.
0 l r: select some numbers from al...ar so that their xor sum is maximum, and print the maximum value.
1 x: append x to the end of the sequence and let n=n+1.
Entry
There are multiple test cases. The first line of input contains an integer T(T≤10), indicating the number of test cases.
For each test case:
The first line contains two integers n,m(1≤n≤5×105,1≤m≤5×105), the number of integers initially in the sequence and the number of operations.
The second line contains n integers a1,a2,...,an(0≤ai<230), denoting the initial sequence.
Each of the next m lines contains one of the operations given above.
It's guaranteed that ∑n≤106,∑m≤106,0≤x<230.
And operations will be encrypted. You need to decode the operations as follows, where lastans denotes the answer to the last type 0 operation and is initially zero:
For every type 0 operation, let l=(l xor lastans)mod n + 1, r=(r xor lastans)mod n + 1, and then swap(l, r) if l>r.
For every type 1 operation, let x=x xor lastans.
For each test case:
The first line contains two integers n,m(1≤n≤5×105,1≤m≤5×105), the number of integers initially in the sequence and the number of operations.
The second line contains n integers a1,a2,...,an(0≤ai<230), denoting the initial sequence.
Each of the next m lines contains one of the operations given above.
It's guaranteed that ∑n≤106,∑m≤106,0≤x<230.
And operations will be encrypted. You need to decode the operations as follows, where lastans denotes the answer to the last type 0 operation and is initially zero:
For every type 0 operation, let l=(l xor lastans)mod n + 1, r=(r xor lastans)mod n + 1, and then swap(l, r) if l>r.
For every type 1 operation, let x=x xor lastans.
Export
For each type 0 operation, please output the maximum xor sum in a single line.
Sample input
1
3 3
0 1 2
0 1 1
1 3
0 3 4
Sample Output
1
3
The meaning of problems : a sequence of two operations: interrogation zone XOR maximum value (alternatively interval arbitrary number XOR), add a number to the end of the interval.
Ideas : First, big idea is to find the maximum interval XOR linear group, and then consider how to find a maximum value of any of the exclusive OR section. Consider greedy, to construct a linear group prefix (i.e., the i-th group represents a linear 1-i of these numbers substrate), and then add the number of each group is a linear, when the number of reserved greedy rearward position, thus ensuring each number greater than themselves, and only a few locations or different. Then query interval [L, R], when the first linear R groups which L greater than or equal to query the location of the substrate, and the answer statistics.
Reference linear group blog : https://blog.csdn.net/qaq__qaq/article/details/53812883
interval and the maximum reference XOR blog : https://www.cnblogs.com/hua-dong/p/10266216.html
#include<bits/stdc++.h> using namespace std; struct L_B { int d[30+1],p[30+1]; L_B() { memset(d,0,sizeof(d)); memset(p,0,sizeof(p)); } void operator = (const L_B & s) { for(int i=0;i<31;i++)d[i]=s.d[i],p[i]=s.p[i]; } bool insert(int val,int pos) { for (int i=30;i>=0;i--) if (val&(1LL<<i)) { if (!d[i]) { d[i]=val; p[i]=pos; break; } if(p[i]<pos) { int now; now=p[i]; p[i]=pos; pos=now; now=d[i]; d[i]=val; val=now; // swap((long long)pos,p[i]); //swap((long long)val,d[i]); } val^=d[i]; } return val>0; } int query_max(int l) { int ret=0; for (int i=30;i>=0;i--) if (p[i]>=l&&(ret^d[i])>ret) ret^=d[i]; return ret; } }; L_B pre[1000050]; int main() { int t; scanf("%d",&t); while(t--) { int n,m; int ans=0; scanf("%d %d",&n,&m); for(int i=1;i<=n;i++) { int a; scanf("%d",&a); pre[i]=pre[i-1]; pre[i].insert(a,i); } while(m--) { int op; scanf("%d",&op); if(op==1) { int x; scanf("%d",&x); x=x^ans; n++; pre[n]=pre[n-1]; pre[n].insert(x,n); } else { int l,r; scanf("%d %d",&l,&r); l=(l^ans)%n+1; r=(r^ans)%n+1; if(l>r)swap(l,r); years = pre[r].query_max(l); printf ( " % d \ n " , year); } } } Return 0 ; }