可持久化平衡树
#include <bits/stdc++.h>
#define rin(i,a,b) for(int i=(a);i<=(b);++i)
#define irin(i,a,b) for(int i=(a);i>=(b);--i)
#define trav(i,a) for(int i=head[a];i;i=e[i].nxt)
#define Size(a) (int)a.size()
#define pb push_back
#define mkpr std::make_pair
#define fi first
#define se second
#define lowbit(a) ((a)&(-(a)))
typedef long long LL;
typedef std::pair<int,int> pii;
using std::cerr;
using std::endl;
inline int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int MAXN=500005;
int n,tot,root[MAXN];
struct fhq{
int fa,ch[2];
int siz;
int val;
int minn,maxn;
int dat;
}a[MAXN*50];
#define lc a[x].ch[0]
#define rc a[x].ch[1]
inline int nnd(int v){return a[++tot]=(fhq){0,0,0,1,v,v,v,rand()},tot;}
int merge(int x,int y);
inline void init(){
a[0].minn=2147483647,a[0].maxn=-2147483647;
root[0]=merge(nnd(-2147483647),nnd(2147483647));
}
inline void pushup(int x){
a[x].siz=a[lc].siz+a[rc].siz+1;
a[x].minn=std::min(std::min(a[lc].minn,a[rc].minn),a[x].val);
a[x].maxn=std::max(std::max(a[lc].maxn,a[rc].maxn),a[x].val);
}
int merge(int x,int y){
if(!x||!y)return x|y;
if(a[x].dat<a[y].dat){
a[++tot]=a[x];x=tot;
a[x].ch[1]=merge(a[x].ch[1],y);
pushup(x);return x;
}
else{
a[++tot]=a[y];y=tot;
a[y].ch[0]=merge(x,a[y].ch[0]);
pushup(y);return y;
}
}
pii split(int x,int v){
if(!x)return mkpr(0,0);
if(a[x].val<=v){
pii ret=split(rc,v);
a[++tot]=a[x];x=tot;rc=ret.fi;
pushup(x);
return mkpr(x,ret.se);
}
else{
pii ret=split(lc,v);
a[++tot]=a[x];x=tot;lc=ret.se;
pushup(x);
return mkpr(ret.fi,x);
}
}
inline int kth(int x,int k){
while(1){
if(a[lc].siz>=k)x=lc;
else if(a[lc].siz+1==k)return x;
else k-=a[lc].siz+1,x=rc;
}
return x;
}
#undef lc
#undef rc
int main(){
n=read();init();
rin(i,1,n){
int verid=read(),opt=read(),x=read();
root[i]=root[verid];
if(opt==1){
pii ret=split(root[i],x);
root[i]=merge(ret.fi,merge(nnd(x),ret.se));
}
else if(opt==2){
pii ret1=split(root[i],x-1),ret2=split(ret1.se,x);
root[i]=merge(merge(ret1.fi,a[ret2.fi].ch[0]),merge(a[ret2.fi].ch[1],ret2.se));
}
else if(opt==3){
pii ret=split(root[i],x-1);
printf("%d
",a[ret.fi].siz);
root[i]=merge(ret.fi,ret.se);
}
else if(opt==4){
printf("%d
",a[kth(root[i],x+1)].val);
}
else if(opt==5){
pii ret=split(root[i],x-1);
printf("%d
",a[ret.fi].maxn);
root[i]=merge(ret.fi,ret.se);
}
else{
pii ret=split(root[i],x);
printf("%d
",a[ret.se].minn);
root[i]=merge(ret.fi,ret.se);
}
}
return 0;
}