splay 32分求助
查看原帖
splay 32分求助
271556
No21楼主2023/9/7 19:38
#include<bits/stdc++.h>
using namespace std;
namespace TO
{
	const int N=2e6+21;
	struct node
	{
		int fa,size,cnt,val,ch[2];
	}t[N];
	int root,tot,n,m,last,ans;
	void update(int x)
	{
		t[x].size=t[t[x].ch[0]].size+t[t[x].ch[1]].size+t[x].cnt;
	}
	void rotate(int x)
	{
		int f=t[x].fa;
		int g=t[f].fa;
		bool son=(x==t[f].ch[1]);
		t[x].fa=g;
		t[g].ch[(f==t[g].ch[1])]=x;
		t[f].ch[son]=t[x].ch[son^1];
		t[t[x].ch[son^1]].fa=f;
		t[x].ch[son^1]=f;
		t[f].fa=x;
		update(f);
		update(x);
	}
	void splay(int x,int goal)
	{
		while(t[x].fa!=goal)
		{
			int f=t[x].fa;
			int g=t[f].fa;
			if(g!=goal)
				(x==t[f].ch[0])^(f==t[g].ch[0]) ? rotate(x) : rotate(f);
			rotate(x);
		}
		if(goal==0)
			root=x;
	}
	void find(int x)
	{
		int u=root;
		if(!u)
			return ;
		while(t[u].val!=x&&t[u].ch[t[u].val<x])
			u=t[u].ch[t[u].val<x];
		splay(u,0);
	}
	void insert(int x)
	{
		int f=0,u=root;
		while(u&&t[u].val!=x)
		{
			f=u;
			u=t[u].ch[x>t[u].val];
		}
		if(u)
			t[u].cnt++;
		else
		{
			u=++tot;
			if(f)
				t[f].ch[x>t[f].val]=u;
			t[u].ch[0]=t[u].ch[1]=0;
			t[u].fa=f;
			t[u].cnt=1;
			t[u].size=1;
			t[u].val=x;
		}
		splay(u,0);
	}
	int NEXT(int x, int p)
	{
		find(x);
		int u=root;
		if(t[u].val>x&&p)
        	return u;
    	if(t[u].val<x&&!p)
        	return u;
		u=t[u].ch[p];
		while(t[u].ch[p^1])
			u=t[u].ch[p^1];
		return u;
	}
	void Delete(int x)
	{
		int g=NEXT(x,0);
		int f=NEXT(x,1);
		splay(g,0);
		splay(f,g);
		int d=t[f].ch[0];
		if(t[d].cnt>1)
		{
			t[d].cnt--;
			splay(d,0);
		}
		else
			t[f].ch[0]=0;
	}
	int kth(int x)
	{
		int u=root;
		if(t[u].size<x)
			return 0;
		for(;;)
		{
			if(x>t[t[u].ch[0]].size+t[u].cnt)
			{
				x-=t[t[u].ch[0]].size+t[u].cnt;
				u=t[u].ch[1];
			}
			else if(x<=t[t[u].ch[0]].size)
				u=t[u].ch[0];
			else
				return t[u].val;
		}
	}
	int mian()
	{
        int uuu;
		cin>>n>>m;
        insert(2e9);
		insert(-2e9);
        for(int i=1;i<=n;i++)
        {
            cin>>uuu;
            insert(uuu);
        }
		int opt,x;
		while(m--)
		{
			cin>>opt>>x;
			x^=last;
			if(opt==1)
				insert(x);
			if(opt==2)
				Delete(x);
			if(opt==3)
			{
				find(x);
                last=t[t[root].ch[0]].size+1;
                ans^=last;
			}
			if(opt==4)
            {
				last=kth(x+1);
                ans^=last;
            }
			if(opt==5)
            {
				last=t[NEXT(x,0)].val;
                ans^=last;
            }
			if(opt==6)
            {
				last=t[NEXT(x,1)].val;
                ans^=last;
            }
		}
        cout<<ans;
		return 0;
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	return TO::mian();
}
2023/9/7 19:38
加载中...