Splay求调
查看原帖
Splay求调
615965
Coffins楼主2023/8/1 20:33

rt,T了三个点

#include<bits/stdc++.h>
using namespace std;
int n,m,op,x;
const int Max=3e6+5;
int fa[Max],ch[Max][2],cnt[Max],val[Max],siz[Max];
int rt,tot=0;
void maintain(int cur)
{
	siz[cur]=siz[ch[cur][0]]+siz[ch[cur][1]]+cnt[cur];
}
void rotate(int x)
{
	int y=fa[x],z=fa[y];
	int lx=(x==ch[y][1]),ly=(y==ch[z][1]);
	fa[x]=z;
	fa[y]=x;
	if(ch[x][lx^1])
	{
		fa[ch[x][lx^1]]=y;
		
	}
	if(z)
	{
		ch[z][ly]=x;
	}
	ch[y][lx]=ch[x][lx^1];
	ch[x][lx^1]=y;
	maintain(y);
	maintain(x);
}
void splay(int x)
{
	while(fa[x])
	{
		rotate(x);
		if(fa[x]&&fa[fa[x]])
		{
			rotate((ch[fa[x]][1]==x)==(ch[fa[fa[x]]][1]==fa[x])?fa[x]:x);
		}
	}
	rt=x;
}
void get(int x)
{
	int cur=rt,f=0;
	while(cur)
	{
		f=cur;
		if(x<val[cur])
		{
			cur=ch[cur][0];
		}
		else if(x==val[cur])
		{
			splay(cur);
			return;
		}
		else if(x>val[cur])
		{
			cur=ch[cur][1];
		}
	}
	ch[f][x>val[f]]=++tot;
	fa[tot]=f;
	val[tot]=x;
	splay(tot);
}
void ins(int x)
{
	get(x);
	cnt[rt]++;
	maintain(rt);
}
void del(int x)
{
	get(x);
	cnt[rt]--;
	maintain(rt);
}
int rnk(int x)
{
	get(x);
	return siz[ch[rt][0]]+1;
}
int kth(int x,int k)
{
	if(k<=siz[ch[x][0]])
	{
		return kth(ch[x][0],k);
	}
	if(k<=siz[ch[x][0]]+cnt[x])
	{
		return val[x];
	}
	return kth(ch[x][1],k-siz[ch[x][0]]-cnt[x]);
}
int pre(int x)
{
	return kth(rt,rnk(x)-1);
}
int nxt(int x)
{
	return kth(rt,rnk(x+1));
}
int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
		{
			f=-1;
		}
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
	{
        x=(x<<3)+(x<<1)+ch-'0';
        ch=getchar();
    }
    return x*f;
}
int main()
{
	/*ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);*/
	n=read();
	m=read();
	for(int i=1;i<=n;i++)
	{
		x=read();
		ins(x);
		//cout<<i<<'\n';
	}
	//cout<<"NYE!!!\n";
	int ans=0,las=0;
	for(int i=1;i<=m;i++)
	{
		op=read();
		x=read();
		x^=las;
		//cout<<op<<" "<<x<<'\n';
		if(op==1)
		{
			ins(x);
		}
		if(op==2)
		{
			del(x);
		}
		if(op==3)
		{
			las=rnk(x);
			ans^=las;
		}
		if(op==4)
		{
			las=kth(rt,x);
			ans^=las;
		} 
		if(op==5)
		{
			las=pre(x);
			ans^=las;
		}
		if(op==6)
		{
			las=nxt(x);
			ans^=las;
		}
	}
	cout<<ans;
	return 0;
}
2023/8/1 20:33
加载中...