萌新求助fhqTreap,第4个点re

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

_zdc_ @ 2021-06-10 22:11:59

#include<bits/stdc++.h>
using namespace std;
const int N=1000001;
int n,p,opt,x,cnt,rt,m,a,b,c,d,t[N][2],siz[N],v[N],key[N],lst,ans;
int new_node(int a){
    siz[++cnt]=1;
    v[cnt]=a;
    key[cnt]=rand();
    return cnt;
}
void push_up(int m){
    siz[m]=siz[t[m][0]]+siz[t[m][1]]+1;
    return;
}
int merge(int a,int b){
    if(!a || !b) return a+b;
    if(key[a]>key[b]){
        t[b][0]=merge(a,t[b][0]);
        push_up(b);
        return b;
    }else{
        t[a][1]=merge(t[a][1],b);
        push_up(a);
        return a;
    }
}
void split(int cur,int k,int &a,int &b){
    if(!cur){
        a=b=0;
        return;
    }
    if(v[cur]<=k){
        a=cur;
        split(t[cur][1],k,t[a][1],b);
    }else{
        b=cur;
        split(t[cur][0],k,a,t[b][0]);
    }
    push_up(cur);
    return;
}
int kth(int cur,int k){
    m=t[cur][0];
    if(siz[m]+1==k) return cur;
    if(siz[m]+1<k) return kth(t[cur][1],k-siz[m]-1);
    if(siz[m]+1>k) return kth(m,k);
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>p;
    for(int i=1;i<=n;i++){
        cin>>m;
        split(rt,m,a,b);
        rt=merge(merge(a,new_node(m)),b);
    }
    while(p--){
        cin>>opt>>x;
        x^=lst;
        if(opt==1){
            split(rt,x,a,b);
            rt=merge(merge(a,new_node(x)),b);
        }else if(opt==2){
            split(rt,x,a,b);
            split(a,x-1,a,c);
            rt=merge(merge(a,merge(t[c][0],t[c][1])),b);
        }else if(opt==3){
            split(rt,x-1,a,b);
            lst=siz[a]+1;
            rt=merge(a,b);
        }else if(opt==4){
            a=kth(rt,x);
            lst=v[a];
        }else if(opt==5){
            split(rt,x-1,a,b);
            c=kth(a,siz[a]);
            rt=merge(a,b);
            lst=v[c];
        }else if(opt==6){
            split(rt,x,a,b);
            c=kth(b,1);
            rt=merge(a,b);
            lst=v[c];
        }
        if(opt>=3) ans^=lst;
    }
    cout<<ans;
    return 0;
}

by YamadaRyou @ 2021-06-10 22:40:23

@zdcqwq2010 本地测并没有问题qwq


by lxgw @ 2021-06-10 22:54:11

宁只需要把数组大小开到 1100001 就过了 (


by _zdc_ @ 2021-06-11 06:49:41

草,我是智障,谢谢!


|