Submission #979216

#TimeUsernameProblemLanguageResultExecution timeMemory
979216batsukh2006Ball Machine (BOI13_ballmachine)C++17
100 / 100
171 ms46836 KiB
#include<iostream> #include<stdio.h> #include<math.h> #include<map> #include<string> #include<algorithm> #include<vector> #include<string.h> #include<utility> #include<set> #include<cmath> #include<queue> #include<deque> #include<functional> #include<stack> #include<limits.h> #include<iomanip> #include<unordered_map> #include<numeric> #include<tuple> using namespace std; #define MOD 1000000009 #define int long long #define ff first #define ss second #define endl '\n' typedef pair<int,int> pp; int order=0; const int mxN=1e5+1; vector<bool> e(mxN); vector<vector<int> > p(mxN,vector<int>(20)); vector<int> lvl(mxN),o(mxN),v[mxN],dp(mxN,1e9); void pre(int a, int d){ lvl[a]=d; for(auto node: v[a]){ pre(node,d+1); dp[a]=min(dp[a],dp[node]); } dp[a]=min(dp[a],a); } void dfs(int a){ vector<pp> t; for(auto node: v[a]){ t.push_back({dp[node],node}); } sort(t.begin(),t.end()); for(auto node: t) dfs(node.ss); o[a]=++order; } int find(int a){ for(int i=19; i>=0; i--){ if(e[p[a][i]]) a=p[a][i]; } return a; } signed main(){ // freopen("248.in", "r", stdin); // freopen("248.out", "w", stdout); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m; cin>>n>>m; for(int i=1; i<=n; i++){ cin>>p[i][0]; v[p[i][0]].push_back(i); } pre(0,0); dfs(0); for(int i=1; i<20; i++){ for(int j=1; j<=n; j++){ p[j][i]=p[p[j][i-1]][i-1]; } } priority_queue<pp,vector<pp>,greater<pp> > q; for(int i=1; i<=n; i++) q.push({o[i],i}); while(m--){ int t,x; cin>>t>>x; if(t==1){ int r; while(x--){ r=q.top().ss; e[r]=true; q.pop(); } cout<<r<<endl; }else{ int c=find(x); e[c]=false; q.push({o[c],c}); cout<<lvl[x]-lvl[c]<<endl; } } 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...