이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;
const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=100010, LOG=18;
int n, m, k, u, v, x, y, t, a, b, ans;
int par[MAXN][LOG], mn[MAXN], h[MAXN];
int stt[MAXN], fnt[MAXN], ras[MAXN], timer;
int dead[MAXN], cnt[MAXN];
vector<int> G[MAXN];
set<pii> st;
void dfs1(int node){
mn[node]=node;
for (int v:G[node]){
h[v]=h[node]+1;
dfs1(v);
mn[node]=min(mn[node], mn[v]);
cnt[node]++;
}
}
void dfs2(int node){
ras[stt[node]=timer++]=node;
sort(all(G[node]), [](int i, int j){
return mn[i]<mn[j];
});
for (int v:G[node]) dfs2(v);
fnt[node]=timer;
}
int AddBall(){
int v=st.begin()->second;
st.erase(st.begin());
dead[v]=1;
if (!--cnt[par[v][0]] && !dead[par[v][0]]) st.insert({stt[par[v][0]], par[v][0]});
return v;
}
int RemBall(int v){
for (int i=LOG-1; ~i; i--) if (dead[par[v][i]]) v=par[v][i];
dead[v]=0;
if (!cnt[v]) st.insert({stt[v], v});
if (!cnt[par[v][0]]++) st.erase({stt[par[v][0]], par[v][0]});
return v;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin>>n>>m;
for (int i=1; i<=n; i++){
cin>>par[i][0];
G[par[i][0]].pb(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);
for (int v=1; v<=n; v++) if (!cnt[v] && !dead[v]) st.insert({stt[v], v});
while (m--){
cin>>t>>x;
if (t==1){
while (x--) ans=AddBall();
cout<<ans<<'\n';
continue ;
}
ans=RemBall(x);
cout<<h[x]-h[ans]<<'\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... |