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>
#define pb emplace_back
#define ll long long
#define fi first
#define se second
#define mp make_pair
//#define int int64_t
using namespace std;
const int N = (int)1e5 + 2;
const int logN = 20;
typedef pair<int, int> pii;
int p[N][logN + 1], del[N], d[N], deg[N];
int Min[N], order[N], cur = 0;
int root, n, q, x, k, low, high, mid;
vector<int> a[N];
set<pii> leaf;
void DFS(int u) {
deg[u] = a[u].size(), Min[u] = u;
for(int i = 1; i <= logN; ++i) p[u][i] = p[p[u][i - 1]][i - 1];
for(int v: a[u]) {
d[v] = d[u] + 1, DFS(v);
Min[u] = min(Min[u], Min[v]);
}
}
void Order(int u) {
vector<pii> child;
for(int v: a[u]) child.pb(Min[v], v);
sort(child.begin(), child.end());
for(pii p: child) Order(p.se);
order[u] = ++cur;
if(deg[u] == 0) leaf.insert(mp(order[u], u));
}
int Node(int x, int step) {
for(int i = logN; i >= 0; --i)
if(step >= (1 << i)) x = p[x][i], step -= (1 << i);
return x;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define Task "test"
if(fopen(Task".inp", "r")) {
freopen(Task".inp", "r", stdin);
freopen(Task".out", "w", stdout);
}
cin >> n >> q;
for(int i = 1; i <= n; ++i) {
cin >> p[i][0]; a[p[i][0]].pb(i);
if(!p[i][0]) root = i;
}
DFS(root); Order(root);
while(q --) {
cin >> x >> k;
if(x == 1) {
while(k --) {
x = (*leaf.begin()).se; leaf.erase(leaf.begin());
if(--deg[p[x][0]] == 0) leaf.insert(mp(order[p[x][0]], p[x][0]));
del[x] = 1;
}
cout << x << '\n';
} else {
x = k;
for(int i = logN; i >= 0; --i) {
if(del[p[x][i]]) x = p[x][i];
}
del[x] = 0, leaf.insert(mp(order[x], x));
if(++deg[p[x][0]] == 1) leaf.erase(mp(order[p[x][0]], p[x][0]));
cout << d[k] - d[x] << '\n';
}
}
}
Compilation message (stderr)
ballmachine.cpp: In function 'int32_t main()':
ballmachine.cpp:50:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(Task".inp", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:51:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(Task".out", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |