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 104 105 106 107 108
| #include<iostream> #include<cstdio> #include<cstdlib> #define ll int 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,inf=(1ll<<31)-1;
ll bufch[2][N+5],bufval[N+5],bufsiz[N+5],bufrnd[N+5],bufrt[N+5]; ll *nowch[2]={bufch[0],bufch[1]},*nowval=bufval,*nowsiz=bufsiz ,*nowrnd=bufrnd,*nowrt=bufrt;
struct Fhq_Treap{ ll *ch[2],*val,*siz,*rnd;ll *rt,sz; inline void Init(ll x) { x+=5;ch[0]=nowch[0];ch[1]=nowch[1];val=nowval;siz=nowsiz; rnd=nowrnd;rt=nowrt; nowch[0]+=x;nowch[1]+=x;nowval+=x;nowsiz+=x;nowrnd+=x;nowrt+=x; } inline void Pushup(ll x) {siz[x]=siz[ch[0][x]]+siz[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]; } inline void Split(ll p,ll k,ll &x,ll &y) { if(!p) {x=y=0;return;} if(val[p]<=k) { x=++sz;Assign(x,p);Split(ch[1][x],k,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; 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 Del(ll ver,ll k) { ll x,y,z;Split(rt[ver],k,x,y);Split(x,k-1,x,z); rt[ver]=Merge(Merge(x,Merge(ch[0][z],ch[1][z])),y); } inline void Ins(ll ver,ll k) { ll x,y;Split(rt[ver],k,x,y);val[++sz]=k;siz[sz]++;rnd[sz]=rand(); rt[ver]=Merge(Merge(x,sz),y); } inline ll Rk(ll ver,ll k) { ll x,y,z;Split(rt[ver],k-1,x,y);z=siz[x]+1; rt[ver]=Merge(x,y);return z; } inline ll Kth(ll ver,ll k) { ll cur=rt[ver]; while(1) { if(ch[0][cur]&&k<=siz[ch[0][cur]]) cur=ch[0][cur]; else { k-=siz[ch[0][cur]]+1; if(k<=0) return val[cur];cur=ch[1][cur]; } } } inline ll Pre(ll ver,ll k) { ll x,y; Split(rt[ver],k-1,x,y); if(!x) return -inf; rt[ver]=Merge(x,y); return Kth(ver,Rk(ver,k)-1); } inline ll Nxt(ll ver,ll k) { ll x,y; Split(rt[ver],k,x,y); if(!y) return inf; rt[ver]=Merge(x,y); return Kth(ver,Rk(ver,k+1)); } }t;
int main() { srand(73939133);ll n;n=read();t.Init(n+2); for(ll i=1;i<=n;i++) { ll v,op,x;v=read();op=read();x=read(); t.rt[i]=t.rt[v]; if(op==1) {t.Ins(i,x);} if(op==2) {t.Del(i,x);} if(op==3) {writeln(t.Rk(i,x));} if(op==4) {writeln(t.Kth(i,x));} if(op==5) {writeln(t.Pre(i,x));} if(op==6) {writeln(t.Nxt(i,x));} }
return 0; }
|