「模板」可持久化 HFQ-Treap
老师用的是静态数组的写法,开了很多数组…
其实个人更倾向于 struct
或者用 class
封装起来。
但是鉴于太难打 好吧,是我懒得打。
然后就借鉴了老师的模板,写出了属于自己的 压行 风格。
代码见下:(注:题目是洛谷P3835)
#include<cstdio>
#include<cstdlib>
#define rep(i,__l,__r) for(int i=__l,i##_end_=__r;i<=i##_end_;++i)
#define fep(i,__l,__r) for(int i=__l,i##_end_=__r;i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define pii pair<int,int>
#define Endl putchar('\n')
// #define FILEOI
#ifdef FILEOI
#define MAXBUFFERSIZE 500000
inline char fgetc(){
static char buf[MAXBUFFERSIZE+5],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXBUFFERSIZE,stdin),p1==p2)?EOF:*p1++;
}
#undef MAXBUFFERSIZE
#define cg (c=fgetc())
#else
#define cg (c=getchar())
#endif
template<class T>inline void qread(T& x){
char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
if(f)x=-x;
}
inline int qread(){
int x=0;char c;bool f=0;
while(cg<'0'||'9'<c)f|=(c=='-');
for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
return f?-x:x;
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){
if(x<0)return (void)(putchar('-'),fwrit(-x));
if(x>9)fwrit(x/10);putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}
const int MAXN=5e7;
const int INF=(1ll<<31)-1;
int ncnt;
int son[MAXN+5][2];
int keyof[MAXN+5];
int rd[MAXN+5];
int siz[MAXN+5];
int rt[MAXN+5];
int rcnt;
inline void pushup(const int p){
siz[p]=siz[son[p][0]]+siz[son[p][1]]+1;
}
inline int newnode(const int key){
++ncnt;
son[ncnt][0]=son[ncnt][1]=0;
keyof[ncnt]=key;
rd[ncnt]=rand();
siz[ncnt]=1;
return ncnt;
}
struct Pair{
int ele[2];
Pair(){ele[0]=ele[1]=0;}
Pair(const int A,const int B){ele[0]=A,ele[1]=B;}
};
inline int cpyNode(const int p){
++ncnt;
son[ncnt][0]=son[p][0];
son[ncnt][1]=son[p][1];
keyof[ncnt]=keyof[p];
rd[ncnt]=rd[p];
siz[ncnt]=siz[p];
return ncnt;
}
Pair split(int p,const int key,const bool mode,const bool flg=0){
//mode=0 时为按 key 分裂
//mode=1 时为按 rank 分裂
Pair ret=Pair(0,0);
if(p){
int d;
if(!flg)p=cpyNode(p);
if(mode)d=(siz[son[p][0]]+1<=key);
else d=(keyof[p]<=key);
if(mode && d)ret=split(son[p][d],key-siz[son[p][0]]-1,mode,flg);
else ret=split(son[p][d],key,mode,flg);
son[p][d]=ret.ele[d^1];
pushup(p);
ret.ele[d^1]=p;
}
return ret;
}
int unite(int r1,int r2,const bool flg=0){
if(r1*r2==0)return r1|r2;
if(rd[r1]<rd[r2]){
if(!flg)r1=cpyNode(r1);
son[r1][1]=unite(son[r1][1],r2,flg);
return pushup(r1),r1;
}
else{
if(!flg)r2=cpyNode(r2);
son[r2][0]=unite(r1,son[r2][0],flg);
return pushup(r2),r2;
}
}
int inser(const int p,const int key){
Pair tmp=split(p,key,0);
return unite(unite(tmp.ele[0],newnode(key)),tmp.ele[1]);
}
int delet(const int p,const int key){
Pair tmp0=split(p,key,0);
Pair tmp1=split(tmp0.ele[0],key-1,0);
return unite(tmp1.ele[0],tmp0.ele[1]);
}
int getRank(const int p,const int key){
if(!p)return 1;
if(keyof[p]>key)return getRank(son[p][0],key);
if(keyof[p]<key)return siz[son[p][0]]+1+getRank(son[p][1],key);
return siz[son[p][0]]+1;
}
int atRank(const int p,const int rk){
if(!p)return -INF;
if(siz[son[p][0]]>=rk)return atRank(son[p][0],rk);
else if(siz[son[p][0]]+1<rk)return atRank(son[p][1],rk-siz[son[p][0]]-1);
else return keyof[p];
}
int getPre(const int p,const int key){
Pair tmp1=split(p,key-1,0,1);
int tsiz=siz[tmp1.ele[0]]-1;
Pair tmp0=split(tmp1.ele[0],tsiz,1,1);
int ret=keyof[tmp0.ele[1]];
tmp1.ele[0]=unite(tmp0.ele[0],tmp0.ele[1],1);
unite(tmp1.ele[0],tmp1.ele[1],1);
return ret;
}
int getSuf(const int p,const int key){
Pair tmp0=split(p,key,0,1);
Pair tmp1=split(tmp0.ele[1],1,1,1);
int ret=keyof[tmp1.ele[0]];
tmp0.ele[1]=unite(tmp1.ele[0],tmp1.ele[1],1);
unite(tmp0.ele[0],tmp0.ele[1],1);
return ret;
}
int N,v,opt,x;
signed main(){
#ifdef FILEOI
freopen("file.in","r",stdin);
freopen("file.out","w",stdout);
#endif
srand(233);
qread(N);
rt[0]=inser(rt[0],-INF);
rt[0]=inser(rt[0],INF);
while(N--){
qread(v,opt,x);
rt[++rcnt]=rt[v];
if(opt==1)rt[rcnt]=inser(rt[rcnt],x);
else if(opt==2)rt[rcnt]=delet(rt[rcnt],x);
else if(opt==3)writc(getRank(rt[rcnt],x)-1,'\n');
else if(opt==4)writc(atRank(rt[rcnt],x+1),'\n');
else if(opt==5)writc(getPre(rt[rcnt],x),'\n');
else writc(getSuf(rt[rcnt],x),'\n');
}
return 0;
}