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 + 10;
int cont = 0;
int in[MAXN], out[MAXN], dad[MAXN];
int within[MAXN];
vector<int> adj[MAXN];
void dfs(int node) {
in[node] = ++cont;
within[cont] = node;
for (auto u : adj[node]) {
if (u != dad[node]) dfs(u);
}
out[node] = cont;
}
struct Node {
bool ocupado = 1;
int mil = 0x3f3f3f3f;
};
Node merge(Node a, Node b) {
Node ans;
ans.ocupado = (a.ocupado && b.ocupado);
ans.mil = min(a.mil, b.mil);
return ans;
}
Node seg[4*MAXN];
void build(int node, int l, int r) {
if (l == r) {
seg[node].ocupado = 0;
seg[node].mil = within[l];
return;
}
int m = (l + r)/2;
build(2*node, l, m);
build(2*node + 1, m + 1, r);
seg[node] = merge(seg[2*node], seg[2*node + 1]);
}
Node empt;
Node query(int node, int l, int r, int tl, int tr) {
if ((l > tr) || (tl > r)) return empt;
if ((tl <= l) && (r <= tr)) return seg[node];
int m = (l + r)/2;
return merge(query(2*node, l, m, tl, tr), query(2*node + 1, m + 1, r, tl, tr));
}
void update(int node, int l, int r, int po, bool v) {
if (l == r) {
seg[node].ocupado = v;
if (v) seg[node].mil = 0x3f3f3f3f;
else seg[node].mil = within[l];
return;
}
int m = (l + r)/2;
if (po <= m) {
update(2*node, l, m, po, v);
} else {
update(2*node + 1, m + 1, r, po, v);
}
seg[node] = merge(seg[2*node], seg[2*node + 1]);
}
int main() {
int n, q;
cin >> n >> q;
int root;
for (int i = 1; i <= n; i++) {
cin >> dad[i];
adj[dad[i]].push_back(i);
if (dad[i] == 0) root = i;
}
dfs(root);
build(1, 1, n);
while (q--) {
int t, x;
cin >> t >> x;
if (t == 2) {
update(1, 1, n, in[x], 0);
cout << "0\n";
continue;
}
auto k = query(1, 1, n, in[x], out[x]).mil;
cout << k << "\n";
update(1, 1, n, in[k], 1);
}
}
Compilation message (stderr)
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:80:8: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
80 | dfs(root);
| ~~~^~~~~~
# | 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... |