Problem p03204 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <time.h>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define MOD 998244353LL
#define INF 1000000000LL
#define EPS 1e-10
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define DUMP(a)                                                                \
  REP(d, a.size()) {                                                           \
    cout << a[d];                                                              \
    if (d != a.size() - 1)                                                     \
      cout << " ";                                                             \
    else                                                                       \
      cout << endl;                                                            \
  }
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v)                                                              \
  sort(ALL(v));                                                                \
  v.erase(unique(ALL(v)), v.end());
#define pb push_back

vector<vector<ll>> g;
vector<ll> weight;
vector<ll> ans;

constexpr int MAX_SIZE = 505050;
template <typename T> class SegTree {
private:
  int n;
  const function<T(T, T)> op; // 演算
  const T ie;                 // 演算の単位元
  T seq[MAX_SIZE];

public:
  /// op: 演算, ie: 演算の単位元
  SegTree(int _n, function<T(T, T)> op, const T ie) : op(op), ie(ie) {
    n = 1;
    while (n < _n)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; i++)
      seq[i] = ie;
  }

  /// k 番目(0-indexed)の要素を e で更新
  void update(ll k, const T e) {
    k += n - 1;
    seq[k] = e;
    while (k > 0) {
      k = (k - 1) / 2;
      seq[k] = op(seq[k * 2 + 1], seq[k * 2 + 2]);
    }
  }

  // k 番目(0-indexed)の要素を取得
  T get(ll k) {
    k += n - 1;
    return seq[k];
  }

  /// [a, b) 番目(0-indexed)の要素全体の演算結果を返す
  T query(ll a, ll b, int k = 0, int l = 0, int r = -1) {
    if (r == -1)
      r = n;
    if (r <= a || b <= l)
      return ie;
    if (a <= l && r <= b)
      return seq[k];
    T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return op(vl, vr);
  }
};
// [Range Maximum Query] op: [] (long long a, long long b) { return max(a, b);
// }, ie: -1e18

void dfs(ll pos, ll par, ll dpt, ll maxi_pos, SegTree<ll> &seg) {
  seg.update(dpt, pos);
  if (seg.get(maxi_pos) > pos) {
    if (seg.query(0, maxi_pos) > seg.query(maxi_pos + 1, dpt + 1)) {
      weight[seg.get(maxi_pos)]++;
    } else
      weight[seg.get(maxi_pos + 1)]++;
  } else
    weight[pos]++;
  REP(i, g[pos].size()) {
    if (g[pos][i] == par)
      continue;
    if (seg.get(maxi_pos) < pos) {
      dfs(g[pos][i], pos, dpt + 1, dpt, seg);
    } else {
      dfs(g[pos][i], pos, dpt + 1, maxi_pos, seg);
    }
  }
  seg.update(dpt, -1);
  return;
}

void calc(ll pos, ll par, ll cost) {
  cost += weight[pos];
  if (pos != 0)
    ans[pos - 1] = cost - 1;
  REP(i, g[pos].size()) {
    if (g[pos][i] == par)
      continue;
    calc(g[pos][i], pos, cost);
  }
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  g.resize(n);
  weight.assign(n, 0);
  REP(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].pb(b);
    g[b].pb(a);
  }
  SegTree<ll> seg(
      n + 1, [](ll a, ll b) { return max(a, b); }, -1);
  dfs(0, -1, 1, 0, seg);
  ans.resize(n - 1);
  calc(0, -1, 0);
  DUMP(ans);
  return 0;
}

/* --------------------------------------- */


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <time.h>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define MOD 998244353LL
#define INF 1000000000LL
#define EPS 1e-10
#define FOR(i, n, m) for (ll i = n; i < (ll)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define DUMP(a)                                                                \
  REP(d, a.size()) {                                                           \
    cout << a[d];                                                              \
    if (d != a.size() - 1)                                                     \
      cout << " ";                                                             \
    else                                                                       \
      cout << endl;                                                            \
  }
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v)                                                              \
  sort(ALL(v));                                                                \
  v.erase(unique(ALL(v)), v.end());
#define pb push_back

vector<vector<ll>> g;
vector<ll> weight;
vector<ll> ans;

constexpr int MAX_SIZE = 1000010;
template <typename T> class SegTree {
private:
  int n;
  const function<T(T, T)> op; // 演算
  const T ie;                 // 演算の単位元
  T seq[MAX_SIZE];

public:
  /// op: 演算, ie: 演算の単位元
  SegTree(int _n, function<T(T, T)> op, const T ie) : op(op), ie(ie) {
    n = 1;
    while (n < _n)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; i++)
      seq[i] = ie;
  }

  /// k 番目(0-indexed)の要素を e で更新
  void update(ll k, const T e) {
    k += n - 1;
    seq[k] = e;
    while (k > 0) {
      k = (k - 1) / 2;
      seq[k] = op(seq[k * 2 + 1], seq[k * 2 + 2]);
    }
  }

  // k 番目(0-indexed)の要素を取得
  T get(ll k) {
    k += n - 1;
    return seq[k];
  }

  /// [a, b) 番目(0-indexed)の要素全体の演算結果を返す
  T query(ll a, ll b, int k = 0, int l = 0, int r = -1) {
    if (r == -1)
      r = n;
    if (r <= a || b <= l)
      return ie;
    if (a <= l && r <= b)
      return seq[k];
    T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return op(vl, vr);
  }
};
// [Range Maximum Query] op: [] (long long a, long long b) { return max(a, b);
// }, ie: -1e18

void dfs(ll pos, ll par, ll dpt, ll maxi_pos, SegTree<ll> &seg) {
  seg.update(dpt, pos);
  if (seg.get(maxi_pos) > pos) {
    if (seg.query(0, maxi_pos) > seg.query(maxi_pos + 1, dpt + 1)) {
      weight[seg.get(maxi_pos)]++;
    } else
      weight[seg.get(maxi_pos + 1)]++;
  } else
    weight[pos]++;
  REP(i, g[pos].size()) {
    if (g[pos][i] == par)
      continue;
    if (seg.get(maxi_pos) < pos) {
      dfs(g[pos][i], pos, dpt + 1, dpt, seg);
    } else {
      dfs(g[pos][i], pos, dpt + 1, maxi_pos, seg);
    }
  }
  seg.update(dpt, -1);
  return;
}

void calc(ll pos, ll par, ll cost) {
  cost += weight[pos];
  if (pos != 0)
    ans[pos - 1] = cost - 1;
  REP(i, g[pos].size()) {
    if (g[pos][i] == par)
      continue;
    calc(g[pos][i], pos, cost);
  }
  return;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  g.resize(n);
  weight.assign(n, 0);
  REP(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].pb(b);
    g[b].pb(a);
  }
  SegTree<ll> seg(
      n + 1, [](ll a, ll b) { return max(a, b); }, -1);
  dfs(0, -1, 1, 0, seg);
  ans.resize(n - 1);
  calc(0, -1, 0);
  DUMP(ans);
  return 0;
}

/* --------------------------------------- */