#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
pipes.cpp: In function 'int main()':
pipes.cpp:80:8: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
80 | dfs(root);
| ~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
10072 KB |
Execution killed with signal 11 |
2 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
3 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
4 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
5 |
Runtime error |
5 ms |
10168 KB |
Execution killed with signal 11 |
6 |
Runtime error |
5 ms |
10100 KB |
Execution killed with signal 11 |
7 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
8 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
9 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
10 |
Runtime error |
6 ms |
10076 KB |
Execution killed with signal 11 |
11 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
12 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
13 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
14 |
Runtime error |
4 ms |
10060 KB |
Execution killed with signal 11 |
15 |
Runtime error |
6 ms |
10332 KB |
Execution killed with signal 6 |
16 |
Runtime error |
4 ms |
10076 KB |
Execution killed with signal 11 |
17 |
Runtime error |
5 ms |
10272 KB |
Execution killed with signal 11 |
18 |
Runtime error |
5 ms |
10072 KB |
Execution killed with signal 11 |
19 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
20 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
21 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
22 |
Runtime error |
6 ms |
10076 KB |
Execution killed with signal 11 |
23 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
24 |
Runtime error |
5 ms |
10328 KB |
Execution killed with signal 11 |
25 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
10036 KB |
Execution killed with signal 11 |
2 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
3 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
4 |
Runtime error |
4 ms |
10076 KB |
Execution killed with signal 11 |
5 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
6 |
Runtime error |
4 ms |
10076 KB |
Execution killed with signal 11 |
7 |
Runtime error |
5 ms |
10260 KB |
Execution killed with signal 11 |
8 |
Runtime error |
5 ms |
10192 KB |
Execution killed with signal 11 |
9 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 6 |
10 |
Runtime error |
5 ms |
10112 KB |
Execution killed with signal 11 |
11 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
12 |
Runtime error |
8 ms |
10212 KB |
Execution killed with signal 11 |
13 |
Runtime error |
5 ms |
10048 KB |
Execution killed with signal 11 |
14 |
Runtime error |
6 ms |
10328 KB |
Execution killed with signal 11 |
15 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
16 |
Runtime error |
4 ms |
10216 KB |
Execution killed with signal 11 |
17 |
Runtime error |
4 ms |
10056 KB |
Execution killed with signal 11 |
18 |
Runtime error |
4 ms |
10064 KB |
Execution killed with signal 11 |
19 |
Runtime error |
6 ms |
10332 KB |
Execution killed with signal 6 |
20 |
Runtime error |
4 ms |
10076 KB |
Execution killed with signal 11 |
21 |
Runtime error |
5 ms |
10284 KB |
Execution killed with signal 11 |
22 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
23 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
24 |
Runtime error |
6 ms |
10076 KB |
Execution killed with signal 11 |
25 |
Runtime error |
7 ms |
10160 KB |
Execution killed with signal 11 |
26 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
27 |
Runtime error |
6 ms |
10332 KB |
Execution killed with signal 6 |
28 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
29 |
Runtime error |
5 ms |
10072 KB |
Execution killed with signal 11 |
30 |
Runtime error |
5 ms |
10072 KB |
Execution killed with signal 11 |
31 |
Runtime error |
5 ms |
10064 KB |
Execution killed with signal 11 |
32 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
33 |
Runtime error |
6 ms |
10332 KB |
Execution killed with signal 6 |
34 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
35 |
Runtime error |
5 ms |
10256 KB |
Execution killed with signal 11 |
36 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
37 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
38 |
Runtime error |
5 ms |
10072 KB |
Execution killed with signal 11 |
39 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
40 |
Runtime error |
6 ms |
10072 KB |
Execution killed with signal 11 |
41 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
42 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
43 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
44 |
Runtime error |
4 ms |
10076 KB |
Execution killed with signal 11 |
45 |
Runtime error |
8 ms |
10476 KB |
Execution killed with signal 11 |
46 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
47 |
Runtime error |
5 ms |
10160 KB |
Execution killed with signal 11 |
48 |
Runtime error |
5 ms |
10088 KB |
Execution killed with signal 11 |
49 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
50 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
51 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
52 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 6 |
53 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 11 |
54 |
Runtime error |
5 ms |
10076 KB |
Execution killed with signal 6 |