SplayWA40pts求调
查看原帖
SplayWA40pts求调
1163238
YWHHDJSer楼主2024/7/5 19:41

RT。

#include<bits/stdc++.h>
using namespace std;
#define il inline
#define ri register int
int a,mn,u,deta,lst,man;
char in;
struct Splay
{
	#define N 1000001
	int root,cnt,fa[N],val[N],siz[N],sons[N][2],num[N];
	#undef N
	il void pu(int x)
	{
		siz[x]=siz[sons[x][0]]+siz[sons[x][1]]+num[x];
	}
	il void esc(int x)
	{
		fa[x]=siz[x]=sons[x][0]=sons[x][1]=val[x]=num[x]=0;
	}
	il int faso(int x)
	{
		return sons[fa[x]][0]==x?0:1;
	}
	il void rota(int x)
	{
		int y=fa[x],z=fa[y],pl=faso(x);
		sons[y][pl]=sons[x][pl^1];
		if(sons[x][pl^1]!=0)
		{
			fa[sons[x][pl^1]]=y;
		}
		sons[x][pl^1]=y;
		fa[y]=x;
		fa[x]=z;
		if(z!=0)
		{
			pl=(y==sons[z][0]?0:1);
			sons[z][pl]=x;
		}
		pu(y);
		pu(x);
		return;
	}
	il void splay(int x,int y)
	{
		if(y==0)
		{
			root=x;
		}
		while(fa[x]!=y)
		{
			int u=fa[x],v=fa[u];
			if(v!=y)
			{
				int pl=(faso(x)==faso(u)?u:x);
				rota(pl);
			}
			rota(x);
		}
		return;
	}
	il void push(int x)
	{
		if(root==0)
		{
			cnt++;
			val[cnt]=x;
			num[cnt]++;
			root=cnt;
			pu(root);
			return;
		}
		int pl=root,y=0;
		while(1==1)
		{
			if(val[pl]==x)
			{
				num[pl]++;
				pu(pl);
				pu(y);
				splay(pl,0); 
				return;
			}
			y=pl;
			val[pl]<x?pl=sons[pl][1]:pl=sons[pl][0];
			if(pl==0)
			{
				cnt++;
				val[cnt]=x;
				num[cnt]++;
				fa[cnt]=y;
				val[y]<x?sons[y][1]=cnt:sons[y][0]=cnt;
				pu(cnt);
				pu(y);
				splay(cnt,0);
				return;
			}
		}
	}
	il int renk(int x)
	{
		int rn=0,pl=root;
		while(1==1)
		{
			if(x<val[pl])
			{
				pl=sons[pl][0];
				continue;
			}
			rn+=siz[sons[pl][0]];
			if(pl==0)
			{
				rn++;
				return rn;
			}
			if(x==val[pl])
			{
				splay(pl,0);
				rn++;
				return rn;
			}
			rn+=num[pl];
			pl=sons[pl][1];
		}
	}
	il void pop(int x)
	{
		int z=renk(x);
		if(num[root]>1)
		{
			num[root]--;
			pu(root);
			return;
		}
		if(sons[root][0]==0&&sons[root][1]==0)
		{
			esc(root);
			root=0;
			return;
		}
		int pl=root;
		if(sons[root][0]==0)
		{
			root=sons[root][1];
			fa[root]=0;
			esc(pl);
			return;
		}
		if(sons[root][1]==0)
		{
			root=sons[root][0];
			fa[root]=0;
			esc(pl);
			return;
		}
		int y;
		if(sons[root][0]==0)
		{
			y=root;
		}
		y=sons[root][0];
		while(sons[y][1]!=0)
		{
			y=sons[y][1];
		}
		splay(y,0);
		fa[sons[pl][1]]=y;
		sons[y][1]=sons[pl][1];
		esc(pl);
		pu(root);
	}
	il int thnk(int x)
	{
		int pl=root;
		while(1==1)
		{
			if(sons[pl][0]!=0&&x<=siz[sons[pl][0]])
			{
				pl=sons[pl][0];
				continue;
			}
			x-=siz[sons[pl][0]];
			if(x<=num[pl])
			{
				splay(pl,0);
				return pl;
			}
			x-=num[pl];
			pl=sons[pl][1];
		}
	}
	il int front(int x)
	{
		(*this).push(x);
		if(sons[root][0]==0)
		{
			return root;
		}
		int pl=sons[root][0],rn;
		while(sons[pl][1]!=0)
		{
			pl=sons[pl][1];
		}
		rn=val[pl];
		splay(pl,0);
		(*this).pop(x);
		return rn;
	}
	il int back(int x)
	{
		(*this).push(x);
		if(sons[root][1]==0)
		{
			return root;
		}
		int pl=sons[root][1],rn;
		while(sons[pl][0]!=0)
		{
			pl=sons[pl][0];
		}
		rn=val[pl];
		splay(pl,0);
		(*this).pop(x);
		return rn;
	}
	void out(int x)
	{
		if(val[x]==-0x3f3f3f3f||val[x]==0x3f3f3f3f)
		{
			return;
		}
		if(sons[x][0]!=0)
		{
			out(sons[x][0]);
		}
		if(sons[x][1]!=0)
		{
			out(sons[x][1]);
		}
		esc(x);
		return;
	}
	il void out_min(int x)
	{
		(*this).push(x);
		if(sons[root][0]==0)
		{
			return;
		}
		out(sons[root][0]);
		(*this).pop(x);
		if(root!=2)
		{
			fa[2]=root;
			sons[root][0]=2;
		}
		pu(root);
		return;
	}
}tree;
int main()
{
//	freopen("D.in","r",stdin);
	tree.push(0x3f3f3f3f);
	tree.push(-0x3f3f3f3f);
	man=2;
	scanf("%d%d",&a,&mn);
	while(a--)
	{
		scanf("\n%c%d",&in,&u);
//		printf("--------%d--------%d\n",tree.siz[tree.root],tree.cnt);
		if(in=='I')
		{
			if(u>=mn)
			{
				tree.push(u-deta);
				man++;
			}
			continue;
		}
		if(in=='A')
		{
			deta+=u;
			continue;
		}
		if(in=='S')
		{
			deta-=u;
			tree.out_min(mn-deta);
			continue;
		}
		if(in=='F')
		{
			if(u>tree.siz[tree.root]-2)
			{
				puts("-1");
			}
			else
			{
				int v=tree.siz[tree.root]-u;
				printf("%d\n",tree.val[tree.thnk(v)]+deta);
			}
			continue;
		}
	}
	printf("%d",man-tree.siz[tree.root]);
 	return 0;
}
2024/7/5 19:41
加载中...