求助(第八个和第九个点TLE)
查看原帖
求助(第八个和第九个点TLE)
428449
Amon_Xolotl楼主2023/2/28 16:21
#include<bits/stdc++.h>
using namespace std;
const int inf=1e9,M=50000000+10,l=1e7+5;
int n,rt,tot;
struct node
{
	int s[2],sz,fa,tag,val,cnt;
};
node tr[M];
void pushup(int x)
{
	tr[x].sz=tr[tr[x].s[0]].sz+tr[tr[x].s[1]].sz+tr[x].cnt;
}
void rotate(int x)
{
	int y=tr[x].fa,z=tr[y].fa;
	int k=x==tr[y].s[1];
	tr[z].s[y==tr[z].s[1]]=x;
	tr[x].fa=z;
	tr[tr[x].s[k^1]].fa=y;
	tr[y].s[k]=tr[x].s[k^1];
	tr[x].s[k^1]=y;
	tr[y].fa=x;
	pushup(y);
	pushup(x);
}
void splay(int x,int k)
{
	while(tr[x].fa!=k)
	{
		int y=tr[x].fa;
		if(tr[y].fa!=k)
		{
			rotate((x==tr[y].s[1])==(y==tr[tr[y].fa].s[1])?y:x);
		}
		rotate(x);
	}
	if(!k)
	{
		rt=x;
	}
}
void insert(int x)
{
	if(!rt)
	{
		tr[x].cnt++;
		tr[x].fa=0;
		tr[0].s[0]=x;
		rt=x;
		tr[x].tag=1;
		pushup(x);
		
		return;
	}
	int now=rt,f=0;
	while(1)
	{
		if(now==x)
		{
			tr[now].cnt++;
			pushup(now);
			pushup(f);
			splay(x,0);
			break;
		}
		f=now,now=tr[f].s[f<x];
		if(!now)
		{
			tr[x].fa=f;
			tr[x].tag=1;
			tr[x].cnt++;
			tr[f].s[f<x]=x;
			pushup(x);
			pushup(f);
			splay(x,0);
			
			break;
		}
	}
}
int tri(int x)
{
	int now=rt,f=0;
	int ans=0;
	while(1)
	{
		if(now==x)
		{
			splay(x,0);
			return tr[tr[now].s[0]].sz+1;
		}
		f=now;
		now=tr[f].s[f<x];
	}
}
int find(int x,int now)
{
	int lsz=tr[tr[now].s[0]].sz+tr[now].cnt;
	int lsy=tr[tr[now].s[0]].sz+1;
	if(x<lsy)
    {
    	return find(x,tr[now].s[0]);
	}
	if(x>=lsy&&x<=lsz)
	{
		splay(now,0);
		return now;
	}
	if(x>lsz)
	{
		return find(x-lsz,tr[now].s[1]);
	}
}
int linx(int x)
{
    while(tr[tr[x].s[1]].tag)
    {
    	x=tr[x].s[1];
	}
	splay(x,0);
	return x;
}
int link(int x)
{
    while(tr[tr[x].s[0]].tag)
    {
    	x=tr[x].s[0];
	}
	splay(x,0);
	return x;
}
void del(int x)
{
	splay(x,0);
	if(tr[rt].cnt>1)
	{
		tr[rt].cnt--;
		pushup(rt);
		return;
	}
	if(!tr[tr[rt].s[0]].tag&&!tr[tr[rt].s[1]].tag)
	{
		rt=0;
	}
	if(!tr[tr[rt].s[0]].tag)
	{
		int now=rt;
		rt=tr[rt].s[1];
		tr[rt].fa=0;
		tr[now].s[1]=tr[now].s[0]=tr[now].sz=tr[now].fa=tr[now].cnt=tr[now].tag=0;
		return;
	}
	if(!tr[tr[rt].s[1]].tag)
	{
		int now=rt;
		rt=tr[rt].s[0];
		tr[rt].fa=0;
		tr[now].s[1]=tr[now].s[0]=tr[now].sz=tr[now].fa=tr[now].cnt=tr[now].tag=0;
		return;
	}
	int now=rt;
	x=linx(tr[x].s[0]);
	tr[tr[now].s[1]].fa=rt;
	tr[rt].s[1]=tr[now].s[1];
	tr[now].s[1]=tr[now].s[0]=tr[now].sz=tr[now].fa=tr[now].cnt=tr[now].tag=0;
	pushup(rt);
}
int main()
{
//	freopen("6.in","r",stdin);
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int opt,x;
		cin>>opt>>x;
		if(opt==1)
		{
			insert(x+l);
		}
		if(opt==2)
		{
			del(x+l);
		}
		if(opt==3)
		{
			cout<<tri(x+l)<<endl;
		}
		if(opt==4)
		{
			cout<<find(x,rt)-l<<endl;
		}
		if(opt==5)
		{
			insert(x+l);
			cout<<linx(tr[x+l].s[0])-l<<endl;
			del(x+l);
		}
		if(opt==6)
		{
			insert(x+l);
			cout<<link(tr[x+l].s[1])-l<<endl;
			del(x+l);
		}
	}
	return 0;
}
2023/2/28 16:21
加载中...