蒟蒻求助,30分,替罪羊树,总会死循环,找不到理由

P6136 【模板】普通平衡树(数据加强版)

_13858886080 @ 2022-08-10 21:20:42

#include<bits/stdc++.h>
using namespace std;
inline int read(){
    int p=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){p=p*10+c-'0';c=getchar();}
    return f*p;
}
int a,s,gen,tot;
struct tree{int q,w,e,daxiao,shuliang;}shu[1500010];
int zx[1500010],tt;
inline void shangchuan(int z){shu[z].daxiao=shu[shu[z].q].daxiao+shu[shu[z].w].daxiao+shu[z].shuliang;}
inline int zaoshu(int z,int x){
    if(z>x)return 0;
    if(z==x){shu[zx[z]].q=shu[zx[z]].w=0;shangchuan(zx[z]);return zx[z];}
    int mid=(z+x)/2;
    shu[zx[mid]].q=zaoshu(z,mid-1);shu[zx[mid]].w=zaoshu(mid+1,x);
    shangchuan(zx[mid]);
    return zx[mid];
}
inline void zhongxu(int z){if(!z)return;zhongxu(shu[z].q);zx[++tt]=z;zhongxu(shu[z].w);}
inline void chongjian(int&z){
    if(shu[z].e&&shu[z].daxiao*0.7<max(shu[shu[z].q].daxiao,shu[shu[z].w].daxiao))
    tt=0,zhongxu(z),z=zaoshu(1,tt);
}
inline void jiashu(int&z,int x){
    if(!z){z=++tot;shu[z].e=x;shu[z].shuliang=shu[z].daxiao=1;return;}
    if(shu[z].e==x)shu[z].shuliang++;
    //shu[z].daxiao++;
    else if(shu[z].e>x)jiashu(shu[z].q,x);
    else jiashu(shu[z].w,x);
    shangchuan(z);chongjian(z);
}
inline int shanshu(int&z){
    int re;
    if(shu[z].q){re=shanshu(shu[z].q);shangchuan(z);}
    else re=z,z=shu[z].w;
    return re;
}
inline void jianshu(int&z,int x){
    if(shu[z].e>x)jianshu(shu[z].q,x),shangchuan(z);
    if(shu[z].e<x)jianshu(shu[z].w,x),shangchuan(z);
    if(shu[z].e==x){
        if(shu[z].shuliang>1)shu[z].shuliang--,shu[z].daxiao--;
        else if(!shu[z].q)z=shu[z].w;
        else if(!shu[z].w)z=shu[z].q;
        else{
            int c=shanshu(shu[z].w);
            shu[z].e=shu[c].e;shu[z].shuliang=shu[c].shuliang;
            shangchuan(z);
        }
    }
}
inline int zhaoshu(int z,int x){
    if(shu[z].e==x)return shu[shu[z].q].daxiao+1;
    if(shu[z].e>x)return zhaoshu(shu[z].q,x);
    if(shu[z].w)return zhaoshu(shu[z].w,x)+shu[z].daxiao-shu[shu[z].w].daxiao;
    return shu[shu[z].q].daxiao+1;
}
inline int paiming(int z,int x){
    if(shu[shu[z].q].daxiao>=x)return paiming(shu[z].q,x);
    if(x>shu[shu[z].q].daxiao+shu[z].shuliang)
    return paiming(shu[z].w,x-shu[shu[z].q].daxiao-shu[z].shuliang);
    return shu[z].e;
}
int re;
inline int qianqu(int z,int x){while(z)if(x<=shu[z].e)z=shu[z].q;else re=z,z=shu[z].w;return shu[re].e;}
inline int houqu(int z,int x){while(z)if(x>=shu[z].e)z=shu[z].w;else re=z,z=shu[z].q;return shu[re].e;}
int lla,ans,z,x;
int main(){
//  freopen("P6136_2.in","r",stdin);
//  freopen("P6136_2k.out","w",stdout);
    a=read();s=read();
    for(int i=1;i<=a;i++){z=read();jiashu(gen,z);}
    while(s--){
        z=read(),x=read()^lla;
        if(z==1)jiashu(gen,x);
        if(z==2)jianshu(gen,x);
        if(z==3)lla=zhaoshu(gen,x),ans^=lla;
        if(z==4)lla=paiming(gen,x),ans^=lla;
        if(z==5)lla=qianqu(gen,x),ans^=lla;
        if(z==6)lla=houqu(gen,x),ans^=lla;
    }
    cout<<ans;
    return 0;
}


|