90pts 求条
查看原帖
90pts 求条
591869
piantouqu楼主2024/7/13 09:33
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue> 
#include<vector>
#include<map>
#define ll long long
#define inf 0x3f3f3f3f
#define lowbit(x) (x&(-x))
using namespace std;
inline ll read(){
	ll a=0;short f=1;char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		a=a*10+c-'0';
		c=getchar();
	}
	return a*f;
}
const int N=4e5+5;
int root,tot;
struct Node{
	int s[2],v,f,sz,cnt;
}t[N];

inline void up(int u){
	t[u].sz=t[t[u].s[0]].sz+t[t[u].s[1]].sz+t[u].cnt;
}
 
inline void rotate(int x){
	int y=t[x].f;
	int z=t[y].f;
	int k=(t[y].s[1]==x);
	t[z].s[t[z].s[1]==y]=x;
	t[x].f=z;
	t[y].s[k]=t[x].s[k^1];
	t[t[x].s[k^1]].f=y;
	t[x].s[k^1]=y; 
	t[y].f=x;
	up(y),up(x);
}

inline void splay(int x,int goal){
	while(t[x].f!=goal){
		int y=t[x].f;
		int z=t[y].f;
		if(z!=goal)
			(t[y].s[0]==x)^(t[z].s[0]==y)?rotate(x):rotate(y);
		rotate(x);
	}
	if(goal==0)
		root=x;
}

inline void insert(int x){
	int u=root,fa=0;
	while(u&&t[u].v!=x){
		fa=u;
		u=t[u].s[x>t[u].v];
	}
	if(u) t[u].cnt++;
	else{
		u=++tot;
		if(fa)
			t[fa].s[x>t[fa].v]=u;
		t[u].s[0]=t[u].s[1]=0;
		t[tot].f=fa;
		t[tot].v=x;
		t[tot].cnt=1;
		t[tot].sz=1;
	}
	splay(u,0);
}

inline void find(int x){
	int u=root;
	if(!u) return;
	while(t[u].s[x>t[u].v]&&x!=t[u].v)
		u=t[u].s[x>t[u].v];
	splay(u,0);
}
 
inline int Next(int x,int f){
	find(x);
	int u=root;
	if(t[u].v>x&&f) return u;
	if(t[u].v<x&&!f) return u;
	u=t[u].s[f];
	while(t[u].s[f^1]) u=t[u].s[f^1];
	return u;
}
 
inline void Delete(int x){
	int last=Next(x,0);
	int next=Next(x,1);
	splay(last,0);splay(next,last);
	int del=t[next].s[0];
	t[next].s[0]=0;
	up(last),up(next); 
}
 
inline int kth(int x){
	int u=root;
	while(1){
		int y=t[u].s[0];
		if(x>t[y].sz+t[u].cnt){
			x-=t[y].sz+t[u].cnt;
			u=t[u].s[1];
		}
		else
			if(t[y].sz>=x)
				u=y;
			else
				return t[u].v;
	}
}

int dt,ans,k;
int main(){
	int n=read(),minn=read();
	k=minn;
	insert(+214748364);
	insert(-214748364);
	while(n--){
		char op;int x;
		cin>>op>>x;
		if(op=='I') if(x>=minn) insert(x-dt);
		if(op=='A') k-=x,dt+=x;
		if(op=='S'){
			dt-=x;
			k+=x;
			int y;
			while(t[(y=Next(k,0))].v!=-214748364&&t[y].v<k)
				ans+=t[y].cnt,Delete(t[y].v);
		}
		if(op=='F'){
			x++;
			if(x>=t[root].sz) cout<<-1<<"\n";
			else cout<<kth(t[root].sz-x+1)+dt<<"\n";
		}
	}
	cout<<ans;
	return 0;
}
2024/7/13 09:33
加载中...