Splay过了模板1,加强版全TLE,求助

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

黑影洞人 @ 2022-07-26 14:17:16

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cctype> 
#define N 200005
#define int long long
#define inf 2147483647
using namespace std;
inline int read(){
    int ans=0,flg=1;char c=getchar();
    while(!isdigit(c)){if(c=='-')flg=-1;c=getchar();}
    while(isdigit(c)){ans=(ans<<3)+(ans<<1)+c-48;c=getchar();}
    return ans*flg;
}
struct Splay{
    int ch[N][2],fa[N],val[N],cnt[N],size[N],tot,root;
    void clear(int sz=N-1){
        for(int i=0;i<=sz;i++)ch[i][0]=ch[i][1]=fa[i]=val[i]=cnt[i]=size[i]=0;
        root=tot=0;
    }
    int son(int x){return ch[fa[x]][1]==x;}
    void update(int x){size[x]=size[ch[x][0]]+size[ch[x][1]]+cnt[x];}
    void connect(int x,int y,int son){fa[x]=y;ch[y][son]=x;}
    void rotate(int x){
        int y=fa[x],z=fa[y],k=son(x),v=ch[x][!k];
        connect(v,y,k);connect(x,z,son(y));connect(y,x,!k);
        update(y),update(x);
    } 
    void splay(int x,int goal=0){
        while(fa[x]!=goal){
            int y=fa[x],z=fa[y];
            if(z!=goal)rotate(son(x)==son(y)?y:x);
            rotate(x);
        }
        if(!goal)root=x;
    }
    int find(int x){
        int p=root;
        for(;ch[p][x>val[p]]&&x!=val[p];p=ch[p][x>val[p]]);
        splay(p);return p;
    }
    int rank(int k){return size[ch[find(k)][0]];}
    int kth(int k){
       int p=root;
         while(1){
          if(ch[p][0]&&k<=size[ch[p][0]])p=ch[p][0];
          else if(k>size[ch[p][0]]+cnt[p]){
             k-=size[ch[p][0]]+cnt[p];
             p=ch[p][1];
          }else return p;
      }
   }
   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=++tot;
        if(p)ch[p][x>val[p]]=cur;
        ch[cur][0]=ch[cur][1]=0;
        fa[cur]=p;val[cur]=x;
        cnt[cur]=size[cur]=1;
        }
        splay(cur);
   }
    int prev(int x){
    //  splay(x);
        find(x);
        if(val[root]<x)return root;
      int p=ch[root][0];
      while(ch[p][1])p=ch[p][1];
      return p;
   }
   int next(int x){
   //   splay(x);
    find(x);
        if(val[root]>x)return root;
      int p=ch[root][1];
      while(ch[p][0])p=ch[p][0];
      return p;
   }  
   void del(int x){
    int last=prev(x),nxt=next(x);
    splay(last);splay(nxt,last);
    int delt=ch[nxt][0];
    if(cnt[delt]>1)cnt[delt]--,splay(delt);
    else ch[nxt][0]=0,update(nxt),update(root);
   }
}fifi;
int n,last,m,ans;
signed main(){
    n=read(),m=read();
    for(int i=1;i<=n;i++)fifi.insert(read());
    fifi.insert(inf),fifi.insert(-inf);
    while(m--){
        int op=read(),x=read();
        x^=last;
        switch(op){
            case 1:fifi.insert(x);break;
            case 2:fifi.del(x);break;
            case 3:last=fifi.rank(x);ans^=last;break;
            case 4:last=fifi.val[fifi.kth(x+1)];ans^=last;break;
            case 5:last=fifi.val[fifi.prev(x)];ans^=last;break;
            case 6:last=fifi.val[fifi.next(x)];ans^=last;break;
        }
    }
    printf("%lld",ans); 
    return 0;
}

by zjhztxy @ 2022-07-26 14:21:21

prev,next操作后应splay


by The_BJX @ 2022-07-26 14:28:16

@黑影洞人

您访问完不splay,链卡成傻逼


by 黑影洞人 @ 2022-07-26 14:34:04

@The_BJX 多谢(orz%%%%%%%%%%%%%%%)(doge)


by 黑影洞人 @ 2022-07-26 14:39:45

@The_BJX tmd现在30分


by 345678910jqka @ 2022-09-27 20:22:42

有没有一种可能数组开小了


by 966123anyunchuan @ 2023-07-23 22:50:36

我也是数组开小了,改了就 ac 了


|