WA#5#6求助

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

akk123 @ 2021-04-05 23:06:44

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define res register int
#define inf 1<<30
#define ll long long
using namespace std;
const int maxn=1100005;
int x,tot=0,rt,Ans=0;
struct node{
    int val,ls,rs,pri,cnt,size;
    #define lc(x) Treap[x].ls
    #define rc(x) Treap[x].rs
    #define p(x) Treap[x].pri
    #define v(x) Treap[x].val
    #define c(x) Treap[x].cnt
    #define s(x) Treap[x].size
}Treap[maxn];
inline int read(){
    res x=0, w=0; char ch=0;
    while (!isdigit(ch)) w|=ch=='-', ch=getchar();
    while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48), ch=getchar();
    return w?-x:x;
}
inline void upd(const int &k){s(k)=s(lc(k))+s(rc(k))+c(k);}
inline void Zag(int &k){//left
    res y=rc(k);
    rc(k)=lc(y),lc(y)=k,s(y)=s(k),upd(k),k=y;
}
inline void Zig(int &k){//right
    res y=lc(k);
    lc(k)=rc(y),rc(y)=k,s(y)=s(k),upd(k),k=y;
}
void Insert(int &k,const int &key){
    if(v(k)==key){++s(k),++c(k);return;}
    if(!k){
        k=++tot,v(k)=key,lc(k)=rc(k)=0,c(k)=s(k)=1,p(k)=rand();return;
    }++s(k);
    if(key<v(k)){
        Insert(lc(k),key);
        if(p(lc(k))<p(k)) Zig(k);
    }else{
        Insert(rc(k),key);
        if(p(rc(k))<p(k)) Zag(k);
    }
}
void Delete(int &k,const int &key){
//  if(key==489516703) return;
    if(k==0) return;
    if(v(k)==key){
        if(c(k)>1){--c(k),--s(k);return;}
        else if(!lc(k)||!rc(k)) {k=lc(k)+rc(k);return;}
        if(p(lc(k))<p(rc(k))){
            Zig(k),Delete(k,key);
        }else{
            Zag(k),Delete(k,key);
        }return;
    }--s(k);
    if(key<v(k)&&lc(k)) Delete(lc(k),key);
    else if(key>v(k)&&rc(k)) Delete(rc(k),key);
}
inline int QueryRank(const int &key){
    res k=rt,tem=0;//printf("%d %d %d\n",s(k),lc(k),s(lc(k)));
    while(k){
        if(key==v(k)) return tem+1+s(lc(k));
        if(key<v(k)) k=lc(k);
        else tem+=s(lc(k))+c(k),k=rc(k);
    }return tem+1;
}
inline int QueryKth(int k){
    res x=rt,tem=0;
    while(x){
        if(s(lc(x))<k&&s(lc(x))+c(x)>=k) return v(x);
        if(s(lc(x))>=k) x=lc(x);
        else k-=s(lc(x))+c(x),tem=v(x),x=rc(x);
    }
    return tem;
}
inline int QueryPre(const int &key){
    res k=rt,ans=-inf;
    while(k){
        if(v(k)<key) ans=v(k),k=rc(k);
        else k=lc(k);
    }return ans;
}
inline int QuerySuf(const int &key){
    res k=rt,ans=inf;
    while(k){
        if(v(k)>key) ans=v(k),k=lc(k);
        else k=rc(k);
    }return ans;
}
int main(){
//  freopen("www.in","r",stdin);
    res n,m,opt,x;n=read(),m=read();for(res i=1;i<=n;++i){x=read();Insert(rt,x);}
    ll final=0,last=0;
    for(res i=1;i<=m;++i){
        opt=read(),x=read();
    //  if(i==43) printf("opt:%d  x:%d\n",opt,x);
        if(opt==1) {Insert(rt,x^last);}
        if(opt==2) {Delete(rt,x^last);}
        if(opt==3) last=QueryRank(x^last),final^=last;
        if(opt==4) last=QueryKth(x^last),final^=last;
        if(opt==5) last=QueryPre(x^last),final^=last;
        if(opt==6) last=QuerySuf(x^last),final^=last;
    }printf("%lld\n",final);
    return 0;
}

普通版过了 求大佬帮忙


|