Submission #648617

#TimeUsernameProblemLanguageResultExecution timeMemory
648617ghostwriterBall Machine (BOI13_ballmachine)C++14
100 / 100
153 ms23484 KiB
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include <debug.h> #else #define debug(...) #endif #define ft front #define bk back #define st first #define nd second #define ins insert #define ers erase #define pb push_back #define pf push_front #define _pb pop_back #define _pf pop_front #define lb lower_bound #define ub upper_bound #define mtp make_tuple #define bg begin #define ed end #define all(x) (x).bg(), (x).ed() #define sz(x) (int)(x).size() typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ldb; typedef pair<int, int> pi; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll; typedef string str; template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); } template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i)) #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i)) #define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i)) #define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i)) #define EACH(i, x) for (auto &(i) : (x)) #define WHILE while #define file "TEST" mt19937 rd(chrono::steady_clock::now().time_since_epoch().count()); ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); } /* ---------------------------------------------------------------- END OF TEMPLATE ---------------------------------------------------------------- Tran The Bao - ghostwriter Training for VOI23 gold medal ---------------------------------------------------------------- DIT ME CHUYEN BAO LOC ---------------------------------------------------------------- */ const int N = 1e5 + 5; int n, q, minn[N], h[N], s[N], tout[N], itout[N], p[N][17], timer = 0, root = 0, f[N], f1[N]; vi adj[N]; void dfs(int u) { minn[u] = u; EACH(v, adj[u]) { dfs(v); minn[u] = min(minn[u], minn[v]); } } bool cmp(int a, int b) { return minn[a] < minn[b]; } void dfs1(int u) { FOR(j, 1, 16) ::p[u][j] = ::p[::p[u][j - 1]][j - 1]; s[u] = 1; sort(all(adj[u]), cmp); EACH(v, adj[u]) { h[v] = h[u] + 1; p[v][0] = u; dfs1(v); s[u] += s[v]; } tout[u] = ++timer; } int getn(int a, int h) { FOR(i, 0, 16) if (h & (1 << i)) a = p[a][i]; return a; } void upd(int pos, int v) { for (int i = pos; i <= n; i += (i & -i)) f[i] += v; } int fempty() { int cur = 0, cnt = 0, ans = n + 1; FOS(i, 16, 0) { if (cur + (1 << i) > n) continue; if (cnt + f[cur + (1 << i)] < cur + (1 << i)) ans = min(ans, cur + (1 << i)); else { cur |= 1 << i; cnt += f[cur]; } } return ans; } void upd1(int pos, int v) { for (int i = pos; i <= n; i += (i & -i)) f1[i] += v; } void upd1(int l, int r, int v) { upd1(l, v); upd1(r + 1, -v); } int get1(int pos) { int rs = 0; for (int i = pos; i >= 1; i -= (i & -i)) rs += f1[i]; return rs; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); // freopen(file".inp", "r", stdin); // freopen(file".out", "w", stdout); cin >> n >> q; FOR(i, 1, n) { int p; cin >> p; if (p) adj[p].pb(i); else root = i; } dfs(root); dfs1(root); FOR(i, 1, n) itout[tout[i]] = i; WHILE(q--) { int t; cin >> t; if (t == 1) { int k, last = 0; cin >> k; FOR(i, 1, k) { int pos = itout[fempty()]; upd(tout[pos], 1); upd1(tout[pos] - s[pos] + 1, tout[pos], 1); last = pos; } cout << last << '\n'; continue; } int x; cin >> x; int h = get1(tout[x]); cout << h - 1 << '\n'; int tnode = getn(x, h - 1); upd(tout[tnode], -1); upd1(tout[tnode] - s[tnode] + 1, tout[tnode], -1); } return 0; } /* 8 2 0 1 2 2 3 3 4 6 1 8 2 5 ---------------------------------------------------------------- From Benq: stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH ---------------------------------------------------------------- */

Compilation message (stderr)

ballmachine.cpp: In function 'void dfs(int)':
ballmachine.cpp:36:31: warning: unnecessary parentheses in declaration of 'v' [-Wparentheses]
   36 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
ballmachine.cpp:56:2: note: in expansion of macro 'EACH'
   56 |  EACH(v, adj[u]) {
      |  ^~~~
ballmachine.cpp: In function 'void dfs1(int)':
ballmachine.cpp:32:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
ballmachine.cpp:63:2: note: in expansion of macro 'FOR'
   63 |  FOR(j, 1, 16) ::p[u][j] = ::p[::p[u][j - 1]][j - 1];
      |  ^~~
ballmachine.cpp:36:31: warning: unnecessary parentheses in declaration of 'v' [-Wparentheses]
   36 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
ballmachine.cpp:66:2: note: in expansion of macro 'EACH'
   66 |  EACH(v, adj[u]) {
      |  ^~~~
ballmachine.cpp: In function 'int getn(int, int)':
ballmachine.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
ballmachine.cpp:75:2: note: in expansion of macro 'FOR'
   75 |  FOR(i, 0, 16)
      |  ^~~
ballmachine.cpp: In function 'int fempty()':
ballmachine.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   33 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
      |                               ^
ballmachine.cpp:83:2: note: in expansion of macro 'FOS'
   83 |  FOS(i, 16, 0) {
      |  ^~~
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
ballmachine.cpp:101:5: note: in expansion of macro 'FOR'
  101 |     FOR(i, 1, n) {
      |     ^~~
ballmachine.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
ballmachine.cpp:109:5: note: in expansion of macro 'FOR'
  109 |     FOR(i, 1, n) itout[tout[i]] = i;
      |     ^~~
ballmachine.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
ballmachine.cpp:116:7: note: in expansion of macro 'FOR'
  116 |       FOR(i, 1, k) {
      |       ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...