제출 #310726

#제출 시각아이디문제언어결과실행 시간메모리
310726VROOM_VARUNBall Machine (BOI13_ballmachine)C++14
100 / 100
351 ms37172 KiB
/*
ID: varunra2
LANG: C++
TASK: ballmachine
*/

#include <bits/stdc++.h>
using namespace std;

#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
  cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif

#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second

const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions

int n, q;
const int siz = 20;
VI par;
VVI adj;
VI mn;
VI d;
VVI lca;
VI ord;
vector<bool> vis;
int root = 0;

void init() {
  par.resize(n);
  adj.resize(n);
  mn.resize(n);
  d.resize(n);
  lca.resize(n);
  vis.assign(n, false);
  for (int i = 0; i < n; i++) {
    lca[i].resize(siz);
  }
}

void dfs1(int u, int v) {
  lca[u][0] = v;
  for (int i = 1; i < siz; i++) {
    int to = lca[u][i - 1];
    if (to == -1)
      lca[u][i] = -1;
    else
      lca[u][i] = lca[to][i - 1];
  }
  mn[u] = u;
  for (auto& x : adj[u]) {
    if (x == v) continue;
    d[x] = d[u] + 1;
    dfs1(x, u);
    mn[u] = min(mn[u], mn[x]);
  }
}

void dfs2(int u, int v) {
  VII frst;
  trav(x, adj[u]) {
    if (x == v) continue;
    frst.PB(MP(mn[x], x));
  }

  sort(all(frst));

  trav(x, frst) { dfs2(x.y, u); }
  ord.PB(u);
}

int main() {
// #ifndef ONLINE_JUDGE
  // freopen("ballmachine.in", "r", stdin);
  // freopen("ballmachine.out", "w", stdout);
// #endif
  cin.sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> q;

  init();

  for (int i = 0; i < n; i++) {
    cin >> par[i];
    par[i]--;
  }

  root = min_element(all(par)) - par.begin();

  for (int i = 0; i < n; i++) {
    if (~par[i]) {
      adj[i].PB(par[i]);
      adj[par[i]].PB(i);
    }
  }

  dfs1(root, -1);
  dfs2(root, -1);

  set<PII> empt;
  // set<PII> use;

  VI to(n);
  rep(i, 0, n) { to[ord[i]] = i; }

  for (int i = 0; i < n; i++) {
    empt.insert(MP(i, ord[i]));
  }


  while (q--) {
    int type;
    cin >> type;
    if (type == 1) {
      int k;
      cin >> k;
      int lst;
      while (k--) {
        // we need to insert a ball
        auto it = empt.begin();
        PII cur = *it;
        empt.erase(it);
        lst = cur.y;
        vis[lst] = true;
      }
      cout << lst + 1 << '\n';
    } else {
      int u;
      cin >> u;
      u--;
      int ret = 0;
      for (int i = siz - 1; i >= 0; i--) {
        if (lca[u][i] != -1 and vis[lca[u][i]]) {
          ret += (1 << i);
          u = lca[u][i];
        }
      }
      cout << ret << '\n';
      vis[u] = false;
      empt.insert(MP(to[u], u));
    }
  }

  return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

ballmachine.cpp: In function 'int main()':
ballmachine.cpp:164:26: warning: 'lst' may be used uninitialized in this function [-Wmaybe-uninitialized]
  164 |       cout << lst + 1 << '\n';
      |                          ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...