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

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 4e5 + 10;
#define fi first
#define se second
#define pb push_back
int n, a[N], b[N];
vector<int> v[N];
int t[N << 2];
#define mid (l + r >> 1)
#define ls o << 1
#define rs o << 1 | 1
void up(int o, int l, int r, int p, int d) {
  if (l == r) {
    t[o] = d;
    return;
  }
  if (p <= mid)
    up(ls, l, mid, p, d);
  else
    up(rs, mid + 1, r, p, d);
  t[o] = max(t[ls], t[rs]);
}
int get(int o, int l, int r, int x, int y) {
  if (x <= l && r <= y)
    return t[o];
  int z = 0;
  if (x <= mid)
    z = max(z, get(ls, l, mid, x, y));
  if (y > mid)
    z = max(z, get(rs, mid + 1, r, x, y));
  return z;
}
int get(int o, int l, int r, int x) {
  if (l == r)
    return t[o];
  int z = 0;
  if (x <= mid)
    return get(ls, l, mid, x);
  else
    return get(rs, mid + 1, r, x);
}
int len, cnt, c[N], ans[N];
void dfs(int x, int y) {
  int pre = get(1, 1, len, a[x]);
  int nx = get(1, 1, len, 1, c[x]) + 1;
  ans[x] = max(ans[y], nx);
  up(1, 1, len, a[x], nx);
  for (auto k : v[x]) {
    if (k == y)
      continue;
    dfs(k, x);
  }
  up(1, 1, len, a[x], pre);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i], b[++cnt] = a[i], b[++cnt] = a[i] - 1;
  sort(b + 1, b + 1 + cnt);
  len = unique(b + 1, b + 1 + cnt) - b - 1;
  for (int i = 1; i <= n; i++) {
    c[i] = lower_bound(b + 1, b + 1 + len, a[i] - 1) - b;
    a[i] = lower_bound(b + 1, b + 1 + len, a[i]) - b;
  }
  for (int i = 1; i < n; i++) {
    int s, t;
    cin >> s >> t;
    v[s].pb(t);
    v[t].pb(s);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
int n, a[N], b[N];
vector<int> v[N];
int t[N << 2];
#define mid (l + r >> 1)
#define ls o << 1
#define rs o << 1 | 1
void up(int o, int l, int r, int p, int d) {
  if (l == r) {
    t[o] = d;
    return;
  }
  if (p <= mid)
    up(ls, l, mid, p, d);
  else
    up(rs, mid + 1, r, p, d);
  t[o] = max(t[ls], t[rs]);
}
int get(int o, int l, int r, int x, int y) {
  if (x <= l && r <= y)
    return t[o];
  int z = 0;
  if (x <= mid)
    z = max(z, get(ls, l, mid, x, y));
  if (y > mid)
    z = max(z, get(rs, mid + 1, r, x, y));
  return z;
}
int get(int o, int l, int r, int x) {
  if (l == r)
    return t[o];
  int z = 0;
  if (x <= mid)
    return get(ls, l, mid, x);
  else
    return get(rs, mid + 1, r, x);
}
int len, cnt, c[N], ans[N];
void dfs(int x, int y) {
  int pre = get(1, 1, len, a[x]);
  int nx = get(1, 1, len, 1, c[x]) + 1;
  ans[x] = nx;
  up(1, 1, len, a[x], nx);
  for (auto k : v[x]) {
    if (k == y)
      continue;
    dfs(k, x);
  }
  up(1, 1, len, a[x], pre);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i], b[++cnt] = a[i], b[++cnt] = a[i] - 1;
  sort(b + 1, b + 1 + cnt);
  len = unique(b + 1, b + 1 + cnt) - b - 1;
  for (int i = 1; i <= n; i++) {
    c[i] = lower_bound(b + 1, b + 1 + len, a[i] - 1) - b;
    a[i] = lower_bound(b + 1, b + 1 + len, a[i]) - b;
  }
  for (int i = 1; i < n; i++) {
    int s, t;
    cin >> s >> t;
    v[s].pb(t);
    v[t].pb(s);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << '\\n';
  return 0;
}