WA+TLE 10 求助

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

Thunder_S @ 2021-10-11 20:20:14

#include<cstdio>
#include<stdlib.h>
#define N 2000005
using namespace std;
int n,m,opt,x,tot,rt,last,ans,a[N],son[N][2],rd[N],size[N];
int read()
{
    int res=0;char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar();
    return res;
}
int make(int x)
{
    son[++tot][0]=son[tot][1]=0;
    rd[tot]=rand();
    a[tot]=x;
    size[tot]=1;
    return tot;
}
void upd(int x) {size[x]=size[son[x][0]]+size[son[x][1]]+1;}
void split(int now,int v,int &x,int &y)
{
    if (!now)
    {
        x=y=0;
        return; 
    } 
    if (a[now]<=v)
    {
        x=now;
        split(son[now][1],v,son[now][1],y);
        upd(x);
    }
    else
    {
        y=now;
        split(son[now][0],v,x,son[now][0]);
        upd(y);
    }
}
int merge(int x,int y)
{
    if (!x||!y) return x+y;
    if (rd[x]>rd[y])
    {
        son[x][1]=merge(son[x][1],y);
        upd(x);
        return x;
    }
    else
    {
        son[y][0]=merge(x,son[y][0]);
        upd(y);
        return y; 
    }
}
void ins(int v)
{
    int x,y;
    split(rt,v,x,y);
    rt=merge(merge(x,make(v)),y);
}
void del(int v)
{
    int x,y,z;
    split(rt,v-1,x,y);
    split(y,v,y,z);
    y=merge(son[y][0],son[y][1]);
    rt=merge(merge(x,y),z);
}
int get_rank(int v)
{
    int x,y;
    split(rt,v-1,x,y);
    int res=size[x]+1;
    rt=merge(x,y);
    return res;
}
int get_sum(int now,int v)
{
    if (v<=size[son[now][0]]) return get_sum(son[now][0],v);
    if (v<=size[son[now][0]]+1) return now;
    return get_sum(son[now][1],v-size[son[now][0]]-1);
}
int get_pre(int v)
{
    int x,y;
    split(rt,v-1,x,y);
    int res=get_sum(x,size[x]);
    rt=merge(x,y);
    return a[res]; 
}
int get_suf(int v)
{
    int x,y;
    split(rt,v,x,y);
    int res=get_sum(y,1);
    rt=merge(x,y);
    return a[res];
}
int main()
{
    n=read();m=read();
    for (int i=1;i<=n;++i)
        x=read(),ins(x);
    last=0;
    while (m--)
    {
        opt=read();x=read();
        x^=last;
        if (opt==1) ins(x);
        if (opt==2) del(x);
        if (opt==3) ans^=get_rank(x),last=get_rank(x);
        if (opt==4) ans^=a[get_sum(rt,x)],last=a[get_sum(rt,x)];
        if (opt==5) ans^=get_pre(x),last=get_pre(x);
        if (opt==6) ans^=get_suf(x),last=get_suf(x);
    }
    printf("%d\n",ans);
    return 0;
}

|