Submission #26387

#TimeUsernameProblemLanguageResultExecution timeMemory
26387samir_droubiBall Machine (BOI13_ballmachine)C++14
100 / 100
879 ms32444 KiB
#include <bits/stdc++.h> using namespace std; int n,q; const int mxn=(1e5)+5; vector<int>tr[mxn]; int dep[mxn]; int pr[mxn][23]; int mn[mxn]; void dfs(int v) { mn[v]=v; for(int i=0;i<tr[v].size();++i) { int u=tr[v][i]; pr[u][0]=v; dep[u]=dep[v]+1; dfs(u); mn[v]=min(mn[v],mn[u]); } } int mxlog=0; void fill() { while((1<<mxlog)<=n)++mxlog; for(int j=1;j<mxlog;++j) for(int i=1;i<=n;++i) pr[i][j]=pr[pr[i][j-1]][j-1]; } int in[mxn]; vector<int>ord; bool cmp(int x,int y) { return mn[x]<mn[y]; } void dfs1(int v) { sort(tr[v].begin(),tr[v].end(),cmp); for(int i=0;i<tr[v].size();++i) { int u=tr[v][i]; dfs1(u); } in[v]=ord.size(); ord.push_back(v); } int st[mxn*4]; int lz[mxn*4]; void pros(int p,int l,int r) { if(lz[p]==-1)return; st[p]=lz[p]*(r-l+1); if(l!=r) { lz[p*2]=lz[p]; lz[p*2+1]=lz[p]; } lz[p]=-1; } void upd(int p,int l,int r,int i,int j,int x) { pros(p,l,r); if(r<i||l>j)return; if(l>=i&&r<=j) { lz[p]=x; pros(p,l,r); return; } int md=(l+r)/2; upd(p*2,l,md,i,j,x); upd(p*2+1,md+1,r,i,j,x); st[p]=st[p*2]+st[p*2+1]; } int sum(int p,int l,int r,int i,int j) { pros(p,l,r); if(r<i||l>j)return 0; if(l>=i&&r<=j)return st[p]; int md=(l+r)/2; int x=sum(p*2,l,md,i,j); int y=sum(p*2+1,md+1,r,i,j); return x+y; } int bs(int k) { int l=1; int r=n; while(l<=r) { int md=(l+r)/2; int s=md-sum(1,1,n,1,md); if(s<k)l=md+1; else if(s>k)r=md-1; else return md; } } set<int>emp; int find(int v) { for(int i=mxlog-1;i>=0;--i) { int p=pr[v][i]; int vl=0; if(p)vl=sum(1,1,n,in[p],in[p]); if(vl)v=p; } return v; } void pros(int i) { int ans=0; while(!emp.empty()) { set<int>::iterator it=emp.begin(); if(*it>i)break; ans=*it; emp.erase(it); } printf("%d\n",ord[ans]); } int main() { memset(lz,-1,sizeof lz); scanf("%d%d",&n,&q); for(int i=1;i<=n;++i) { int p; scanf("%d",&p); tr[p].push_back(i); } for(int i=1;i<=n;++i)emp.insert(i); ord.push_back(-1); dfs(0); fill(); dfs1(0); while(q--) { int ty,x; scanf("%d%d",&ty,&x); if(ty==1) { int i=bs(x); upd(1,1,n,1,i,1); pros(i); } else { int v=find(x); upd(1,1,n,in[v],in[v],0); emp.insert(in[v]); printf("%d\n",dep[x]-dep[v]); } } return 0; }

Compilation message (stderr)

ballmachine.cpp: In function 'void dfs(int)':
ballmachine.cpp:14:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<tr[v].size();++i)
               ^
ballmachine.cpp: In function 'void dfs1(int)':
ballmachine.cpp:42:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<tr[v].size();++i)
               ^
ballmachine.cpp: In function 'int bs(int)':
ballmachine.cpp:101:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:131:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d",&n,&q);
                     ^
ballmachine.cpp:135:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&p);
                 ^
ballmachine.cpp:146:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&ty,&x);
                       ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...