Submission #859260

#TimeUsernameProblemLanguageResultExecution timeMemory
8592608pete8Ball Machine (BOI13_ballmachine)C++14
65.57 / 100
1075 ms42152 KiB
#include<iostream> #include<stack> #include<map> #include<vector> #include<string> #include<unordered_map> #include <queue> #include<cstring> #include<limits.h> #include<cmath> #include<set> #include<algorithm> #include<bitset> #include<list> using namespace std; #define ll long long #define f first #define endl "\n" #define s second #define pii pair<int,int> #define ppii pair<int,pii> #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define F(n) for(int i=0;i<n;i++) #define lb lower_bound #define ub upper_bound //#define p push #define pb push_back #define fastio ios::sync_with_stdio(false);cin.tie(NULL); using namespace std; #define int long long const int mxn=1e5,mod=69,lg=30,root=80,inf=1e18; vector<ppii>adj[mxn+10]; int up[mxn+10][lg+5],h[mxn+10],st,go[mxn+10]; bitset<mxn+10>on; int ch=0; int dfs(int cur,int p){ int cm=inf; for(auto &i:adj[cur]){ h[i.f]=h[cur]+1; ch=max(ch,h[i.f]); int k=dfs(i.f,cur); cm=min(cm,k); i.s.f=k; } return min(cm,cur); } int solve(int cur,int p){ int cm=inf; for(auto &i:adj[cur])cm=min(cm,i.s.f); for(auto &i:adj[cur]){ i.s.s=solve(i.f,cur); if(i.s.f==cm)go[cur]=i.s.s; } if(cm==inf)return go[cur]=cur; return go[cur]; } int32_t main(){ fastio int n,m;cin>>n>>m; for(int i=1;i<=n;i++){ int a;cin>>a; if(a==0){ st=i; continue; } adj[a].pb({i,{-1,-1}}); up[i][0]=a; } priority_queue<pii>pq; dfs(st,-1); solve(st,-1); for(int j=1;j<=lg;j++)for(int i=1;i<=n;i++)up[i][j]=up[up[i][j-1]][j-1]; while(m--){ int t;cin>>t; if(t==1){ int k;cin>>k; int ans=0; for(int j=0;j<k;j++){ while(!pq.empty()&&!on[pq.top().s])pq.pop(); while(!pq.empty()&&on[up[pq.top().s][0]])pq.pop(); if(pq.empty())pq.push({h[go[st]],go[st]}),on[go[st]]=true,ans=go[st]; else{ int cur=pq.top().s; cur=up[cur][0]; int cm=inf; pq.pop(); for(auto i:adj[cur])if(!on[i.f])cm=min(cm,i.s.f); for(auto i:adj[cur]){ if(i.s.f==cm){ pq.push({h[i.s.s],i.s.s}),on[i.s.s]=true,ans=i.s.s; break; } } if(cm==inf){ on[cur]=true; ans=cur; pq.push({h[cur],cur}); } } } cout<<ans<<'\n'; } else{ int x,y,diff;cin>>x; y=x; for(int i=lg;i>=0;i--)if(on[up[x][i]])x=up[x][i]; diff=h[y]-(h[x]+1); for(int i=0;i<=lg;i++)if(diff&(1<<i))y=up[y][i]; on[x]=false; if(diff!=-1)pq.push({h[y],y}); else{for(auto i:adj[x])pq.push({h[i.f],i.f});} cout<<diff+1<<'\n'; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...