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>
using namespace std;
const int maxN = 1e5 + 5;
int a[maxN], mn[maxN], depth[maxN], par[20][maxN], order[maxN], cntOrder = 0;
bool picked[maxN];
vector<int> adj[maxN];
void pre_dfs(int u, int pre) {
mn[u] = a[u];
for (auto v: adj[u]) {
if (v == pre) continue;
depth[v] = depth[u] + 1;
pre_dfs(v, u);
mn[u] = min(mn[u], mn[v]);
}
}
void dfs_order(int u, int pre) {
for (auto v: adj[u]) {
if (v == pre) continue;
dfs_order(v, u);
}
order[u] = ++cntOrder;
}
bool cmp (int u, int v) {
return mn[u] < mn[v];
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, q, root = -1;
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++) {
cin >> par[0][i];
if (par[0][i] == 0) root = i;
else adj[par[0][i]].push_back(i);
}
pre_dfs(root, root);
for (int i = 1; i <= n; i++)
sort(adj[i].begin(), adj[i].end(), cmp);
dfs_order(root, root);
for (int k = 1; k <= 16; k++)
for (int i = 1; i <= n; i++)
par[k][i] = par[k - 1][par[k - 1][i]];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
for (int i = 1; i <= n; i++)
pq.push({order[i], i});
while (q--) {
int type, x;
cin >> type >> x;
if (type == 1) {
int last;
while (x--) {
last = pq.top().second; pq.pop();
picked[last] = true;
}
cout << last << '\n';
}
else {
int u = x;
for (int k = 16; k >= 0; k--) {
int v = par[k][u];
if (picked[v]) u = v;
}
cout << depth[x] - depth[u] << '\n';
picked[u] = false;
pq.push({order[u], u});
}
}
return 0;
}
Compilation message (stderr)
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:70:29: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
70 | cout << last << '\n';
| ^~~~
# | 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... |