forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ.cpp
More file actions
226 lines (192 loc) · 5.41 KB
/
J.cpp
File metadata and controls
226 lines (192 loc) · 5.41 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define SZ(X) ((int) ((X).size()))
using namespace std;
struct Node {
Node *child[2], *father;
int size, key;
bool rev;
} *root, *nil;
int initTree() { // Must be called at the start of program
nil = new Node;
nil->child[0] = nil->child[1] = nil->father = nil;
nil->size = nil->key = 0;
return 0;
}
int ensureInit = initTree();
// For updating node's value based on children (sum, min, max...)
void update(Node *x) {
if (x == nil) return ;
x->size = x->child[0]->size + x->child[1]->size + 1;
}
// For pushing down lazy update values
void refine(Node *x) {
if (x == nil) return ;
if (x->rev) {
x->child[0]->rev = !x->child[0]->rev;
x->child[1]->rev = !x->child[1]->rev;
swap(x->child[0], x->child[1]);
x->rev = false;
}
}
void print(Node *x) {
if (x == nil) return;
refine(x);
print(x->child[0]);
cout << x->key << ' ';
print(x->child[1]);
}
Node *createNode(int key) {
Node * tmp = new Node();
tmp->child[0] = tmp->child[1] = tmp->father = nil;
tmp->key = key;
tmp->size = 1;
return tmp;
}
void setChild(Node *x, Node *y, int d) {
x->child[d] = y;
y->father = x;
}
int getDirection(Node *x, Node *y) {
return x->child[0] == y ? 0 : 1;
}
void rotate(Node *x, int d) {
Node *y = x->father;
Node *z = x->child[d];
setChild(x, z->child[d ^ 1], d);
setChild(y, z, getDirection(y, x));
setChild(z, x, d ^ 1);
update(x);
update(z);
}
Node *splay(Node *x) {
if (x == nil) return nil;
while (x->father != nil) {
Node *y = x->father;
Node *z = y->father;
int dy = getDirection(y, x);
int dz = getDirection(z, y);
if (z == nil) {
rotate(y, dy);
} else if (dy == dz) {
rotate(z, dz);
rotate(y, dy);
} else {
rotate(y, dy);
rotate(z, dz);
}
}
return x;
}
Node *access2(Node *p, int k) {
refine(p);
if (k <= p->child[0]->size) return access2(p->child[0], k);
k -= p->child[0]->size + 1;
if (!k) return p;
return access2(p->child[1], k);
}
Node *access(Node * &root, int k) {
Node *res = access2(root, k);
splay(res);
root = res;
return root;
}
Node *join(Node *x, Node *y) {
if (x == nil) return y;
while (1) {
refine(x);
if (x->child[1] == nil) break;
x = x->child[1];
}
splay(x);
setChild(x, y, 1);
update(x);
return x;
}
// Note: If cut by position, really careful to return nil if empty
void cut(Node *x, Node * &t1, Node * &t2) {
t1 = splay(x);
t2 = t1->child[1];
t2->father = t1->child[1] = nil;
update(t1);
}
// t1 = (1 --> k); t2 = (k+1 --> size)
void cut(Node * &root, Node * &t1, Node * &t2, int k) {
if (k == 0) {
t1 = nil;
t2 = root;
} else if (k == root->size) {
t1 = root;
t2 = nil;
} else {
Node *x = access(root, k);
cut(x, t1, t2);
}
}
Node *create(int from, int to) {
if (from > to) return nil;
int mid = (from + to) >> 1;
Node *p = createNode(mid);
Node *left = create(from, mid - 1);
Node *right = create(mid + 1, to);
if (left != nil) setChild(p, left, 0);
if (right != nil) setChild(p, right, 1);
update(p);
return p;
}
const int MN = 100111;
Node* a[MN];
int main() {
initTree();
int q; scanf("%d", &q);
int entered = 0;
root = nil;
while (q--) {
char c = '#';
while (c < 'A' || c > 'Z') scanf("%c", &c);
if (c == 'F') {
++entered;
a[entered] = createNode(entered);
root = join(a[entered], root);
}
else if (c == 'B') {
++entered;
a[entered] = createNode(entered);
root = join(root, a[entered]);
}
else if (c == 'O') {
int u; scanf("%d", &u);
Node* cur = a[u];
splay(cur);
int left = cur->child[0]->size;
int right = cur->child[1]->size;
if (left <= right) {
Node* moved = cur->child[0];
printf("%d\n", moved->size);
moved->father = nil;
Node* other = cur->child[1];
other->father = nil;
root = join(other, moved);
}
else {
Node* moved = cur->child[1];
printf("%d\n", moved->size);
moved->father = nil;
Node* other = cur->child[0];
other->father = nil;
root = join(moved, other);
}
}
// cout << c << endl;
// print(root);
// cout << endl;
}
}