#include <bits/stdc++.h>
using namespace std;
int n,m,x,t,root,change,cnt;
priority_queue<int,vector<int>,greater<int>> q;
char op;
struct id
{
	int l,r,ix,num,d,siz;
}bst[100005];
int nw(int x)
{
	bst[++t].num = x;
	bst[t].d = rand();
	bst[t].siz = 1;
	return t;
}
void up(int p)
{
	bst[p].siz = bst[bst[p].l].siz + bst[bst[p].r].siz + 1;
}
void split(int p,int x,int &l,int &r)
{
	if (p == 0)
	{
		l = r = 0;
		return;
	}
	if (bst[p].num <= x)
	{
		l = p;
		split(bst[p].r,x,bst[p].r,r);
	}
	else
	{
		r = p;
		split(bst[p].l,x,l,bst[p].l);
	}
	up(p);
}
int merge(int l,int r)
{
	if (l == 0 || r == 0)
	{
		return l + r;
	}
	if (bst[l].d > bst[r].d)
	{
		bst[l].r = merge(bst[l].r,r);
		up(l);
		return l;
	}
	else
	{
		bst[r].l = merge(l,bst[r].l);
		up(r);
		return r;
	}
}
void add(int x)
{
	int l,r;
	split(root,x,l,r);
	int p = nw(x);
	int q = merge(l,p);
	root = merge(q,r);
}
void del(int x)
{
	int l,r,p;
	split(root,x,l,r);
	split(l,x - 1,l,p);
	p = merge(bst[p].l,bst[p].r);
	root = merge(merge(l,p),r);
}
int where(int p,int x)
{
	if (x == bst[bst[p].r].siz + 1)
	{
		return bst[p].num;
	}
	else if (x <= bst[bst[p].r].siz)
	{
		return where(bst[p].r,x);
	}
	else
	{
		return where(bst[p].l,x - bst[bst[p].r].siz - 1);
	}
}
void all(int p,int x)
{
	if (p == 0)
	{
		return;
	}
	if (bst[p].num >= m)
	{
		bst[p].num += x;
		if (bst[p].num < m)
		{
			cnt++;
		}
	}
	all(bst[p].l,x);
	all(bst[p].r,x);
}
void db(int p)
{
	if (p == 0)
	{
		return;
	}
	if (bst[p].num != 0)
	{
		cout << bst[p].num << " ";
	}
	db(bst[p].l);
	db(bst[p].r);
}
int main()
{
	cin >> n >> m;
	while (n--)
	{
		cin >> op >> x;
		if (op == 'I' && x >= m)
		{
			add(x);
//			db(root);
//			cout << "\n";
		}
		else if (op == 'A')
		{
			all(root,x);
//			db(root);
//			cout << "\n";
		}
		else if (op == 'S')
		{
			all(root,-x);
//			db(root);
//			cout << "\n";
		}
		else if (op == 'F')
		{
			if (t - cnt < x)
			{
				cout << -1 << '\n';
			}
			else
			{
				cout << where(root,x) << '\n';
			}
//			db(root);
//			cout << "\n";
		}
	}
	cout << cnt;
	return 0;
}

0 comments

No comments so far...

Information

ID
147
Time
1000ms
Memory
256MiB
Difficulty
9
Tags
# Submissions
28
Accepted
3
Uploaded By