Submission #1087713

#TimeUsernameProblemLanguageResultExecution timeMemory
1087713TrinhKhanhDungBall Machine (BOI13_ballmachine)C++14
11.83 / 100
185 ms29008 KiB
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define sz(x) (int)x.size() #define ALL(v) v.begin(),v.end() #define MASK(k) (1LL << (k)) #define BIT(x, i) (((x) >> (i)) & 1) #define oo (ll)1e18 #define INF (ll)1e9 #define MOD (ll)(1007050321) using namespace std; template<class T1, class T2> bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;} template<class T1, class T2> bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;} template<class T1, class T2> void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;} template<class T1, class T2> void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;} template<class T> void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());} struct fen{ vector<int> tree; int n; fen(int _n = 0){ n = _n; tree.resize(n + 3, 0); } void upd(int p, int c){ while(p <= n){ tree[p] += c; p += p & -p; } } int get(int p){ int ans = 0; while(p > 0){ ans += tree[p]; p -= p & -p; } return ans; } } bit; const int MAX = 1e5 + 10, LOG = 17; int N, Q; vector<int> adj[MAX]; int root, h[MAX], d[MAX][LOG], mi[MAX], timer1, pos[MAX], timer2, in[MAX], out[MAX]; struct cmp { bool operator()(const int &x, const int &y) const {return pos[x] < pos[y];} }; set<int, cmp> st; void dfs(int u, int p = -1){ mi[u] = u; for(int v: adj[u]){ if(v == p) continue; h[v] = h[u] + 1; d[v][0] = u; for(int i = 1; i < LOG; i++) d[v][i] = d[d[v][i - 1]][i - 1]; dfs(v, u); minimize(mi[u], mi[v]); } } void dfs2(int u, int p = -1){ in[u] = ++timer2; sort(ALL(adj[u]), [&](int a, int b){return mi[a] < mi[b];}); for(int v: adj[u]){ if(v == p) continue; dfs2(v, u); } out[u] = timer2; pos[u] = ++timer1; } int addNode(){ int x = *st.begin(); st.erase(x); bit.upd(in[x] + 1, -1); bit.upd(out[x] + 1, 1); return x; } int delNode(int x){ int k = x, val = bit.get(in[x]); for(int i = LOG - 1; i >= 0; i--){ if(d[k][i] != 0 && bit.get(d[k][i]) == val){ k = d[k][i]; } } bit.upd(in[k] + 1, 1); bit.upd(out[k] + 1, -1); st.insert(k); return h[x] - h[k]; } void solve(){ cin >> N >> Q; for(int i = 1; i <= N; i++){ int x; cin >> x; if(x == 0) root = i; else adj[x].push_back(i); } dfs(root); dfs2(root); bit = fen(N); for(int i = 1; i <= N; i++){ bit.upd(in[i] + 1, 1); bit.upd(out[i] + 1, -1); st.insert(i); } while(Q--){ int t, x; cin >> t >> x; if(t == 1){ int ans = 0; for(int i = 1; i <= x; i++) ans = addNode(); cout << ans << '\n'; } if(t == 2){ cout << delNode(x) << '\n'; } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); // freopen("numtable.inp","r",stdin); // freopen("numtable.out","w",stdout); 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...