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

#include <bits/stdc++.h>
#define foi(i, k, n) for (int i = (int)k; i < (int)n; ++i)

using namespace std;
using ll = unsigned long long;
using ii = pair<int, int>;

constexpr int MAXN = 5 + 200000;

namespace stree {
#define m ((s + e) / 2)
int t[4 * MAXN], lazy[4 * MAXN];
int query(int s, int e, int p, int l, int r) {
  if (l > r)
    return 0; // Elemento neutro.
  if (s == l && e == r)
    return t[p];
  return max(query(s, m, 2 * p, l, min(m, r)),
             query(1 + m, e, 1 + 2 * p, max(1 + m, l), r));
}
void update(int s, int e, int p, int l, int r, int add) {
  if (l > r)
    return;
  if (s == l && e == r) {
    t[p] = add;
  } else {
    update(s, m, 2 * p, l, min(m, r), add);
    update(1 + m, e, 1 + 2 * p, max(1 + m, l), r, add);
    t[p] = max(t[2 * p], t[1 + 2 * p]);
  }
}
} // namespace stree

namespace comp {
int v[MAXN];
// Comprime todos los numeros comenzando desde BEG en O(nlgn).
void compress(int arr[], int n, int BEG = 1) {
  copy(arr, arr + n, v);
  sort(v, v + n);
  int sz = unique(v, v + n) - v;
  foi(i, 0, n) { arr[i] = BEG + lower_bound(v, v + sz, arr[i]) - v; }
}
} // namespace comp

int a[MAXN], res[MAXN], n;
vector<int> t[MAXN];

void solve(int u, int p, int rup) {
  int rbef = stree::query(1, n, 1, a[u - 1], a[u - 1]);
  int resu = 1 + stree::query(1, n, 1, 1, a[u - 1] - 1);
  bool change = false;
  if (resu > rbef) {
    change = true;
    stree::update(1, n, 1, a[u - 1], a[u - 1], resu);
  }

  rup = max(rup, resu);
  res[u] = rup;

  for (int v : t[u]) {
    if (v == p)
      continue;
    solve(v, u, rup);
  }

  if (change)
    stree::update(1, n, 1, a[u - 1], a[u - 1], rbef);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  foi(i, 0, n) cin >> a[i];
  foi(i, 1, n) {
    int u, v;
    cin >> u >> v;
    t[u].push_back(v);
    t[v].push_back(u);
  }
  comp::compress(a, n);
  solve(1, -1, 1);
  for (int i = 1; i <= n; ++i)
    cout << res[i] << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define foi(i, k, n) for (int i = (int)k; i < (int)n; ++i)

using namespace std;
using ll = unsigned long long;
using ii = pair<int, int>;

constexpr int MAXN = 5 + 200000;

namespace stree {
#define m ((s + e) / 2)
int t[4 * MAXN], lazy[4 * MAXN];
int query(int s, int e, int p, int l, int r) {
  if (l > r)
    return -(1 << 30); // Elemento neutro.
  if (s == l && e == r)
    return t[p];
  return max(query(s, m, 2 * p, l, min(m, r)),
             query(1 + m, e, 1 + 2 * p, max(1 + m, l), r));
}
void update(int s, int e, int p, int l, int r, int add) {
  if (l > r)
    return;
  if (s == l && e == r) {
    t[p] = add;
  } else {
    update(s, m, 2 * p, l, min(m, r), add);
    update(1 + m, e, 1 + 2 * p, max(1 + m, l), r, add);
    t[p] = max(t[2 * p], t[1 + 2 * p]);
  }
}
} // namespace stree

namespace comp {
int v[MAXN];
// Comprime todos los numeros comenzando desde BEG en O(nlgn).
void compress(int arr[], int n, int BEG = 1) {
  copy(arr, arr + n, v);
  sort(v, v + n);
  int sz = unique(v, v + n) - v;
  foi(i, 0, n) { arr[i] = BEG + lower_bound(v, v + sz, arr[i]) - v; }
}
} // namespace comp

int a[MAXN], res[MAXN], n;
vector<int> t[MAXN];

void solve(int u, int p, int rup) {
  int rbef = stree::query(1, n, 1, a[u], a[u]);
  int resu = 1 + stree::query(1, n, 1, 1, a[u] - 1);
  bool change = false;
  if (resu > rbef) {
    change = true;
    stree::update(1, n, 1, a[u], a[u], resu);
  }

  rup = max(rup, resu);
  res[u] = rup;

  for (int v : t[u]) {
    if (v == p)
      continue;
    solve(v, u, rup);
  }

  if (change)
    stree::update(1, n, 1, a[u], a[u], rbef);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  foi(i, 1, n) {
    int u, v;
    cin >> u >> v;
    t[u].push_back(v);
    t[v].push_back(u);
  }
  comp::compress(a, n);
  solve(1, -1, 1);
  for (int i = 1; i <= n; ++i)
    cout << res[i] << '\\n';
  return 0;
}