이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |