Problem p02698 (C++) — buggy submission, verdict: Runtime Error

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> struct Segtree {
  int n;
  T op;
  vector<T> elm;
  function<T(T, T)> f;

  Segtree(int n, T init, function<T(T, T)> f, T op = T())
      : n(n), op(op), elm(2 * n, init), f(f) {
    for (int i = n - 1; i >= 1; --i)
      elm[i] = f(elm[2 * i], elm[2 * i + 1]);
  }

  Segtree(int n, vector<T> init, function<T(T, T)> f, T op = T())
      : n(n), op(op), elm(2 * n), f(f) {
    for (int i = 0; i < n; ++i)
      elm[i + n] = init[i];
    for (int i = n - 1; i >= 1; --i)
      elm[i] = f(elm[2 * i], elm[2 * i + 1]);
  }

  void set(int x, T val) {
    x += n;
    elm[x] = val;
    while (x >>= 1)
      elm[x] = f(elm[2 * x], elm[2 * x + 1]);
  }

  void update(int x, T val) {
    x += n;
    elm[x] = f(elm[x], val);
    while (x >>= 1)
      elm[x] = f(elm[2 * x], elm[2 * x + 1]);
  }

  T get(int x, int y) const {
    T l = op, r = op;
    for (x += n, y += n - 1; x <= y; x >>= 1, y >>= 1) {
      if (x & 1)
        l = f(l, elm[x++]);
      if (!(y & 1))
        r = f(elm[y--], r);
    }
    return f(l, r);
  }
};

void solve(long long n, std::vector<long long> a, std::vector<long long> u,
           std::vector<long long> v) {
  vector<vector<int>> edges(n);
  for (int i = 0; i < n - 1; ++i) {
    --u[i], --v[i];
    edges[u[i]].emplace_back(v[i]);
    edges[v[i]].emplace_back(u[i]);
  }
  vector<vector<int>> childs(n);
  vector<int> depth(n, -1);
  function<void(int)> dfs = [&](int x) {
    for (auto y : edges[x])
      if (depth[y] == -1) {
        depth[y] = depth[x] + 1;
        childs[x].push_back(y);
        dfs(y);
      }
  };
  depth[0] = 0;
  dfs(0);

  vector<pair<int, int>> va;
  for (int i = 0; i < n; ++i)
    va.emplace_back(a[i], i);
  sort(va.begin(), va.end());
  sort(a.begin(), a.end());
  vector<int> node_idx(n);
  vector<int> ch_idx(n);
  for (int i = 0; i < n; ++i) {
    node_idx[va[i].second] = i;
    ch_idx[va[i].second] =
        distance(a.begin(), lower_bound(a.begin(), a.end(), va[i].first));
  }

  Segtree<int> seg(
      n, 0, [](auto x, auto y) { return max(x, y); }, 0);
  vector<int> ans(n, -MOD);
  function<void(int)> f = [&](int x) {
    i64 res = seg.get(0, ch_idx[x]) + 1;
    seg.set(node_idx[x], res);
    for (auto y : childs[x])
      f(y);
    ans[x] = seg.elm[1];
    seg.set(node_idx[x], 0);
  };
  f(0);
  for (int i = 0; i < n; ++i)
    cout << ans[i] << endl;
}

signed main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> a(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
  }
  std::vector<long long> u(N - 1);
  std::vector<long long> v(N - 1);
  for (int i = 0; i < N - 1; i++) {
    scanf("%lld", &u[i]);
    scanf("%lld", &v[i]);
  }
  solve(N, std::move(a), std::move(u), std::move(v));
  return 0;
}


Error: 0

Fixed submission:

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> struct Segtree {
  int n;
  T op;
  vector<T> elm;
  function<T(T, T)> f;

  Segtree(int n, T init, function<T(T, T)> f, T op = T())
      : n(n), op(op), elm(2 * n, init), f(f) {
    for (int i = n - 1; i >= 1; --i)
      elm[i] = f(elm[2 * i], elm[2 * i + 1]);
  }

  Segtree(int n, vector<T> init, function<T(T, T)> f, T op = T())
      : n(n), op(op), elm(2 * n), f(f) {
    for (int i = 0; i < n; ++i)
      elm[i + n] = init[i];
    for (int i = n - 1; i >= 1; --i)
      elm[i] = f(elm[2 * i], elm[2 * i + 1]);
  }

  void set(int x, T val) {
    x += n;
    elm[x] = val;
    while (x >>= 1)
      elm[x] = f(elm[2 * x], elm[2 * x + 1]);
  }

  void update(int x, T val) {
    x += n;
    elm[x] = f(elm[x], val);
    while (x >>= 1)
      elm[x] = f(elm[2 * x], elm[2 * x + 1]);
  }

  T get(int x, int y) const {
    T l = op, r = op;
    for (x += n, y += n - 1; x <= y; x >>= 1, y >>= 1) {
      if (x & 1)
        l = f(l, elm[x++]);
      if (!(y & 1))
        r = f(elm[y--], r);
    }
    return f(l, r);
  }
};

void solve(long long n, std::vector<long long> a, std::vector<long long> u,
           std::vector<long long> v) {
  vector<vector<int>> edges(n);
  for (int i = 0; i < n; ++i) {
    --u[i], --v[i];
    edges[u[i]].emplace_back(v[i]);
    edges[v[i]].emplace_back(u[i]);
  }
  vector<vector<int>> childs(n);
  vector<int> depth(n, -1);
  function<void(int)> dfs = [&](int x) {
    for (auto y : edges[x])
      if (depth[y] == -1) {
        depth[y] = depth[x] + 1;
        childs[x].push_back(y);
        dfs(y);
      }
  };
  depth[0] = 0;
  dfs(0);

  vector<pair<int, int>> va;
  for (int i = 0; i < n; ++i)
    va.emplace_back(a[i], i);
  sort(va.begin(), va.end());
  sort(a.begin(), a.end());
  vector<int> node_idx(n);
  vector<int> ch_idx(n);
  for (int i = 0; i < n; ++i) {
    node_idx[va[i].second] = i;
    ch_idx[va[i].second] =
        distance(a.begin(), lower_bound(a.begin(), a.end(), va[i].first));
  }

  Segtree<int> seg(
      n, 0, [](auto x, auto y) { return max(x, y); }, 0);
  vector<int> ans(n, -MOD);
  function<void(int)> f = [&](int x) {
    i64 res = seg.get(0, ch_idx[x]) + 1;
    seg.set(node_idx[x], res);
    for (auto y : childs[x])
      f(y);
    ans[x] = res;
    seg.set(node_idx[x], 0);
  };
  f(0);
  for (int i = 0; i < n; ++i)
    cout << ans[i] << endl;
}

signed main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> a(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
  }
  std::vector<long long> u(N - 1);
  std::vector<long long> v(N - 1);
  for (int i = 0; i < N - 1; i++) {
    scanf("%lld", &u[i]);
    scanf("%lld", &v[i]);
  }
  solve(N, std::move(a), std::move(u), std::move(v));
  return 0;
}