Submission #1093125

#TimeUsernameProblemLanguageResultExecution timeMemory
1093125juicyBall Machine (BOI13_ballmachine)C++17
100 / 100
99 ms22432 KiB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 1e5 + 5, LG = 17;

int n, q;
int a[N], b[N], pr[LG][N];
bool alive[N];
vector<int> g[N];

int ind;

void dfs(int u, bool keep) {
  if (keep) {
    sort(g[u].begin(), g[u].end(), [&](int i, int j) {
      return a[i] < a[j];
    });
    for (int v : g[u]) {
      dfs(v, 1);
    }
    b[u] = ++ind;
    return;
  }
  a[u] = u;
  for (int v : g[u]) {
    dfs(v, 0);
    a[u] = min(a[u], a[v]);
  }
}

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    cin >> pr[0][i];
    g[pr[0][i]].push_back(i);
  }
  for (int j = 1; j < LG; ++j) {
    for (int i = 1; i <= n; ++i) {
      pr[j][i] = pr[j - 1][pr[j - 1][i]];
    }
  }
  dfs(0, 0);
  dfs(0, 1);
  priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
  for (int i = 1; i <= n; ++i) {
    pq.push({b[i], i});
  }
  while (q--) {
    int t; cin >> t;
    if (t == 1) {
      int k; cin >> k;
      int lst = 0;
      while (k--) {
        auto [x, u] = pq.top(); pq.pop();
        lst = u;
        alive[u] = 1;
      }
      cout << lst << "\n";
    } else {
      int u; cin >> u;
      int res = 0;
      for (int j = LG - 1; ~j; --j) {
        if (alive[pr[j][u]]) {
          u = pr[j][u];
          res += 1 << j;
        }
      }
      alive[u] = 0;
      pq.push({b[u], u});
      cout << res << "\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...