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 int long long
using namespace std;
int n, q, timer;
vector<int> ord, mn, stat, h;
vector<vector<int>> par;
vector<vector<pair<int, int>>> adj;
void dfs1(int u=0, int H=0) {
mn[u]=u; h[u]=H;
for(auto &p : adj[u])
dfs1(p.second, H+1), mn[u]=min(mn[u], p.first=mn[p.second]);
}
void dfs2(int u=0) {
for(auto p : adj[u])
dfs2(p.second);
ord[u]=timer--;
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> q;
ord.resize(n+1); adj.resize(n+1);
par.resize(n+1, vector<int>(20));
mn.resize(n+1); stat.resize(n+1);
h.resize(n+1);
for(int i=1; i<=n; i++) {
int p; cin >> p;
adj[p].push_back({i, i});
par[i][0]=p;
}
for(int d=1; d<20; d++)
for(int i=0; i<=n; i++) par[i][d]=par[par[i][d-1]][d-1];
dfs1();
for(auto &p : adj) sort(p.begin(), p.end());
dfs2();
priority_queue<pair<int, int>> pq;
for(int i=0; i<=n; i++) pq.push({ord[i], i});
while(q--) {
int type, in; cin >> type >> in;
if(type==1) {
int las;
while(in--)
stat[las=pq.top().second]=1, pq.pop();
cout << las << '\n';
} else {
int prev; prev=h[in];
for(int d=19; d>=0; d--) if(stat[par[in][d]]) in=par[in][d];
stat[in]=0; pq.push({ord[in], in});
cout << prev-h[in] << '\n';
}
}
return 0;
}
# | 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... |