This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
#define debug(s) cerr << #s << " = " << s << '\n'
#define all(v) (v).begin(), (v).end()
#define mem(a,val) memset(a, val, sizeof a)
#define ff first
#define ss second
typedef long long ll;
typedef tree<pair<int, int>,null_type,less<pair<int, int>>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const int N = 100010;
int n, q, ptr, root;
int T[N], P[N][24], L[N], rev[N], val[N];
vector<int> adj[N];
ordered_set s;
void dfs(int from, int u, int dep) {
T[u] = from;
L[u] = dep;
for (auto v: adj[u]){
if (v == from) continue;
dfs(u, v, dep + 1);
}
rev[ptr] = u;
val[u] = ptr;
s.insert({ptr++, u});
}
void init() {
int i, j;
for (i = 1; i <= n; i++)
for (j = 0; 1 << j < n; j++)
P[i][j] = -1;
for (i = 1; i <= n; i++)
P[i][0] = T[i];
for (j = 1; 1 << j < n; j++)
for (i = 1; i <= n; i++)
if (P[i][j - 1] != -1)
P[i][j] = P[P[i][j - 1]][j - 1];
}
int klevpar(int u, int k) {
int log, i;
for (log = 1; 1 << log <= L[u]; log++);
log--;
for (i = log; i >= 0; i--)
if (L[u] - (1 << i) >= k)
u = P[u][i];
return u;
}
int main() {
scanf("%d %d", &n, &q);
for (int i = 1; i <= n; ++i) {
int x;
scanf("%d", &x);
if (x == 0) root = i;
adj[x].push_back(i);
}
for (int i = 0; i <= n; ++i) sort(all(adj[i]));
dfs(-1, root, 0);
init();
vector<pair<int, int>> er;
while (q--) {
int ty, v;
scanf("%d %d", &ty, &v);
if (ty == 1) {
int ret = (*s.find_by_order(v - 1)).ss;
printf("%d\n", ret);
er.clear();
for (int i = 0; i < v; ++i) er.push_back(*s.find_by_order(i));
for (auto p: er) s.erase(p);
}
else {
int ret = 0;
for (int lev = 0; lev < L[v]; ++lev) {
int u = klevpar(v, lev);
if (s.find({val[u], u}) == s.end()) {
s.insert({val[u], u});
ret = L[v] - lev;
break;
}
}
printf("%d\n", ret);
}
}
}
Compilation message (stderr)
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:62:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &q);
~~~~~^~~~~~~~~~~~~~~~~
ballmachine.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &x);
~~~~~^~~~~~~~~~
ballmachine.cpp:75:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &ty, &v);
~~~~~^~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |