This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
///huynhocute123///
#include<bits/stdc++.h>
using namespace std;
#define S second
#define F first
#define pii pair<int,int>
#define piii pair<int,pair<int,int>>
#define pb push_back
#define pi M_PI
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define REP(i, a, b) for(int i = b; i >= a; --i)
#define ALL(v) v.begin(),v.end()
#define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
#define out(name) if(fopen(name, "w")) freopen(name, "w", stdout);
//random_device rd;
//mt19937 rng(rd());
//#pragma GCC optimize ("O3")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("popcnt")
//#define int long long
const int maxN = 1e5 +9 ;
const int modd = 1e9 + 7;
const int base = 2309;
const int MAX = 1e9+9;
void minimize(int &u, int v){
if(v < u) u = v;
}
void maximize(int &u, int v){
if(v > u) u = v;
}
int n, k, t, m, res, a[maxN], dep[maxN], TIME, tt[maxN], have[maxN], T[maxN][20];
int l, r;
bool vis[maxN];
int mi[maxN];
priority_queue<pii , vector<pii> , greater<pii>>pq;
vector<int> e[maxN];
void dfs2(int u,int p){
mi[u] = u;
for(auto x : e[u]){
if(x == p)continue;
dep[x] =dep[u] +1;
T[x][0]= u;
dfs2(x,u);
minimize(mi[u], mi[x]);
}
}
bool cmp(int a,int b){
return mi[a] < mi[b];
}
void build(){
FOR(i, 1, 18){
FOR(u, 1, n){
T[u][i]= T[T[u][i-1]][i-1];
}
}
}
void dfs3(int u, int p){
for(auto x : e[u]){
if(x == p)continue;
dfs3(x, u);
}
tt[u] = ++TIME;
}
void task1(){
int last =0;
while(r--){
int u = pq.top().S;
have[u] =1;
pq.pop();
last = u;
}
cout << last <<'\n';
}
int lca(int u){
// int ans =0;
REP(i ,0 , 18){
if(have[T[u][i]])u = T[u][i];
}
return u;
}
void task2(){
int u = r, v = lca(u);
cout << dep[u] - dep[v] <<'\n';
have[v] =0;
pq.push({tt[v], v });
// cout <<dep[u] << " " <c< dep[v] <<'\n';
// cout << u << " " << v <<'\n';
}
void solve(){
cin >> n >> m;
int root =0;
FOR(i , 1, n){
int x;cin >> x;
if(x == 0)root= i;
else{
e[i].pb(x);
e[x].pb(i);
// cout << i << " " << x <<'\n';
}
}
dfs2(root, 0);
FOR(i, 1, n){
sort(ALL(e[i]), cmp);
}
dfs3(root,0);
build();
// cout << T[6][1];
FOR(i, 1, n)pq.push( {tt[i], i });
FOR(ix, 1, m){
cin >> l >> r;
if(l == 1)task1();
else task2();
}
}
signed main(){
// freopen("name.inp","r",stdin);
// freopen("name.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
inp("task.inp");
t = 1;
// cin >> t;
while( t-- )solve();
}
Compilation message (stderr)
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:13:47: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | #define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~
ballmachine.cpp:126:5: note: in expansion of macro 'inp'
126 | inp("task.inp");
| ^~~
# | 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... |