This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// credit: Mai Hong Kien
#include <bits/stdc++.h>
#define pb push_back
#define ppb pop_back
#define fr first
#define sc second
#define all(v) v.begin(), v.end()
#define mod (long long)(1e9 + 7)
#define eps (long long)(1e-9)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
ll LIMIT = 2e3 + 5;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll random (ll l, ll r){return uniform_int_distribution <ll> (l, r)(rng);}
const ll Mod = 1e9 + 7;
vector <int> min_index(1e5 + 1), euler;
vector <vector <int>> p(1e5 + 1, vector <int> (30, -1));
void init(int n){
for(int j = 1; (1 << j) <= n; j ++){
for(int i = 1; i <= n; i ++){
if(p[i][j - 1] != -1) p[i][j] = p[p[i][j]][j];
}
}
}
bool comp(int& a, int& b){
return min_index[a] > min_index[b];
}
void init_min(int node, vector <vector <int>>& graph, vector <int>& min_index){
for(auto& x : graph[node]){
init_min(x, graph, min_index);
min_index[node] = min(min_index[node], min_index[x]);
}
}
void dfs_euler(int node, vector <vector <int>>& graph){
euler.pb(node);
for(auto& x : graph[node]) dfs_euler(x, graph);
}
void solve(){
int n, q, start = 1, cnt = 0;
cin >> n >> q;
vector <vector <int>> graph(n + 1);
vector <int> id(n + 1), ball(n + 1, 0);
for(int i = 1; i <= n; i ++){
cin >> p[i][0];
if(p[i][0]) graph[p[i][0]].pb(i);
else start = i;
}
for(int i = 1; i <= n; i ++) min_index[i] = i;
init_min(start, graph, min_index);
for(int i = 1; i <= n; i ++) sort(all(graph[i]), comp);
dfs_euler(start, graph);
reverse(all(euler));
set <pair <int, int>> s;
for(int i = 0; i < euler.size(); i ++) s.insert({i + 1, euler[i]}), id[euler[i]] = i + 1;
while(q --){
int type, x;
cin >> type >> x;
if(type == 1){
while(x --){
auto it = s.begin();
ball[it -> sc] = 1;
if(!x) cout << it -> sc << "\n";
s.erase(it);
}
continue;
}
if(type == 2){
int ans = 0;
while(ball[p[x][0]]){
int i = 25, cur = 1 << 25;
while(p[x][i] == -1 || !ball[p[x][i]]) i --, cur >>= 1;
x = p[x][i];
ans += cur;
}
s.insert({id[x], x});
ball[x] = 0;
cout << ans << "\n";
}
}
}
signed main()
{
cin.tie(0)->sync_with_stdio(0);
if(fopen("TOY.inp", "r")){
freopen("TOY.inp", "r", stdin);
freopen("TOY.out", "w", stdout);
}
// cout << 1; return 0;
ll n = 1;
// cin >> n;
while(n) {
solve();
n --;
cout << "\n";
}
// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
Compilation message (stderr)
ballmachine.cpp: In function 'void solve()':
ballmachine.cpp:75:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int i = 0; i < euler.size(); i ++) s.insert({i + 1, euler[i]}), id[euler[i]] = i + 1;
| ~~^~~~~~~~~~~~~~
ballmachine.cpp:52:26: warning: unused variable 'cnt' [-Wunused-variable]
52 | int n, q, start = 1, cnt = 0;
| ^~~
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | freopen("TOY.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | freopen("TOY.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |