-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path郁闷的出纳员.cpp
More file actions
103 lines (103 loc) · 2.27 KB
/
Copy path郁闷的出纳员.cpp
File metadata and controls
103 lines (103 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 100010, inf = 1e9;
typedef struct node
{
int s[2], p, v;
int size;
void init(int _v, int _p){
v = _v, p = _p;
size = 1;
}
}Node;
Node tr[maxn];
int n, m, delta;
int root, idx;
void pushup(Node &u, Node &l, Node &r)
{
u.size = l.size + r.size + 1;
}
void pushup(int u)
{
pushup(tr[u], tr[tr[u].s[0]], tr[tr[u].s[1]]);
}
void rotate(int x)
{
int y = tr[x].p, z = tr[y].p;
int k = tr[y].s[1] == x;
tr[z].s[tr[z].s[1] == y] = x, tr[x].p = z;
tr[y].s[k] = tr[x].s[k ^ 1], tr[tr[x].s[k ^ 1]].p = y;
tr[x].s[k ^ 1] = y, tr[y].p = x;
pushup(y), pushup(x);
}
void splay(int x, int k)
{
while(tr[x].p != k){
int y = tr[x].p, z = tr[y].p;
if(z != k){
if((tr[y].s[1] == x) ^ (tr[z].s[1] == x)) rotate(x);
else rotate(y);
}
rotate(x);
}
if(!k) root = x;
}
int insert(int v)
{
int u = root, p = 0;
while(u) p = u, u = tr[u].s[v > tr[u].v];
u = ++ idx;
if(p) tr[p].s[v > tr[p].v] = u;
tr[u].init(v, p);
splay(u, 0);
return u;
}
int get_k(int k)
{
int u = root;
while(true)
{
if(tr[tr[u].s[0]].size >= k) u = tr[u].s[0];
else if(tr[tr[u].s[0]].size + 1 == k) return tr[u].v;
else k -= tr[tr[u].s[0]].size + 1, u = tr[u].s[1];
}
return -1;
}
int get(int v) // 找到大于等于v的最小的节点
{
int u = root, res;
while(u){
if(tr[u].v >= v) res = u, u = tr[u].s[0];
else u = tr[u].s[1];
}
return res;
}
int main()
{
scanf("%d %d", &n, &m);
int L = insert(-inf), R = insert(inf);
int tot = 0;
while(n --)
{
char op[2];
int k;
scanf("%s %d", op, &k);
if(*op == 'I'){
if(k >= m) k -= delta, insert(k), tot ++;
}else if(*op == 'A'){
delta += k;
}else if(*op == 'S'){
delta -= k;
R = get(m - delta);
splay(R, 0), splay(L, R);
tr[L].s[1] = 0;
pushup(L), pushup(R);
}else{
if(tr[root].size - 2 < k) puts("-1");
else printf("%d\n", get_k(tr[root].size - k) + delta);
}
}
printf("%d\n", tot - (tr[root].size - 2));
return 0;
}