-
Notifications
You must be signed in to change notification settings - Fork 0
/
888.cpp
65 lines (65 loc) · 1.46 KB
/
888.cpp
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
#include <iostream>
#include <cmath>
#include <vector>
// Problem: https://oj.nctu.edu.tw/problems/888/
int main(int argc, char const *argv[]) {
int T = 0;
scanf("%d", &T);
while (T--) {
int n = 0, q = 0;
scanf("%d%d", &n, &q);
int level = ceil(log(n) / log(2)) + 1;
std::vector<int> v[level];
for (int li = 0; li < level; li++) {
if (!li) {
for (int ni = 0; ni < n; ni++) { v[li].push_back(1); }
}
else {
for(int ni = 0; ni < v[li - 1].size() / 2; ni++) {
v[li].push_back(v[li - 1][ni * 2] + v[li - 1][ni * 2 + 1]);
}
if (v[li - 1].size() & 1) {
v[li].push_back(v[li - 1][v[li - 1].size() - 1]);
}
}
}
while (q--) {
int t = 0, x = 0;
scanf("%d%d", &t, &x);
if (t == 1) {
x--;
if (!v[0][x]) { continue; }
for (int li = 0; li < level; li++) {
v[li][x]++;
x /= 2;
}
}
else if (t == 2) {
x--;
if (!v[0][x]) { continue; }
int div = v[0][x] - 1;
for(int li = 0;li < level;li++){
v[li][x] -= div;
x /= 2;
}
}
else {
if (v[level - 1][0] < x) { printf("endro~!\n"); }
else {
int index = 0;
for (int li = level - 1; li > 0 ;li--) {
v[li][index]--;
if (x > v[li - 1][2 * index]) {
x -= v[li - 1][2 * index];
index = 2 * index + 1;
}
else { index = 2 * index; }
}
v[0][index]--;
printf("%d\n", index + 1);
}
}
}
}
return 0;
}