Submission #643526

#TimeUsernameProblemLanguageResultExecution timeMemory
643526KindaNamelessBall Machine (BOI13_ballmachine)C++14
100 / 100
173 ms22680 KiB
#include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<numeric> #include<string> #include<vector> #include<bitset> #include<queue> #include<stack> #include<deque> #include<cmath> #include<map> #include<set> using namespace std; #define ll long long #define ld long double vector<int> adj[100005]; bool occupied[100005]; int mn[100005], order[100005], par[100005][17]; int timer = 0; bool cmp(const int &a, const int &b){ return mn[a] < mn[b]; } void dfs(int cur){ for(int j = 1; j < 17; ++j){ par[cur][j] = par[par[cur][j-1]][j-1]; } mn[cur] = cur; for(int ch : adj[cur]){ par[ch][0] = cur; dfs(ch); mn[cur] = min(mn[cur], mn[ch]); } } void det(int cur){ sort(adj[cur].begin(), adj[cur].end(), cmp); for(int ch : adj[cur]){ det(ch); } order[cur] = ++timer; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N, Q; cin >> N >> Q; int root = 0; for(int i = 1; i <= N; ++i){ int p; cin >> p; if(p == 0)root = i; else adj[p].push_back(i); } dfs(root); det(root); priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; for(int i = 1; i <= N; ++i){ pq.push(make_pair(order[i], i)); } for(int i = 1; i <= Q; ++i){ int op; cin >> op; if(op == 1){ int k; cin >> k; while(k--){ if(k == 0)cout << pq.top().second << "\n"; occupied[pq.top().second] = 1; pq.pop(); } } else{ int x, diff = 0; cin >> x; for(int j = 16; j >= 0; --j){ if(occupied[par[x][j]]){ x = par[x][j]; diff += (1 << j); } } occupied[x] = 0; pq.push(make_pair(order[x], x)); cout << diff << "\n"; } } 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...