WBLT求调76tps(悬赏一个关注)码风优美,普通版已过

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

hfjh @ 2023-03-10 16:46:18

错误点

#include<bits/stdc++.h>
using namespace std;
const int N = 5e6 + 10,radio = 4,inf = 1e9 + 7;
int n,opt,x,tot = 0,rt = 0,m;
int ch[2 * N][2],v[N],fa[N],siz[N],last = 0,ans = 0;
void print(){
    printf("根%d:",rt);
    for(int i = 1;i <= 20; ++i){
        printf("编号%dZ父亲%d左儿子:%d右儿子:%d值:%d大小:%d\n",i,fa[i],ch[i][0],ch[i][1],v[i],siz[i]);
    }
}
int read(){
    int s = 0,w = 1;
    char c = getchar();
    while(c < '0'||c > '9'){
        if(c == '-') w = -1;
        c = getchar();
    }
    while(c >= '0'&&c <= '9'){
        s = s * 10 + c -'0';
        c = getchar();
    }
    return s * w;
}
int getp(int x){
    return ch[fa[x]][1] == x;
}
void updata(int x){
    if(!ch[x][0]){
        siz[ch[x][0]] = 1;
        return ;
    }
    siz[x] = siz[ch[x][1]] + siz[ch[x][0]];
    if(ch[x][1] != 0) v[x] = v[ch[x][1]];
}
void clear(int x){
    siz[x] = ch[x][0] = ch[x][1] = fa[x] = 0;
    v[x] = -inf;
}
void rotate(int x,bool bj){
    swap(ch[x][0],ch[x][1]);
    if(!bj){//左边多 
        swap(ch[x][0],ch[ch[x][1]][0]);
    }else if(bj){//右边多 
        swap(ch[x][1],ch[ch[x][0]][1]); 
    } 
    if(!bj){
        swap(ch[ch[x][1]][0],ch[ch[x][1]][1]);
    }else if(bj){
        swap(ch[ch[x][0]][0],ch[ch[x][0]][1]);
    } 
}
void maintain(int x){
    if(siz[ch[x][0]] > siz[ch[x][1]] * radio){
        rotate(x,0);
        updata(ch[x][1]);
        fa[ch[ch[x][1]][1]] = ch[x][1]; 
    }
    if(siz[ch[x][1]] > siz[ch[x][0]] * radio){
        rotate(x,1);
        updata(ch[x][0]);
        fa[ch[ch[x][0]][0]] = ch[x][0];
    }   
    fa[ch[x][0]] = x;
    fa[ch[x][1]] = x;
}
void insert(int x,int vv){
    if(ch[x][0] == 0 && ch[x][1] == 0){
        ch[x][0] = ++tot;
        v[ch[x][0]] = min(vv,v[x]);
        ch[x][1] = ++tot;
        fa[ch[x][0]] = x;
        fa[ch[x][1]] = x;
        v[ch[x][1]] = max(vv,v[x]);
        siz[ch[x][0]] = siz[ch[x][1]] = 1;
        updata(x);
        return ;
    }
    if(v[ch[x][0]] >= vv)insert(ch[x][0],vv);
    else insert(ch[x][1],vv);
    updata(x);maintain(x);
}
void del(int x,int vv){
    if(ch[x][0] == 0){
        if(v[x] != vv) return ;
        if(x == rt) rt = 0;
        if(fa[x] == rt){
            rt = ch[fa[x]][getp(x) ^ 1];
        }
        ch[fa[fa[x]]][getp(fa[x])] = ch[fa[x]][getp(x) ^ 1];
        fa[ch[fa[x]][getp(x) ^ 1]] = fa[fa[x]];
        clear(fa[x]);
        clear(x);
        return ;
    }
    if(v[ch[x][0]] < vv) del(ch[x][1],vv);
    else del(ch[x][0],vv);
    updata(x);maintain(x); 
}
int find_x(int x,int vv){//查询 x 数的排名(排名定义为比当前数小的数的个数 +1+1 )
    if(ch[x][0] == 0) return 0;
    if(v[ch[x][0]] < vv) return siz[ch[x][0]] + find_x(ch[x][1],vv);
    else return find_x(ch[x][0],vv);
}
int find_k(int x,int vv){// 查询排名为 x 的数
    if(ch[x][0] == 0) return v[x];
    if(siz[ch[x][0]] < vv) return find_k(ch[x][1],vv - siz[ch[x][0]]);
    else return find_k(ch[x][0],vv);
}

void op(){
    n=read();m=read();
    for(int i = 1; i <= N - 1; ++i) v[i] = -inf;
    for(int i = 1; i <= n; ++i){
        x = read();
        if(rt == 0){
            rt = ++tot;
            v[1] = x; 
            siz[1] = 1;
        }else insert(rt,x);
    }
    for(int i = 1; i <= m; ++i){
        opt = read();x = read(); 
        x = x ^ last;
        if(opt == 1){
            if(rt == 0){
                rt = ++tot;
                v[1] = x; 
                siz[1] = 1;
            }else insert(rt,x);
        }else if(opt == 2){
            del(rt,x);
        }else if(opt == 3){
            last = find_x(rt,x) + 1;
            ans = ans ^ last;
        }else if(opt == 4){
            last = find_k(rt,x);
            ans = ans ^ last;
        }else if(opt == 5){
            insert(rt,x);
            last = find_k(rt,find_x(rt,x));
            ans = ans ^ last;
            del(rt,x);
        }else if(opt == 6){
            insert(rt,x);
            last = find_k(rt,find_x(rt,x + 1) + 1);
            ans = ans ^ last;
            del(rt,x);
        }
    }
}
int main(){
    op();
//  print();
    printf("%d",ans);
    return 0;
}

|