求助!
查看原帖
求助!
1135546
qwp____5_0_3楼主2025/1/27 10:02
#include<iostream>
#include<ctime>
#include<cstdlib>

#define ll long long
#define ls(p) tr[p].pl
#define rs(p) tr[p].pr
#define val(p) tr[p].val
#define rnd(p) tr[p].rnd
#define size(p) tr[p].size
#define push_up(p) size(p)=size(ls(p))+size(rs(p))+1 
#define N 2000010

struct Treap{
	ll pl,pr;
	ll val,rnd;
	ll size;
}; 
Treap tr[N];
 
ll n,m,root,a,cnt,op,x,last=0,ans=0;
inline ll read();
inline void newnode(ll val); 
inline void merge(ll &rt,ll x,ll y);
inline void split(ll rt,ll k,ll &x,ll &y);
inline void insert(ll rt,ll k);
inline void del(ll rt,ll k);
inline ll Ntr(ll rt,ll k);
inline ll Rtn(ll rt,ll k);
inline ll qq(ll rt,ll k);
inline ll hq(ll rt,ll k);

int main()
{
	srand(time(0));
	n=read(),m=read();
	for(int i=1;i<=n;i++)
	{
		a=read();
		insert(root,a);
	}
	for(int i=1;i<=m;i++)
	{
		op=read(),x=read();
		x^=last;
		if(op==1)insert(root,x);
		if(op==2)del(root,x);
		if(op==3)last=Ntr(root,x);
		if(op==4)last=Rtn(root,x);
		if(op==5)last=qq(root,x);
		if(op==6)last=hq(root,x);
		ans^=last;
	}
	std::cout<<ans<<"\n";
	return 0;
}
inline ll read()
{
	ll x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
	return x*f;
}
inline void newnode(ll val)
{
	cnt++;
	size(cnt)=1;
	val(cnt)=val;
	rnd(cnt)=rand();
}
inline void merge(ll &rt,ll x,ll y)
{
	if(rt==0)
	{
		x=0;
		y=0;
		return ;
	}
	if(rnd(x)<=rnd(y))
	{
		rt=x;
		merge(rs(rt),rs(x),y);
	}
	else
	{
		rt=y;
		merge(ls(rt),x,ls(y));
	}
	push_up(rt);
}
inline void split(ll rt,ll k,ll &x,ll &y)
{
	if(x==0||y==0)
	{
		rt=x+y;
		return ;
	}
	if(val(rt)<=k)
	{
		x=rt;
		split(rs(rt),k,rs(x),y);
	}
	else
	{
		y=rt;
		split(ls(rt),k,x,ls(y));
	}
	push_up(rt);
}
inline void insert(ll rt,ll k)
{
	ll px=0,py=0;
	newnode(k);
	split(rt,k-1,px,py); 
	merge(px,px,k);
	merge(rt,px,py);
}
inline void del(ll rt,ll k)
{
	ll px=0,py=0,pz=0;
	split(rt,k,px,py);
	split(px,k-1,px,pz);
	merge(pz,ls(pz),rs(pz));
	merge(px,px,pz);
	merge(rt,px,py);
}
inline ll Ntr(ll rt,ll k)
{
	ll px=0,py=0,jg=0;
	split(rt,k-1,px,py);
	jg=size(px)+1;
	merge(rt,px,py);
	return jg;
}
inline ll Rtn(ll rt,ll k)
{
	while(rt)
	{
		if(size(ls(rt))+1==k) return val(rt);
		if(size(ls(rt))>=k)	rt=ls(rt);
		else{
			k-=size(ls(rt))+1; 
			rt=rs(rt); 
		}
	}
}
inline ll qq(ll rt,ll k){return Rtn(rt,Ntr(rt,k-1));} 
inline ll hq(ll rt,ll k){return Rtn(rt,Ntr(rt,k+1));}
2025/1/27 10:02
加载中...