Submission #1108705

#TimeUsernameProblemLanguageResultExecution timeMemory
11087050pt1mus23Ball Machine (BOI13_ballmachine)C++14
100 / 100
415 ms42364 KiB
// CALIS ULAN CALIS #include <bits/stdc++.h> using namespace std; #define int long long int #define ins insert #define pb push_back #define endl '\n' #define putr(x) cout<<x<<endl;return; #define all(x) x.begin(),x.end() const int mod = 1e9 +7, sze = 1e5 +10, inf = LLONG_MAX, LG = 20; vector<int> graph[sze]; int mnx[sze]; int depth[sze]; set<pair<int,int>> bos; int lan[sze]; int pp[sze]; int dd[sze],mn[sze]; int up[LG][sze]; void dfs(int node,int par=0){ mnx[node]=node; pp[node]=par; up[0][node]=par; for(int i=1;i<LG;i++){ up[i][node]=up[i-1][up[i-1][node]]; } for(auto v:graph[node]){ if(v!=par){ depth[v]=depth[node]+1; dfs(v,node); mnx[node]=min(mnx[node],mnx[v]); // deg++; dd[node]++; } } } int tim = 0; bool cpm(int a,int b){ return mnx[a]<mnx[b]; } int gir =0; void dfs2(int node,int par=0){ sort(all(graph[node]), cpm ); // int deg=0; for(auto v:graph[node]){ if(v!=par){ dfs2(v,node); } } mn[node]=(++gir); if(dd[node]==0){ bos.ins({mn[node],node}); } } void rush(){ int n,q; cin>>n>>q; int root=0; for(int i=1;i<=n;i++){ int x;cin>>x; if(x){ graph[i].pb(x); graph[x].pb(i); } else{ root=i; } } dfs(root,0); dfs2(root,0); while(q--){ int op; int node; cin>>op>>node; if(op==1){ int last= 0; while(node--){ if(bos.empty()){ // putr("bitdi"); assert(0); } auto yalvar = *bos.begin(); bos.erase(bos.begin()); lan[yalvar.second]=1; last= yalvar.second; // cout<<last<<" omg "<<endl; // test(root); if(pp[last]){ --dd[pp[last]]; if(dd[pp[last]]==0){ bos.insert({mn[pp[last]],pp[last]}); } } } cout<<last<<endl; } else{ int emp = node; for(int i= LG-1;i>=0;i--){ if(lan[up[i][emp]]){ emp = up[i][emp]; } } lan[emp]=0; int surus = depth[node] - depth[emp]; // cout<<"ulan "<<emp<<endl; cout<<surus<<endl; if(pp[emp]){ dd[pp[emp]]++; } // test(root); bos.insert({mn[emp],emp}); } } } signed main(){ // ios::sync_with_stdio(0); // cin.tie(0); // cout.tie(0); int tt = 1; // cin>>tt; while(tt--){ rush(); } 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...