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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
| /**************************************************************
Problem: 1503
User: abcdabcd987
Language: C++
Result: Accepted
Time:856 ms
Memory:5496 kb
****************************************************************/
#include <cstdio>
#include <algorithm>
using namespace std;
class SplayTree
{
public:
static const int MAXLEN = 200001;
struct Node
{
int value, count, size;
Node *Parent, *Child[2];
Node(const int V = 0, Node* const P = NULL, Node* const L = NULL, Node* const R = NULL):
value(V), count(1), size(1), Parent(P) { Child[0] = L, Child[1] = R; }
};
Node *Root;
private:
int NodeSize;
Node h[MAXLEN];
void Update(Node *x)
{
x->size = x->count;
if (x->Child[0] != NULL)
x->size += x->Child[0]->size;
if (x->Child[1] != NULL)
x->size += x->Child[1]->size;
}
void Rotate(Node *x, const int c)
{
Node *y = x->Parent;
y->Child[c^1] = x->Child[c];
if (x->Child[c] != NULL)
x->Child[c]->Parent = y;
x->Parent = y->Parent;
if (y->Parent != NULL)
y->Parent->Child[y == y->Parent->Child[1]] = x;
y->Parent = x;
x->Child[c] = y;
Update(y), Update(x);
}
void Splay(Node *x, Node* const goal = NULL)
{
for (Node *y; (y = x->Parent) != goal; )
{
if (y->Parent == goal)
{
Rotate(x, x == y->Child[0]);
break;
}
const int c = y == y->Parent->Child[0];
if (x == y->Child[c])
Rotate(x, c^1), Rotate(x, c);
else
Rotate(y, c), Rotate(x, c);
}
if (goal == NULL)
Root = x;
}
public:
Node* Insert(const int x)
{
if (Root == NULL)
{
h[NodeSize++] = Node(x);
Root = h+NodeSize-1;
return Root;
}
Node *t = Root, *p;
while (t != NULL)
{
p = t;
if (x == t->value)
{
++t->size, ++t->count;
Splay(t);
return t;
}
t = (x < t->value ? t->Child[0] : t->Child[1]);
}
Node *node = h+NodeSize;
*node = Node(x, p);
(x < p->value ? p->Child[0] : p->Child[1]) = node;
Splay(node);
++NodeSize;
return node;
}
void Delete(const int x)
{
Insert(x);
Root = Root->Child[1];
if (Root)
Root->Parent = NULL;
}
Node* GetKth(int k)
{
k = Root->size-k+1;
Node* t = Root, *p;
while (t)
{
p = t;
int LeftSize = (t->Child[0] == NULL ? 0 : t->Child[0]->size);
if (LeftSize < k && k <= LeftSize+t->count)
break;
if (k < LeftSize+t->count)
t = t->Child[0];
else
{
k -= LeftSize+t->count;
t = t->Child[1];
}
}
Splay(p);
return p;
}
};
int n, m, num, tol, Delta;
SplayTree S;
int main()
{
scanf("%d%dn", &n, &m);
for (char o; n > 0; --n)
{
scanf("%c %dn", &o, &num);
switch (o)
{
case 'I':
if (num >= m)
{
++tol;
S.Insert(num-Delta);
}
break;
case 'A':
Delta += num;
break;
case 'S':
Delta -= num;
S.Delete(m-Delta-1);
break;
case 'F':
printf("%dn", (S.Root == NULL || num > S.Root->size ? -1 : S.GetKth(num)->value+Delta));
break;
}
}
printf("%d", tol-S.Root->size);
}
|