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

#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define FPEN freopen("in.txt", "r", stdin);
#else
#define FPEN
#endif
const int maxn = 200009;
typedef long long i64;
typedef pair<i64, i64> pi64;
typedef pair<int, int> pi;
const i64 MOD = 119 << 23 | 1;
class {
public:
  int a[maxn];
  vector<int> V[maxn];
  int n;
  int m;
  void solve() {
    cin >> n;
    vector<int> b;
    for (int i = 1; i <= n; ++i) {
      cin >> a[i];
      b.push_back(a[i]);
    }
    sort(b.begin(), b.end());
    b.erase(unique(b.begin(), b.end()), b.end());
    for (int i = 1; i <= n; ++i) {
      a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin() + 2;
      m = max(a[i], m);
    }
    for (int i = 1; i < n; ++i) {
      int u, v;
      cin >> u >> v;
      V[u].push_back(v);
      V[v].push_back(u);
    }
    ans[1] = 1;
    upd(1, 1, m, a[1], 1);
    dfs(1, 1);
    for (int i = 1; i <= n; ++i) {
      cout << ans[i] << '\\n';
    }
  }
  int ans[maxn];
  void dfs(int u, int f) {
    for (int v : V[u]) {
      if (v == f)
        continue;
      int dpv = query(1, 1, m, 1, a[v] - 1) + 1;
      ans[v] = max(ans[u], dpv);
      int preval = query(1, 1, m, a[v], a[v]);
      upd(1, 1, m, a[v], max(preval, dpv));
      dfs(v, u);
      upd(1, 1, m, a[v], preval);
    }
  }
  int Max[maxn << 3];
  int query(int rt, int l, int r, int ll, int rr) {
    if (ll <= l && rr >= r)
      return Max[rt];
    int res = 0;
    int mid = l + r >> 1;
    if (ll <= mid)
      res = max(query(rt << 1, l, mid, ll, rr), res);
    if (rr > mid)
      res = max(res, query(rt << 1 | 1, mid + 1, r, ll, rr));
    return res;
  }
  void upd(int rt, int l, int r, int p, int v) {
    if (l == r) {
      Max[rt] = v;
      return;
    }
    int mid = l + r >> 1;
    if (p <= mid)
      upd(rt << 1, l, mid, p, v);
    else
      upd(rt << 1 | 1, mid + 1, r, p, v);
    Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]);
  }

} NSPACE;
int main() {
  FPEN;
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  NSPACE.solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define FPEN freopen("in.txt", "r", stdin);
#else
#define FPEN
#endif
const int maxn = 200009;
typedef long long i64;
typedef pair<i64, i64> pi64;
typedef pair<int, int> pi;
const i64 MOD = 119 << 23 | 1;
class {
public:
  int a[maxn];
  vector<int> V[maxn];
  int n;
  void solve() {
    cin >> n;
    vector<int> b;
    for (int i = 1; i <= n; ++i) {
      cin >> a[i];
      b.push_back(a[i]);
    }
    sort(b.begin(), b.end());
    b.erase(unique(b.begin(), b.end()), b.end());
    for (int i = 1; i <= n; ++i) {
      a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin() + 1;
    }
    for (int i = 1; i < n; ++i) {
      int u, v;
      cin >> u >> v;
      V[u].push_back(v);
      V[v].push_back(u);
    }
    ans[1] = 1;
    upd(1, 1, n, a[1], 1);
    dfs(1, 1);
    for (int i = 1; i <= n; ++i) {
      cout << ans[i] << '\\n';
    }
  }
  int ans[maxn];
  void dfs(int u, int f) {
    for (int v : V[u]) {
      if (v == f)
        continue;
      ans[v] = query(1, 1, n, 1, a[v] - 1) + 1;
      int preval = query(1, 1, n, a[v], a[v]);
      upd(1, 1, n, a[v], max(preval, ans[v]));
      dfs(v, u);
      upd(1, 1, n, a[v], preval);
    }
  }
  int Max[maxn << 3];
  int query(int rt, int l, int r, int ll, int rr) {
    if (ll <= l && rr >= r)
      return Max[rt];
    int res = 0;
    int mid = l + r >> 1;
    if (ll <= mid)
      res = max(query(rt << 1, l, mid, ll, rr), res);
    if (rr > mid)
      res = max(res, query(rt << 1 | 1, mid + 1, r, ll, rr));
    return res;
  }
  void upd(int rt, int l, int r, int p, int v) {
    if (l == r) {
      Max[rt] = v;
      return;
    }
    int mid = l + r >> 1;
    if (p <= mid)
      upd(rt << 1, l, mid, p, v);
    else
      upd(rt << 1 | 1, mid + 1, r, p, v);
    Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]);
  }

} NSPACE;
int main() {
  FPEN;
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  NSPACE.solve();
}