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 ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 3e5+5;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n, q;
vector<int> adj[MAXN];
int pri[MAXN];
struct op{
bool operator()(int a, int b){
return pri[a] < pri[b];
}
};
set<int, op> st;
const int LOG = 18;
int par[MAXN][18];
int sbt[MAXN];
int pr = 1;
void dfs1(int v){
sbt[v] = v;
for(int j: adj[v]){
dfs1(j);
sbt[v] = min(sbt[v], sbt[j]);
}
sort(adj[v].begin(), adj[v].end(), [](int a, int b){
return sbt[a] < sbt[b];
});
}
bool covered[MAXN];
void dfs2(int v){
for(int j: adj[v]){
dfs2(j);
}
pri[v] = pr++;
if(v) st.insert(v);
}
int kpar(int v, int k){
for(int j = 0; j < LOG; j++){
if((1<<j) & k){
v = par[v][j];
}
}
return v;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
#ifdef Local
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
#endif
cin>>n>>q;
for(int i = 1; i <= n; i++){
cin>>par[i][0];
adj[par[i][0]].push_back(i);
}
for(int j = 1; j < LOG; j++){
for(int i = 1; i <= n; i++){
par[i][j] = par[par[i][j - 1]][j - 1];
}
}
dfs1(0);
dfs2(0);
while(q--){
int type, num;
cin>>type>>num;
if(type == 1){
int last = 0;
for(int j = 0; j < num; j++){
assert(!st.empty());
int u = *st.begin();
st.erase(*st.begin());
covered[u] = 1;
last = u;
}
cout<<last<<endl;
}else{
int l = 0, r = n;
while(l < r){
int m = (l + r + 1) / 2;
if(covered[kpar(num, m)]){
l = m;
}else{
r = m - 1;
}
}
cout<<l<<endl;
int u = kpar(num, l);
covered[u] = 0;
st.insert(u);
}
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
# | 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... |