#include <bits/stdc++.h>
using namespace std;
vector<int> p, order, depth, pos;
vector< vector<int> > children, up;
vector<int> used;
int root, cnt, max_log;
pair<int, vector<int> > dfs(int u, int par, int d) {
int mn = u;
depth[u] = d;
vector< pair<int, vector<int> > > tmp;
up[u][0] = par;
for (int i = 1; i <= max_log; i++) {
up[u][i] = up[up[u][i - 1]][i - 1];
}
for (auto v : children[u]) {
tmp.push_back(dfs(v, u, d + 1));
mn = min(mn, tmp.back().first);
}
sort(tmp.begin(), tmp.end());
vector<int> ans;
for (auto& v : tmp) {
for (auto& x : v.second) {
ans.push_back(x);
}
}
ans.push_back(u);
return {mn, ans};
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
max_log = (int) ceil(log2((double) n));
up.assign(n + 1, vector<int>(max_log + 1));
p.resize(n + 1);
children.resize(n + 1);
for (int i = 1; i <= n; i++) {
cin >> p[i];
if (p[i] == 0) {
root = i;
} else {
children[p[i]].push_back(i);
}
}
depth.resize(n + 1);
pos.resize(n + 1);
order = dfs(root, root, 0).second;
for (int i = 0; i < n; i++) {
pos[order[i]] = i;
}
used.assign(n + 1, 0);
set<int> st;
for (int i = 1; i <= n; i++) {
st.insert(pos[i]);
}
while (q--) {
int type, k;
cin >> type >> k;
if (type == 1) {
int last;
set<int> newSt;
for (auto& x : st) {
if (k == 0) {
newSt.insert(x);
} else {
k--;
used[order[x]] = 1;
last = order[x];
}
}
st = newSt;
cout << last << '\n';
} else {
int oldK = k;
for (int i = max_log; i >= 0; i--) {
if (used[up[k][i]]) {
k = up[k][i];
}
}
used[k] = 0;
st.insert(k);
cout << depth[oldK] - depth[k] << '\n';
}
}
return 0;
}
Compilation message
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:76:23: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
cout << last << '\n';
^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Incorrect |
227 ms |
16276 KB |
Output isn't correct |
3 |
Execution timed out |
1085 ms |
16996 KB |
Time limit exceeded |
4 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
5 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Incorrect |
4 ms |
512 KB |
Output isn't correct |
8 |
Incorrect |
4 ms |
640 KB |
Output isn't correct |
9 |
Incorrect |
11 ms |
1280 KB |
Output isn't correct |
10 |
Incorrect |
37 ms |
4176 KB |
Output isn't correct |
11 |
Incorrect |
342 ms |
15696 KB |
Output isn't correct |
12 |
Execution timed out |
1063 ms |
16992 KB |
Time limit exceeded |
13 |
Execution timed out |
1070 ms |
17180 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1064 ms |
8348 KB |
Time limit exceeded |
2 |
Execution timed out |
1075 ms |
25588 KB |
Time limit exceeded |
3 |
Execution timed out |
1066 ms |
25672 KB |
Time limit exceeded |
4 |
Incorrect |
361 ms |
9692 KB |
Output isn't correct |
5 |
Incorrect |
845 ms |
10456 KB |
Output isn't correct |
6 |
Incorrect |
902 ms |
10684 KB |
Output isn't correct |
7 |
Incorrect |
398 ms |
8156 KB |
Output isn't correct |
8 |
Execution timed out |
1085 ms |
8092 KB |
Time limit exceeded |
9 |
Execution timed out |
1084 ms |
24956 KB |
Time limit exceeded |
10 |
Execution timed out |
1068 ms |
25644 KB |
Time limit exceeded |
11 |
Execution timed out |
1071 ms |
25836 KB |
Time limit exceeded |
12 |
Execution timed out |
1082 ms |
20792 KB |
Time limit exceeded |
13 |
Execution timed out |
1076 ms |
29652 KB |
Time limit exceeded |
14 |
Execution timed out |
1066 ms |
25544 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1068 ms |
15256 KB |
Time limit exceeded |
2 |
Execution timed out |
1045 ms |
21180 KB |
Time limit exceeded |
3 |
Execution timed out |
1057 ms |
27072 KB |
Time limit exceeded |
4 |
Execution timed out |
1070 ms |
20468 KB |
Time limit exceeded |
5 |
Execution timed out |
1074 ms |
20828 KB |
Time limit exceeded |
6 |
Execution timed out |
1076 ms |
20940 KB |
Time limit exceeded |
7 |
Execution timed out |
1068 ms |
17576 KB |
Time limit exceeded |
8 |
Execution timed out |
1070 ms |
28256 KB |
Time limit exceeded |
9 |
Execution timed out |
1077 ms |
25192 KB |
Time limit exceeded |
10 |
Execution timed out |
1070 ms |
25660 KB |
Time limit exceeded |
11 |
Execution timed out |
1071 ms |
25752 KB |
Time limit exceeded |
12 |
Execution timed out |
1080 ms |
21724 KB |
Time limit exceeded |
13 |
Execution timed out |
1072 ms |
33440 KB |
Time limit exceeded |
14 |
Correct |
344 ms |
21292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1053 ms |
25068 KB |
Time limit exceeded |
2 |
Execution timed out |
1068 ms |
22080 KB |
Time limit exceeded |
3 |
Execution timed out |
1066 ms |
33488 KB |
Time limit exceeded |
4 |
Execution timed out |
1073 ms |
25348 KB |
Time limit exceeded |
5 |
Execution timed out |
1069 ms |
25656 KB |
Time limit exceeded |
6 |
Execution timed out |
1050 ms |
25708 KB |
Time limit exceeded |
7 |
Execution timed out |
1063 ms |
21212 KB |
Time limit exceeded |
8 |
Execution timed out |
1075 ms |
33632 KB |
Time limit exceeded |
9 |
Execution timed out |
1078 ms |
25668 KB |
Time limit exceeded |