Submission #894688

#TimeUsernameProblemLanguageResultExecution timeMemory
894688dwuyBall Machine (BOI13_ballmachine)C++14
100 / 100
182 ms23544 KiB
/// dwuy: _,\,,,_\,__,\,,_ #include <bits/stdc++.h> #define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL) #define file(a) freopen(a".inp","r",stdin); freopen(a".out", "w",stdout) #define fi first #define se second #define endl "\n" #define len(s) int32_t(s.length()) #define MASK(k)(1LL<<(k)) #define TASK "" using namespace std; typedef tuple<int, int, int> tpiii; typedef pair<double, double> pdd; typedef pair<int, int> pii; typedef long long ll; const long long OO = 1e18; const int MOD = 1e9 + 7; const int INF = 1e9; struct SMT{ int n; vector<int> tree; SMT(int n=0){ this->n = n; this->tree.assign(n<<2|1, 0); } void down(int id){ if(tree[id]==0) return; tree[id<<1] += tree[id]; tree[id<<1|1] += tree[id]; tree[id] = 0; } void update(int l, int r, int id, const int &u, const int &v, const int &val){ if(l>v || r<u) return; if(l>=u && r<=v){ tree[id] += val; return; } down(id); int mid = (l+r)>>1; update(l, mid, id<<1, u, v, val); update(mid+1, r, id<<1|1, u, v, val); } void update(int l, int r, int val){ update(1, n, 1, l, r, val); } int get(int pos){ int id = 1; for(int lo=1, hi=n; lo<hi;){ down(id); int mid = (lo+hi)>>1; if(pos<=mid) id = id<<1, hi = mid; else lo = mid + 1, id = id<<1|1; } return tree[id]; } }; const int MX = 100005; int n, q; vector<int> G[MX]; void nhap(){ cin >> n >> q; for(int i=1; i<=n; i++){ int u; cin >> u; G[u].push_back(i); } } int p[MX][17]; int num[MX]; int clo[MX]; int mnID[MX]; int dfsID = 0; void calcID(int u){ mnID[u] = u; for(int v: G[u]){ calcID(v); p[v][0] = u; mnID[u] = min(mnID[u], mnID[v]); } } bool comp(const int &a, const int &b){ return mnID[a] < mnID[b]; } void dfs(int u){ num[u] = ++dfsID; sort(G[u].begin(), G[u].end(), comp); for(int v: G[u]) dfs(v); clo[u] = dfsID; } int cnt[MX]; void solve(){ int root = G[0][0]; calcID(root); dfs(root); set<pair<int, int>> Q; for(int i=1; i<=n; i++) if(G[i].size() == 0){ Q.insert({num[i], i}); } for(int j=1; j<=16; j++){ for(int i=1; i<=n; i++){ p[i][j] = p[p[i][j-1]][j-1]; } } SMT smt(n); while(q--){ int oper, u; cin >> oper >> u; if(oper == 1){ int ans = 0; while(u--){ int v = Q.begin()->se; Q.erase(Q.begin()); ans = v; smt.update(num[v], clo[v], 1); if(++cnt[p[v][0]] == (int)G[p[v][0]].size()){ Q.insert({num[p[v][0]], p[v][0]}); } } cout << ans << endl; } if(oper == 2){ int h = smt.get(num[u]) - 1; int v = u; for(int t=h; t; t-=-t&t) v = p[v][__lg(-t&t)]; smt.update(num[v], clo[v], -1); if(cnt[p[v][0]] == (int)G[p[v][0]].size()){ Q.erase(Q.find({num[p[v][0]], p[v][0]})); } cnt[p[v][0]]--; Q.insert({num[v], v}); cout << h << endl; } } } int32_t main(){ fastIO; //file(TASK); nhap(); solve(); 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...