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;
#define int long long
vector< vector<int> > Graph;
vector<int> minLeft;
vector<int> minRight;
const int INF = 1e15;
void dfs(int x, int last){
for(int a : Graph[x]){
if(a == last) continue;
dfs(a,x);
}
if(!Graph[x].size()){
minLeft[x] = INF;
minRight[x] = INF;
return;
}
minLeft[x] = min(min(minLeft[Graph[x][0]], minRight[Graph[x][0]]), Graph[x][0]);
minRight[x] = min(min(minRight[Graph[x][1]], minLeft[Graph[x][1]]), Graph[x][1]);
}
vector< bool > isFull;
int last;
void add(int node){
// cout << node << endl;
if(Graph[node].size() == 0 || (isFull[Graph[node][0]] && isFull[Graph[node][1]])){
isFull[node] = true;
last = node;
return;
}
if(isFull[Graph[node][1]]) add(Graph[node][0]);
else if(isFull[Graph[node][0]]) add(Graph[node][1]);
else if(minLeft[node] < minRight[node]){
add(Graph[node][0]);
}else{
add(Graph[node][1]);
}
}
vector< int > up;
int tot ;
void rem(int node, int last){
if(node == 0) return;
if(last != -1){
if(!isFull[last] && isFull[node]){
tot++;
isFull[last] = true;
isFull[node] = false;
}
}
rem(up[node], node);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
Graph.resize(n+1);
int root;
up.resize(n+1);
for(int i = 0; i < n; i++){
int a;
cin >> a;
Graph[a].push_back(i+1);
up[i+1] = a;
if(a == 0) root = i+1;
}
minRight.resize(n+1);
minLeft.resize(n+1);
dfs(root,-1);
isFull.resize(n+1);
while(q--){
int a;
cin >> a;
int b;
cin >> b;
if(a == 1){
for(int i = 0; i < b; i++){
add(root);
}
cout << last << endl;
}else{
isFull[b] = false;
tot = 0;
rem(b,-1);
cout << tot << endl;
}
}
}
Compilation message (stderr)
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:93:20: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
add(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... |