https://vijos.org/p/2001
Set Perxor[i] represents the prefix XOR value of 1---i.
So to get the difference or value of a paragraph, only need perxor[j] ^ perxor[i-1]
Then we put Perxor[n] first to add to the dictionary tree, and then use perxor[n-1] to find, find is subscript n contribution.
Similarly, then put perxor[n-1] into, with perxor[n-2] find, will be subscript n-1 contribution. Because this time it can be
Perxor[n-2] ^ perxor[n-1] (because Perxor[n-1] is already in the dictionary tree, so this time corresponds to A[n-1] himself.
Similarly it can be perxor[n-2] ^ perxor[n], that is a[n-1] ^ a[n].
Of course, it also has a m limit, this can be added a delete operation.
It is important to note that:
1, unsigned int is not reserved low 31 bits, it is 2^32 modulo, and 2^32 has 33 bits.
2, the size of the dictionary tree to open Well, the dictionary tree of the struct node *pnext[] only 2 can be. Because the state is only 0 or 1
3, judge whether the first bit is 1 or 0, is ((1 << i) & val) >= 1 do not forget to judge, otherwise re
4, CIN, cout please cancel sync, card me tle
#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<assert.h>#defineIOS Ios::sync_with_stdio (False)using namespacestd;#defineINF (0X3F3F3F3F)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>Const intMAXN = 5e5 + -;intA[MAXN];intPERXOR[MAXN];structNode {intCNT; structNode * pnext[2];} TREE[MAXN* +];intT;structNode *Create () {structNode *p = &tree[t++];//p->cnt = 0;//for (int i = 0; I <= 9; ++i) {//P->pnext[i] = NULL;// } returnp;}voidToinsert (structNode **t,intval) { structNode *p = *T; if(p = =NULL) {P= *t =Create (); } for(inti = -; I >=0; --i) {intid = ((1<< i) & Val) >=1; if(P->pnext[id] = =NULL) {P->pnext[id] =Create (); } P= p->Pnext[id]; P->cnt++; }}voidTodel (structNode **t,intval) { structNode *p = *T; if(p = = NULL)return; for(inti = -; I >=0; --i) {intid = ((1<< i) & Val) >=1; P= p->Pnext[id]; P->cnt--; }}intTofind (structNode *t,intval) { structNode *p =T; if(p = = NULL)return 0; intAns =0;//cout << val << Endl; for(inti = -; I >=0; --i) {intid = ((1<< i) & Val) >=1; if(P->pnext[!id] && p->pnext[!id]->cnt >=1) {ans|= (1<<i); P= p->pnext[!ID]; } Else{p= p->Pnext[id]; } } returnans;}intLISTTOADD[MAXN];ConstLL MOD = (1LL << to);voidWork () {intN, M; CIN>> N >>m; for(inti =1; I <= N; ++i) {cin>>A[i]; } for(inti =1; I <= N; ++i) {Perxor[i]= Perxor[i-1] ^A[i]; }//for (int i = 1; I <= n; ++i) {//cout << perxor[i] << "";// }//cout << Endl;//unsigned int t = (1LL <<);//cout << t << Endl; Long Long intAns =0; structNode *t =NULL; Toinsert (&T, Perxor[n]); intLen =0; listtoadd[++len] =Perxor[n]; intCur =1;//cout << tofind (T, perxor[n]) << Endl; for(inti = n-1; I >=0; --i) {ans+=Tofind (T, perxor[i]); if(ans >= MOD) ans%=MOD;//cout << tofind (T, perxor[i]) << Endl;Toinsert (&T, perxor[i]); listtoadd[++len] =Perxor[i]; if(Len-cur +1>m) {Todel (&t, listtoadd[cur++]); } }//cout << Endl;cout << ans <<Endl;}intMain () {#ifdef local freopen ("Data.txt","R", stdin);//freopen ("Data.txt", "w", stdout);#endifIOS; Work (); return 0;}
View Code
P2001xor-sigma Dictionary tree, however good pits