求助 P1486 郁闷的出纳员
查看原帖
求助 P1486 郁闷的出纳员
444236
Lesiris楼主2023/9/9 22:43
#include<bits/stdc++.h>
using namespace std;
const long long N=300010;
long long n,nn,nnnnn,f,k,rt,tot,fa[N],ch[N][2],val[N],cnt[N],sz[N],x,minn,ans,hhh[N],hh;
char cch;
inline long long read()
{
    long long x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
	{
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
	{
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
struct node 
{
	long long fa;
	long long ch[2];
	long long val;
	long long cnt;
	long long sz;
}s[N];
inline void maintain(long long x) 
{
	s[x].sz = s[s[x].ch[0]].sz + s[s[x].ch[1]].sz +s[x].cnt;
}
inline bool get(long long x) 
{
	return x == s[s[x].fa].ch[1];
}
inline void Clear(long long x) 
{
	s[x].ch[0] = s[x].ch[1] = s[x].fa = s[x].val = s[x].sz= s[x].cnt = 0;
}
inline void rotate(long long x) 
{
	long long y = s[x].fa, z = s[y].fa, chk = get(x);
	s[y].ch[chk] = s[x].ch[chk ^ 1];
	s[s[x].ch[chk ^ 1]].fa = y;
	s[x].ch[chk ^ 1] = y;
	s[y].fa = x;
	s[x].fa = z;
	if(z) s[z].ch[y == s[z].ch[1]] = x;
	maintain(y);
	maintain(x);
}
inline void splay(long long x) 
{
	for(long long f = s[x].fa; f; rotate(x), f = s[x].fa)
	{
		if(s[f].fa) rotate(get(x) == get(f) ? f : x);
	}
	rt = x;
}
inline void ins(long long k) 
{
	if(k<minn) return;
	if(!rt) 
	{
		s[++tot].val=k;
		s[tot].cnt++;
		rt=tot;
		maintain(rt);
		return;
}
	long long now=rt,f=0;
	while(true) 
	{
		if(s[now].val==k) 
		{
			s[now].cnt++;
			maintain(now);
			maintain(f);
			splay(now);
			break;
		}
		f=now;
		now=s[now].ch[s[now].val<k];
		if(!now) 
		{
			s[++tot].val=k;
			s[tot].cnt++;
			s[tot].fa=f;
			s[f].ch[s[f].val<k]=tot;
			maintain(tot);
			maintain(f);
			splay(tot);
			break;
		}
	}
}
inline long long find(long long k) 
{
	long long res = 0, now = rt;
	while(true) 
	{
		if(k<s[now].val) 
		{
			now = s[now].ch[0];
		}
		else 
		{
			res += s[s[now].ch[0]].sz;
			if(k == s[now].val)
			{
				splay(now);
				return res + 1;
			}
			res += s[now].cnt;
			now = s[now].ch[1];
		}
	}
}
inline long long getpre() 
{
	long long now = s[rt].ch[0];
	while (s[now].ch[1]) now = s[now].ch[1];
	return now;
}
inline long long getnxt() 
{
	long long now = s[rt].ch[1];
	while (s[now].ch[0]) now = s[now].ch[0];
	return now;
}
inline void del(long long k)
{
	find(k);
	if(s[rt].cnt > 1) 
	{
		s[rt].cnt--;
		maintain(rt);
		return;
	}
	if(!s[rt].ch[0] && !s[rt].ch[1])
	{
		Clear(rt);
		rt = 0;
		return;
	}
	if(!s[rt].ch[0])
	{
		long long tmp = rt;
		rt = s[rt].ch[1];
		s[rt].fa=0;
		Clear(tmp);
		return;
	}
	if(!s[rt].ch[1])
	{
		long long tmp = rt;
		rt = s[rt].ch[0];
		s[rt].fa = 0;
		Clear(tmp);
		return;
	}
	long long x = getpre(), now = rt;
	splay(x);
	s[s[now].ch[1]].fa = x;
	s[x].ch[1] = s[now].ch[1];
	Clear(now);
	maintain(rt);
}
inline long long getKth(long long k) 
{
	long long now = rt;
	while(true)
	{
		if(s[now].ch[0] && k <= s[s[now].ch[0]].sz)
		{
			now = s[now].ch[0];
		}
		else
		{
			k -= s[now].cnt + s[s[now].ch[0]].sz;
			if(k <= 0)
			{
				splay(now);
				return s[now].val;
			}
			now = s[now].ch[1];
		}
	}
}
void add(long long x) 
{
	for(long long i = 1 ; i <= tot ; i ++) 
	{
		s[i].val += x;
	}
}
void sc(long long x) 
{
	hh=0;
	for(long long i = 1 ; i <= tot ; i ++) 
	{
		//s[i].val -= x;
		if(s[i].val<minn)
		{
			hh++;
			hhh[hh]=i;
		//	cout<<i<<"    "<<s[i].val<<endl<<endl;
		//	ans++;
		//	tot--;
			nn--;
			
			//i++;
		}
	}
	for(long long i=1;i<=hh;i++)
	{
		Clear(s[hhh[i]].val);
	//	del(hhh[i]);
		ans++;
	}
}
void add2(long long x) 
{
	for(long long i = 1 ; i <= tot ; i ++) 
	{
		s[i].val -= x;
	}
}
int main()
{
	//ins(1e10);
	n=read(),minn=read();
	long long nnn=n;
	for(long long i=1;i<=nnn;i++)
	{
		cin>>cch;//cch=getchar();
		x=read();
	/*	for(int i=1;i<=tot;i++)
		{
			cout<<s[i].val<<"  ";
		}
		cout<<endl;*/
		if(cch=='I')
		{
			if(x>=minn)
			{
				ins(x);	
				nn++;
				nnnnn++;
				//n=nn;
			}
		}
		if(cch=='A')
		{
			add(x);
		//	sc(x);
		}
		if(cch=='S')
		{
			add2(x);
			sc(x);
		}
		if(cch=='F')
		{
			long long xxx=getKth(nnnnn-x+1);
		//	cout<<x<<" "<<n-xxx;
			if(x>nn) xxx=-1;
			printf("%lld\n",xxx);
		}
	}
	printf("%lld\n",ans);
	return 0;
}
2023/9/9 22:43
加载中...