TLE 0 求助

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

Thunder_S @ 2021-10-17 21:26:05

//#pragma GCC optimize(2)
#include<cstdio>
//#include<stdlib.h>
#define N 1100005
#define ll long long 
using namespace std;
int n,m,opt,x,tot,rt,last,ans,a[N<<2],son[N<<2][2],rd[N<<2],size[N<<2];
ll rad=1;
int read()
{
    int res=0;char ch=getchar(),fh=' ';
    while (ch<'0'||ch>'9') fh=ch,ch=getchar();
    while (ch>='0'&&ch<='9') res=(res<<1)+(res<<3)+(ch-'0'),ch=getchar();
    if (fh=='-') res=-res;
    return res;
}
int Rand() {rad*=1919810;return (int)rad;}
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);
    }
    else
    {
        y=now;
        split(son[now][0],v,x,son[now][0]);
    }
    upd(now);
}
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,pos=++tot;
    son[tot][0]=son[tot][1]=0;rd[tot]=Rand();a[tot]=v;size[tot]=1;
    split(rt,v,x,y);
    rt=merge(merge(x,pos),y);
}
void del(int v)
{
    int x,y,z;
    split(rt,v-1,x,y);
    if (!y) return; 
    split(y,v,y,z);
    y=merge(son[y][0],son[y][1]);
    rt=merge(x,merge(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);
    while (m--)
    {
        opt=read();x=read();
        x^=last;
        int sum=0;
        if (opt==1) ins(x);
        if (opt==2) del(x);
        if (opt==3) last=get_rank(x);
        if (opt==4) last=a[get_sum(rt,x)];
        if (opt==5) last=get_pre(x);
        if (opt==6) last=get_suf(x);
        ans^=last;
    }
    printf("%lld\n",ans);
    return 0;
} 

by ___balalida___ @ 2021-11-01 18:30:33

你应该是WA了


|