제출 #1125596

#제출 시각아이디문제언어결과실행 시간메모리
1125596AgageldiBall Machine (BOI13_ballmachine)C++20
100 / 100
426 ms128192 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define N 4000005 #define ff first #define ss second #define pb push_back #define sz(s) (int)s.size() #define rep(c, a, b) for(c = a; c <= b; c++) int n, t, q, m = 1, vis[N], r, dis[N], sp[N][30], vip[N], k[N]; vector <int> v[N]; set <int> ans, jog; void solve(int x) { vector <pair<int,int>> s; for(auto i : v[x]) s.pb({dis[i],i}); sort(s.begin(),s.end()); for(auto i : s) solve(i.ss); vis[m] = x; k[x] = m; ans.insert(m); m++; } void dfs(int x) { for(auto i : v[x]) { dfs(i); dis[x] = min(dis[x],dis[i]); } dis[x] = min(x,dis[x]); } int main () { ios::sync_with_stdio(0);cin.tie(0); cin >> n >> q; for(int i = 0; i <= n; i++) { dis[i] = INT_MAX; } for(int i = 1; i <= n; i++) { int x; cin >> x; sp[i][0] = x; if(x) v[x].pb(i); else r = i; } for(int i = 1; i <= 25; i++) { for(int j = 1; j <= n; j++) { sp[j][i] = sp[sp[j][i - 1]][i - 1]; } } dfs(r); solve(r); for(int i = 1; i <= q; i++) { int x; cin >> t >> x; if(t == 1) { int answer = 0; for(int j = 1; j <= x; j++) { answer = *ans.begin(); jog.insert(*ans.begin()); ans.erase(*ans.begin()); } cout << vis[answer] << '\n'; continue; } int jg = x, cnt = 0; for(int j = 25; j >= 0; j--) { if(jog.find(k[sp[jg][j]]) != jog.end()) { jg = sp[jg][j]; cnt += (1 << j); } } cout << cnt << '\n'; ans.insert(k[jg]); jog.erase(k[jg]); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...