Submission #41057

#TimeUsernameProblemLanguageResultExecution timeMemory
41057KerimBall Machine (BOI13_ballmachine)C++14
100 / 100
363 ms69640 KiB
#include "bits/stdc++.h" #define MAXN 100009 #define INF 1000000007 #define mp(x,y) make_pair(x,y) #define all(v) v.begin(),v.end() #define pb(x) push_back(x) #define wr cout<<"----------------"<<endl; #define ppb() pop_back() #define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++) #define ff first #define ss second #define my_little_dodge 46 #define debug(x) cerr<< #x <<" = "<< x<<endl; using namespace std; typedef long long ll; typedef pair<int,int> PII; template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;} template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;} vector<int>adj[MAXN]; int dp[MAXN],id[MAXN],cnt,P[MAXN][18],vis[MAXN],n; set<PII>s; bool cmp(int x,int y){ return (dp[x]<dp[y]); } void pre(int nd){ dp[nd]=nd; if(adj[nd].size()==0) return; tr(it,adj[nd]) pre(*it),umin(dp[nd],dp[*it]); sort(all(adj[nd]),cmp); } vector<int>v; void dfs(int nd){ tr(it,adj[nd]) P[*it][0]=nd,dfs(*it); id[nd]=++cnt; } void build(){ for(int j=1;j<=17;j++) for(int i=1;i<=n;i++) if(~P[i][j-1]) P[i][j]=P[P[i][j-1]][j-1]; } int main(){ memset(P,-1,sizeof P); //~ freopen("file.in", "r", stdin); int q,root; scanf("%d%d",&n,&q); for(int i=1;i<=n;i++){ int p; scanf("%d",&p); if(!p) root=i; else adj[p].pb(i); } pre(root);dfs(root); build(); for(int i=1;i<=n;i++) s.insert(mp(id[i],i)); while(q--){ int t,k,ans=0; scanf("%d%d",&t,&k); if(t==1){ __typeof((s).begin())it=s.begin(); while(k--){ vis[it->ss]=1; ans=it->ss; s.erase(it++); } } else{ for(int j=17;j>=0;j--) if(~P[k][j] and vis[P[k][j]]) k=P[k][j],ans+=(1<<j); vis[k]=0; s.insert(mp(id[k],k)); } printf("%d\n",ans); } return 0; }

Compilation message (stderr)

ballmachine.cpp: In function 'int main()':
ballmachine.cpp:50:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&n,&q);
                        ^
ballmachine.cpp:53:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&p);
                 ^
ballmachine.cpp:65:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&t,&k);
                      ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...