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, qu, ptr, root;
int T[N], P[N][20], L[N], col[N], val[N];
vector<int> adj[N], g[N];
struct dat {
int va, node;
bool operator < (const dat &a) const {
return va > a.va;
}
};
priority_queue<dat> pq;
int dfs0(int from, int u) {
priority_queue<dat> q;
for (auto v: g[u]) {
if (v == from) continue;
q.push({dfs0(u, v), v});
}
int ret = u;
while (!q.empty()) {
adj[u].push_back(q.top().node);
ret = min(ret, q.top().va);
q.pop();
}
return ret;
}
void dfs(int from, int u) {
T[u] = from;
for (auto v: adj[u]) {
if (v == from) continue;
dfs(u, v);
}
val[u] = ptr;
pq.push({ptr++, u});
}
void init() {
int i, j;
for (i = 1; i <= n; i++)
P[i][0] = T[i];
for (j = 1; j < 18; j++)
for (i = 1; i <= n; i++)
P[i][j] = P[P[i][j - 1]][j - 1];
}
int main() {
scanf("%d %d", &n, &qu);
for (int i = 1; i <= n; ++i) {
int x;
scanf("%d", &x);
if (x == 0) root = i;
g[x].push_back(i);
}
dfs0(0, root);
dfs(0, root);
init();
while (qu--) {
int ty, v;
scanf("%d %d", &ty, &v);
if (ty == 1) {
int ret;
while (v--) {
int curnode = pq.top().node;
col[curnode] = 1;
ret = curnode;
pq.pop();
}
printf("%d\n", ret);
}
else {
int ret = 0;
for (int i = 16; i >= 0; --i) {
if (col[P[v][i]]) {
ret += (1 << i);
v = P[v][i];
}
}
col[v] = 0;
pq.push({val[v], v});
printf("%d\n", ret);
}
}
}
Compilation message (stderr)
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:70:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &qu);
~~~~~^~~~~~~~~~~~~~~~~~
ballmachine.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &x);
~~~~~^~~~~~~~~~
ballmachine.cpp:82: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... |