# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
195590 | karma | Ball Machine (BOI13_ballmachine) | C++14 | 927 ms | 21944 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.
#include <bits/stdc++.h>
#define pb emplace_back
#define ll long long
#define fi first
#define se second
#define mp make_pair
//#define int int64_t
using namespace std;
const int N = (int)1e5 + 2;
const int logN = 20;
int p[N][logN + 1], del[N], d[N], deg[N];
int root, n, q, x, k, low, high, mid;
vector<int> a[N];
set<int> leaf;
void DFS(int u) {
deg[u] = a[u].size();
for(int i = 1; i <= logN; ++i) p[u][i] = p[p[u][i - 1]][i - 1];
if(!deg[u]) leaf.insert(u);
for(int v: a[u]) d[v] = d[u] + 1, DFS(v);
}
int Node(int x, int step) {
for(int i = logN; i >= 0; --i)
if(step >= (1 << i)) x = p[x][i], step -= (1 << i);
return x;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define Task "test"
if(fopen(Task".inp", "r")) {
freopen(Task".inp", "r", stdin);
freopen(Task".out", "w", stdout);
}
cin >> n >> q;
for(int i = 1; i <= n; ++i) {
cin >> p[i][0]; a[p[i][0]].pb(i);
if(!p[i][0]) root = i;
}
DFS(root);
while(q --) {
cin >> x >> k;
if(x == 1) {
while(k --) {
x = *leaf.begin(); leaf.erase(x);
if(--deg[p[x][0]] == 0) leaf.insert(p[x][0]);
del[x] = 1;
}
cout << x << '\n';
} else {
low = 0, high = d[k];
while(low <= high) {
mid = (low + high) >> 1;
if(del[Node(k, mid)]) low = mid + 1;
else high = mid - 1;
}
x = Node(k, high), del[x] = 0, leaf.insert(x);
if(++deg[p[x][0]] == 1) leaf.erase(p[x][0]);
cout << d[k] - d[x] << '\n';
}
}
}
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... |