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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1e9;
struct SegmentTree {
private:
  int n;
  vector<int> node;

public:
  SegmentTree() {
    int sz = 200010;
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1, -INF);
    for (int i = 0; i < sz; i++)
      node[i + n - 1] = -INF;
    for (int i = n - 2; i >= 0; i--)
      node[i] = max(node[2 * i + 1], node[2 * i + 2]);
  }

  void update(int x, int val) {
    x += (n - 1);
    node[x] = val;
    while (x > 0) {
      x = (x - 1) / 2;
      node[x] = max(node[2 * x + 1], node[2 * x + 2]);
    }
  }
  // hannkaikukann
  int getmax(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    if (r <= a || b <= l)
      return -INF;
    if (a <= l && r <= b)
      return node[k];

    int vl = getmax(a, b, 2 * k + 1, l, (l + r) / 2);
    int vr = getmax(a, b, 2 * k + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
};

// const ll mod = 1000000007;
int N;
SegmentTree seg;
vector<ll> paths[201000];
ll ans[201000];
vector<int> a;
void dfs(int now, int p) {
  ll tmp = seg.getmax(0, a[now]);
  tmp++;
  ll before = seg.getmax(a[now], a[now] + 1);
  chmax(tmp, before);
  seg.update(a[now], tmp);
  ans[now] = seg.getmax(0, N + 1);
  for (auto to : paths[now]) {
    if (to == p)
      continue;
    dfs(to, now);
  }
  seg.update(a[now], before);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  vector<int> cmp;
  a.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    cmp.push_back(a[i]);
  }
  cmp.push_back(-1);
  sort(cmp.begin(), cmp.end());
  cmp.erase(unique(cmp.begin(), cmp.end()), cmp.end());
  for (int i = 0; i < N; i++) {
    auto itr = lower_bound(cmp.begin(), cmp.end(), a[i]);
    a[i] = itr - cmp.begin();
  }
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    paths[a].push_back(b);
    paths[b].push_back(a);
  }
  seg.update(0, 0);
  dfs(0, -1);
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repo(i, n) for (int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define lb(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define ub(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
using Pair = pair<ll, pair<int, int>>;
#define pq priority_queue<Pair, vector<Pair>, greater<Pair>>
const ll mod = 1000000007;
// const ll mod=998244353;
const ld pi = acos(-1.0);
const ll INF = 1LL << 61;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
// intの最大値2147483647 ≒ 2×10^9
// long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//   cout << fixed << setprecision (20);   小数点以下2０桁まで
// 実行時間制約2秒では２×10^8回くらいまで計算できる

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

  int n;
  cin >> n;

  vector<ll> p(n);
  rep(i, n) { cin >> p[i]; }

  vector<vector<int>> g(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].pb(b);
    g[b].pb(a);
  }

  vector<ll> ans(n, INF);
  vector<int> che(n, -1);
  deque<pair<int, vector<ll>>> que;
  int sa = lb(ans, p[0]);
  ans[sa] = p[0];
  che[0] = lb(ans, INF);
  que.pb({0, ans});

  while (!que.empty()) {
    auto z = que.front();
    int v = z.fi;
    auto y = z.se;
    que.pop_front();

    for (auto nv : g[v]) {
      if (che[nv] != -1)
        continue;
      int x = lb(y, p[nv]);
      ll memo = y[x];
      y[x] = p[nv];
      che[nv] = lb(y, INF);
      que.pb({nv, y});
      y[x] = memo;
    }
  }

  rep(i, n) { cout << che[i] << endl; }
}