제출 #230476

#제출 시각아이디문제언어결과실행 시간메모리
230476alishahali1382Ball Machine (BOI13_ballmachine)C++14
100 / 100
238 ms23804 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...