#include <bits/stdc++.h>
using namespace std;
struct node {
int mn, mni, cnt;
};
const int MAXN=1e5+5;
vector<int> g[MAXN], ord;
priority_queue<int> pq;
node seg[MAXN*4];
int indt[MAXN], prof[MAXN];
int n, q;
void dfs(int cur) {
for(auto viz : g[cur]) {
prof[viz]=prof[cur]+1;
dfs(viz);
}
ord.push_back(cur);
indt[cur]=ord.size()-1;
}
int query(int sind, int ini, int fim, int qini, int qfim) {
if(qini>fim||qfim<ini) return 0;
if(qini<=ini&&qfim>=fim) return seg[sind].mni;
int m=(ini+fim)/2; int e=sind*2; int d=e+1;
int aa=query(e, ini, m, qini, qfim);
int bb=query(d, m+1, fim, qini, qfim);
if(prof[ord[aa]]<=prof[ord[bb]]) return aa;
else return bb;
}
void update(int sind, int ini, int fim, int qind, int k) {
if(qind<ini||qind>fim) return;
if(ini==fim) {
if(k==1) seg[sind].mn=prof[ord[ini]], seg[sind].cnt=0;
else seg[sind].mn=MAXN, seg[sind].cnt=1;
return;
}
int m=(ini+fim)/2; int e=sind*2; int d=e+1;
update(e, ini, m, qind, k); update(d, m+1, fim, qind, k);
seg[sind].mn=min(seg[e].mn, seg[d].mn);
if(seg[sind].mn==seg[e].mn) seg[sind].mni=seg[e].mni;
else seg[sind].mni=seg[d].mni;
seg[sind].cnt=seg[e].cnt+seg[d].cnt;
}
void build(int sind, int ini, int fim) {
if(ini==fim) {
seg[sind].mn=MAXN;
seg[sind].mni=ini;
seg[sind].cnt=1;
return;
}
int m=(ini+fim)/2; int e=sind*2; int d=e+1;
build(e, ini, m); build(d, m+1, fim);
seg[sind].mn=MAXN; seg[sind].mni=0;
seg[sind].cnt=seg[e].cnt+seg[d].cnt;
}
int main() {
scanf("%d %d", &n, &q);
for(int i=1; i<=n; i++) {
int a; scanf("%d", &a);
g[a].push_back(i);
}
ord.push_back(0); prof[1]=1; dfs(1); prof[0]=MAXN;
for(int i=1; i<=n; i++) pq.push(-i);
build(1, 1, n);
while(q--) {
int a, val; scanf("%d %d", &a, &val);
if(a==1) {
int ind;
for(int i=1; i<=val; i++) {
ind=-pq.top();
update(1, 1, n, ind, 1);
pq.pop();
// printf("%d > %d // %d\n", i, ind, ord[query(1, 1, n, 1, n)]);
}
printf("%d\n", ord[ind]);
}
else {
int cara=query(1, 1, n, indt[val], n);
printf("%d\n", prof[val]-prof[ord[cara]]);
update(1, 1, n, cara, -1);
pq.push(-cara);
// printf("qu %d %d > %d\n", indt[val], n, ord[cara]);
}
}
}
Compilation message
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &q);
~~~~~^~~~~~~~~~~~~~~~~
ballmachine.cpp:67:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int a; scanf("%d", &a);
~~~~~^~~~~~~~~~
ballmachine.cpp:77:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int a, val; scanf("%d %d", &a, &val);
~~~~~^~~~~~~~~~~~~~~~~~~
ballmachine.cpp:86:26: warning: 'ind' may be used uninitialized in this function [-Wmaybe-uninitialized]
printf("%d\n", ord[ind]);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
2688 KB |
Output isn't correct |
2 |
Incorrect |
108 ms |
5892 KB |
Output isn't correct |
3 |
Correct |
85 ms |
6136 KB |
Output is correct |
4 |
Runtime error |
7 ms |
5120 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Incorrect |
6 ms |
2688 KB |
Output isn't correct |
6 |
Runtime error |
8 ms |
5248 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Runtime error |
8 ms |
5248 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Runtime error |
9 ms |
5376 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
9 |
Runtime error |
9 ms |
5632 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
10 |
Runtime error |
19 ms |
6756 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
11 |
Runtime error |
36 ms |
11256 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Correct |
75 ms |
6108 KB |
Output is correct |
13 |
Runtime error |
45 ms |
11000 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
5552 KB |
Output is correct |
2 |
Incorrect |
243 ms |
10652 KB |
Output isn't correct |
3 |
Correct |
71 ms |
7920 KB |
Output is correct |
4 |
Runtime error |
15 ms |
8824 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Runtime error |
15 ms |
8320 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Runtime error |
17 ms |
8320 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Runtime error |
17 ms |
8320 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Correct |
54 ms |
5496 KB |
Output is correct |
9 |
Incorrect |
210 ms |
12508 KB |
Output isn't correct |
10 |
Incorrect |
205 ms |
10608 KB |
Output isn't correct |
11 |
Runtime error |
39 ms |
16244 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Runtime error |
63 ms |
16320 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
13 |
Correct |
82 ms |
11376 KB |
Output is correct |
14 |
Correct |
81 ms |
7920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
41 ms |
14040 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Runtime error |
78 ms |
19676 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Runtime error |
54 ms |
20592 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Runtime error |
42 ms |
15988 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Runtime error |
47 ms |
15364 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
6 |
Runtime error |
39 ms |
15476 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
7 |
Runtime error |
55 ms |
18416 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Runtime error |
55 ms |
20592 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
9 |
Runtime error |
51 ms |
17236 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
10 |
Runtime error |
50 ms |
16244 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
11 |
Runtime error |
82 ms |
18796 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Runtime error |
84 ms |
19692 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
13 |
Runtime error |
78 ms |
21996 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
14 |
Runtime error |
35 ms |
15472 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
39 ms |
17136 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Runtime error |
65 ms |
20332 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Correct |
101 ms |
14324 KB |
Output is correct |
4 |
Runtime error |
44 ms |
17268 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
5 |
Incorrect |
151 ms |
8664 KB |
Output isn't correct |
6 |
Incorrect |
136 ms |
11372 KB |
Output isn't correct |
7 |
Runtime error |
64 ms |
20432 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
8 |
Correct |
102 ms |
14324 KB |
Output is correct |
9 |
Correct |
73 ms |
8016 KB |
Output is correct |