# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
36405 | ToMoClone | Ball Machine (BOI13_ballmachine) | C++14 | 299 ms | 22528 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
*/
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int N = 100001;
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > Empty;
vector<int> child[N];
int n, q, cnt, Min[N], par[17][N], Ball[N], Pos[N];
void dfs(int u){
for(int v:child[u])
dfs(v), Min[u] = min(Min[u], Min[v]);
sort(child[u].begin(), child[u].end(), [&](int a, int b){
return Min[a] < Min[b];
});
}
void dfs2(int u){
for(int v:child[u]) dfs2(v);
Empty.push(make_pair(Pos[u] = ++cnt, u));
}
int main(){
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; ++i){
int u; scanf("%d", &u);
child[u].push_back(i);
par[0][i] = u, Min[i] = i;
}
for(int i = 1; i <= n; ++i)
if(par[0][i] == 0) dfs(i);
for(int i = 1; i <= n; ++i)
if(par[0][i] == 0) dfs2(i);
for(int j = 1; j < 17; ++j)
for(int i = 1; i <= n; ++i)
par[j][i] = par[j - 1][par[j - 1][i]];
while(q--){
int type; scanf("%d", &type);
if(type == 2){
int u, jump = 0; scanf("%d", &u);
for(int i = 16; i >= 0; --i)
if(Ball[par[i][u]]) u = par[i][u], jump += (1 << i);
printf("%d\n", jump);
Ball[u] = 0, Empty.push(make_pair(Pos[u], u));
}
else {
int num, LastNode = 0; scanf("%d", &num);
while(num--){
LastNode = Empty.top().second, Empty.pop();
Ball[LastNode] = 1;
}
printf("%d\n", LastNode);
}
}
}
Compilation message (stderr)
# | 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... |