WA30pts求助

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

LightHouseAC @ 2021-11-29 20:49:19

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
const int N=5000010;
int fa[N],ch[N][2],val[N],rev[N],siz[N],cnt[N],root,ncnt;
inline bool chk(int x){
    return ch[fa[x]][1]==x;
}
inline void pushup(int x){
    siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+cnt[x];
}
inline void rotate(int x){
    int y=fa[x],z=fa[y],k=chk(x),w=ch[x][k^1];
    ch[y][k]=w;fa[w]=y;
    ch[z][chk(y)]=x;fa[x]=z;
    ch[x][k^1]=y;fa[y]=x;
    pushup(y);pushup(x);
}
inline void splay(int x,int goal=0){
    while(fa[x]!=goal){
        int y=fa[x],z=fa[y];
        if(z!=goal){
            if(chk(x)==chk(y))rotate(y);
            else rotate(x);
        }rotate(x);
    }if(!goal)root=x;
}
inline void insert(int x){
    int cur=root,p=0;
    while(cur && val[cur]!=x){
        p=cur;
        cur=ch[cur][x>val[cur]];
    }if(cur)cnt[cur]++;
    else{
        cur=++ncnt;
        if(p)ch[p][x>val[p]]=cur;
        fa[cur]=p;val[cur]=x;
        siz[cur]=cnt[cur]=1;
        ch[cur][0]=ch[cur][1]=0;
    }splay(cur);
}
inline int kth(int k){
    k++;
    int cur=root;
    while(1){
        if(ch[cur][0] && k<=siz[ch[cur][0]])
            cur=ch[cur][0];
        else if(k>siz[ch[cur][0]]+cnt[cur]){
            k-=siz[ch[cur][0]]+cnt[cur];
            cur=ch[cur][1];
        }else{
            splay(cur);
            return cur;
        }
    }
}
inline void find(int x){
    int cur=root;
    while(ch[cur][x>val[cur]] && val[cur]!=x)
        cur=ch[cur][x>val[cur]];
    splay(cur);
}
inline int Rank(int x){
    find(x);
    /*if(val[root]>=x)printf("%d\n",siz[ch[root][0]]);
    else printf("%d\n",siz[ch[root][0]]+cnt[root]);*/
    return siz[ch[root][0]];
}
inline int pre(int x){
    find(x);
    if(val[root]<x)return root;
    int cur=ch[root][0];
    while(ch[cur][1])
        cur=ch[cur][1];
    splay(cur);
    return cur;
}
inline int succ(int x){
    find(x);
    if(val[root]>x)return root;
    int cur=ch[root][1];
    while(ch[cur][0])
        cur=ch[cur][0];
    splay(cur);
    return cur;
}
inline void remove(int x){
    int last=pre(x),next=succ(x);
    splay(last);splay(next,last);
    int del=ch[next][0];
    if(cnt[del]>1){
        cnt[del]--;splay(del);
    }
    else
        ch[next][0]=0;
}
inline int read(){
    int data=0,w=1;char ch=0;
    while(ch!='-' && (ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')w=-1,ch=getchar();
    while(ch>='0' && ch<='9')data=data*10+ch-'0',ch=getchar();
    return data*w;
}
int n,m;
int main(){
    n=read();m=read();
    int opt,x;
    insert(-0x7fffffff);insert(0x7fffffff);
    for(int i=1;i<=n;i++)
        insert(read());
    int ans=0,last=0,y=0;
    while(m--){
        opt=read();x=read();
        x^=last;
        switch(opt){
            case 1:{
                insert(x);
                break;
            }case 2:{
                remove(x);
                break;
            }case 3:{
                y=Rank(x);
                ans^=y;
                last=y;
                break;
            }case 4:{
//              printf("%d\n",val[kth(x)]);
                y=val[kth(x)];
                ans^=y;
                last=y;
                break;
            }case 5:{
//              printf("%d\n",val[pre(x)]);
                y=val[pre(x)];
                ans^=y;
                last=y;
                break;
            }case 6:{
//              printf("%d\n",val[succ(x)]);
                y=val[succ(x)];
                ans^=y;
                last=y;
                break;
            }
        }
    }
    printf("%d",ans);
    return 0;
}

调了一年没调出来,枯了,复制的代码改一改就交都WA


by Carnival @ 2021-11-29 20:56:36

@LightHouseAC 会不会是因为要查前驱、后继或者排名的那个数不在平衡树中?


by LightHouseAC @ 2021-11-29 23:10:06

@Gamemode 有可能,我改一下试试


|