1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| #include<iostream> #include<cstdio> #include<cstdlib> #define ll long long using namespace std; namespace Ehnaev{ inline ll read() { ll ret=0,f=1;char ch=getchar(); while(ch<48||ch>57) {if(ch==45) f=-f;ch=getchar();} while(ch>=48&&ch<=57) {ret=(ret<<3)+(ret<<1)+ch-48;ch=getchar();} return ret*f; } inline void write(ll x) { static char buf[22];static ll len=-1; if(x>=0) {do{buf[++len]=x%10+48;x/=10;}while(x);} else {putchar(45);do{buf[++len]=-(x%10)+48;x/=10;}while(x);} while(len>=0) putchar(buf[len--]); } }using Ehnaev::read;using Ehnaev::write; inline void writeln(ll x) {write(x);putchar(10);}
const ll N=2e7;
ll bufch[2][N+5],bufval[N+5],bufsiz[N+5],bufrnd[N+5] ,bufrev[N+5],bufrt[N+5],bufdat[N+5]; ll *nowch[2]={bufch[0],bufch[1]},*nowval=bufval,*nowsiz=bufsiz ,*nowrnd=bufrnd,*nowrev=bufrev,*nowrt=bufrt,*nowdat=bufdat;
struct Fhq_Treap{ ll *ch[2],*val,*siz,*rnd,*rev,*dat;ll *rt,sz; inline void Init(ll x) { x+=3;ch[0]=nowch[0];ch[1]=nowch[1];val=nowval;siz=nowsiz; rnd=nowrnd;rev=nowrev;rt=nowrt;dat=nowdat; nowch[0]+=x;nowch[1]+=x;nowval+=x;nowsiz+=x; nowrnd+=x;nowrev+=x;nowrt+=x;nowdat+=x; } inline void Pushup(ll x) { siz[x]=siz[ch[0][x]]+siz[ch[1][x]]+1; dat[x]=dat[ch[0][x]]+dat[ch[1][x]]+val[x]; } inline void Pushdown(ll x) { if(rev[x]) { if(ch[0][x]) {sz++;Assign(sz,ch[0][x]);ch[0][x]=sz;} if(ch[1][x]) {sz++;Assign(sz,ch[1][x]);ch[1][x]=sz;} swap(ch[0][x],ch[1][x]);rev[x]=0; if(ch[0][x]) rev[ch[0][x]]^=1; if(ch[1][x]) rev[ch[1][x]]^=1; } } inline void Assign(ll x,ll y) { ch[0][x]=ch[0][y];ch[1][x]=ch[1][y];val[x]=val[y];siz[x]=siz[y]; rnd[x]=rnd[y];rev[x]=rev[y];dat[x]=dat[y]; } inline void Split(ll p,ll k,ll &x,ll &y) { if(!p) {x=y=0;return;}Pushdown(p); if(siz[ch[0][p]]<k) { x=++sz;Assign(x,p); Split(ch[1][x],k-siz[ch[0][p]]-1,ch[1][x],y);Pushup(x); } else { y=++sz;Assign(y,p); Split(ch[0][y],k,x,ch[0][y]);Pushup(y); } } inline ll Merge(ll x,ll y) { if(!x||!y) return x|y;Pushdown(x);Pushdown(y); if(rnd[x]<rnd[y]) {ch[1][x]=Merge(ch[1][x],y);Pushup(x);return x;} else {ch[0][y]=Merge(x,ch[0][y]);Pushup(y);return y;} } inline void Ins(ll ver,ll p,ll k) { val[++sz]=k;siz[sz]++;rnd[sz]=rand();dat[sz]=k; if(!rt[ver]) {rt[ver]=sz;return;} ll x,y,z=sz;Split(rt[ver],p,x,y);rt[ver]=Merge(Merge(x,z),y); } inline void Del(ll ver,ll k) { ll x,y,z;Split(rt[ver],k,x,y);Split(x,k-1,x,z);rt[ver]=Merge(x,y); } inline void Rev(ll ver,ll l,ll r) { ll x,y,z;Split(rt[ver],r,x,y);Split(x,l-1,x,z); rev[z]^=1;rt[ver]=Merge(Merge(x,z),y); } inline ll Ask(ll ver,ll l,ll r) { ll x,y,z,ret;Split(rt[ver],r,x,y);Split(x,l-1,x,z); ret=dat[z];rt[ver]=Merge(Merge(x,z),y);return ret; } }t;
int main() { srand(73939133);ll n;n=read();t.Init(n);
ll lastans=0; for(ll i=1;i<=n;i++) { ll v,op,x,y;v=read();op=read();x=read()^lastans; t.rt[i]=t.rt[v]; if(op==1) {y=read()^lastans;t.Ins(i,x,y);} if(op==2) {t.Del(i,x);} if(op==3) {y=read()^lastans;t.Rev(i,x,y);} if(op==4) {y=read()^lastans;writeln(lastans=t.Ask(i,x,y));} }
return 0; }
|