20分求助P1486
查看原帖
20分求助P1486
449548
liuyuannb楼主2022/7/19 14:34
#include<bits/stdc++.h>
using namespace std;
struct node{
	int l,r,ans,key,siz;
}t[1000001];
int root,cnt=0;
int xj(int x)
{
	t[++cnt].ans=x;
	t[cnt].siz=1;
	t[cnt].key=rand();
	return cnt;
}
void gx(int x)
{
	t[x].siz=t[t[x].l].siz+t[t[x].r].siz+1;
}
void fl(int now,int ans,int &x,int &y)
{
	if(!now)
	{
		x=y=0;
		return ;
	}
	if(t[now].ans<=ans)
	{
		x=now;
		fl(t[now].r,ans,t[now].r,y);
	}
	else
	{
		y=now;
		fl(t[now].l,ans,x,t[now].l);
	}
	gx(now);
}
int hb(int x,int y)
{
	if(!x||!y)
	  return x+y;
	if(t[x].key<t[y].key)
	{
		t[x].r=hb(t[x].r,y);
		gx(x);
		return x;
	}
	else
	{
		t[y].l=hb(x,t[y].l);
		gx(y);
		return y;
	}
}
int x,y,z,mi,sum=0;
void cr(int k)
{
	if(k>=mi)
	{
		fl(root,k,x,y);
		root=hb(hb(x,xj(k)),y);
	}
}
void sc(int now)
{
	sum++;
	fl(root,t[now].ans,x,z);
	fl(x,t[now].ans-1,x,y);
	y=hb(t[y].l,t[y].r);
	root=hb(hb(x,y),z);
}
void jc(int now)
{
	if(t[now].l)
	  jc(t[now].l);
	if(t[now].r)
	  jc(t[now].r);
	if(t[now].ans<mi)
	  sc(now);
}
void xg(int k,int now)
{
	t[now].ans+=k;
	if(t[now].l)
	  xg(k,t[now].l);
	if(t[now].r)
	  xg(k,t[now].r);
}
void cx(int k)
{
	int now=root;
	if(t[root].siz<k)
	{
		printf("-1\n");
		return ;
	}
	while(now)
	{
		if(t[t[now].r].siz+1==k)
		  break;
		else if(t[t[now].r].siz+1>k)
		  now=t[now].r;
		else
		{
			k-=t[t[now].r].siz+1;
			now=t[now].l;
		}
	}
	printf("%d\n",t[now].ans);
}
int main()
{
	int n;
	scanf("%d%d",&n,&mi);
	while(n)
	{
		n--;
		int k;
		char c;
		cin>>c>>k;
		if(c=='I')
		  cr(k);
		if(c=='A')
		  xg(k,root);
		if(c=='S')
		{
			xg(-k,root);
			jc(root);
		}
		if(c=='F')
		  cx(k);
	}
	printf("%d\n",sum);
	return 0;
}

P1486 [NOI2004] 郁闷的出纳员

2022/7/19 14:34
加载中...