Submission #49007

#TimeUsernameProblemLanguageResultExecution timeMemory
49007NamnamseoBall Machine (BOI13_ballmachine)C++17
77.38 / 100
1086 ms73028 KiB
#include <bits/stdc++.h> using namespace std; #define sz(v) ((int)((v).size())) #define all(v) (v).begin(), (v).end() #define pb push_back #define coord_comp(v) sort(all(v)), v.erase(unique(all(v)), v.end()) #define v_index(v, x) (lower_bound(all(v),x)-(v).begin()) typedef pair<int,int> pp; typedef long long ll; void read(int& x){ scanf("%d",&x); } void read(ll& x){ scanf("%lld",&x); } template<typename T1,typename T2> void read(pair<T1,T2>& p){ read(p.first); read(p.second); } template<typename T,typename... Args> void read(T&a,Args&...b){ read(a); read(b...); } int submin[100010]; int insind[100010]; int par[20][100001]; auto comp = [&](const int& a,const int& b){ return insind[a] < insind[b]; }; set<int,decltype(comp)> s(comp); vector<int> edge[100010]; int n; int dfs(int x){ int ret=x; for(int y : edge[x]){ ret=min(ret, dfs(y)); } return submin[x] = ret; } int nowt; void dd(int x){ insind[x] = nowt; ++nowt; vector<pp> asdf; for(int y : edge[x]){ asdf.push_back({submin[y], y}); } sort(all(asdf)); for(pp yy : asdf) dd(yy.second); } int cc[100001]; set<int> emptyNodes; int main(){ int Q; read(n,Q); int rt; for(int i=1; i<=n; ++i){ int p; read(p); par[0][i]=p; if(p==0) rt=i; else edge[p].pb(i); } dfs(rt); dd(rt); for(int i=1; i<=n; ++i){ cc[i] = edge[i].size(); if(cc[i] == 0){ s.insert(i); } emptyNodes.insert(i); } par[0][rt] = -1; for(int i=1; i<=17; ++i){ for(int j=1; j<=n; ++j){ int t=par[i-1][j]; if(t == -1) par[i][j]=-1; else par[i][j]=par[i-1][t]; } } for(;Q--;){ int q,x; read(q,x); if(q == 1){ int p; for(;x--;){ p = *s.begin(); s.erase(s.begin()); emptyNodes.erase(p); int z=par[0][p]; if(z != -1){ --cc[z]; if(cc[z] == 0){ s.insert(z); } } } printf("%d\n", p); } else { int step = 0; for(int i=17; 0<=i; --i){ if(par[i][x] == -1) continue; int t = par[i][x]; if(emptyNodes.find(t) == emptyNodes.end()) x = t, step += (1<<i); } printf("%d\n", step); s.insert(x); emptyNodes.insert(x); if(par[0][x] != -1){ s.erase(par[0][x]); ++cc[par[0][x]]; } } } return 0; }

Compilation message (stderr)

ballmachine.cpp: In function 'void read(int&)':
ballmachine.cpp:10:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 void read(int& x){ scanf("%d",&x); }
                    ~~~~~^~~~~~~~~
ballmachine.cpp: In function 'void read(ll&)':
ballmachine.cpp:11:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 void read(ll& x){ scanf("%lld",&x); }
                   ~~~~~^~~~~~~~~~~
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:73:13: warning: 'rt' may be used uninitialized in this function [-Wmaybe-uninitialized]
  par[0][rt] = -1;
  ~~~~~~~~~~~^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...