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

#include <bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define eb emplace_back
#define mid ((l + r) >> 1)

const int N = 1000007, inf = 0x3f3f3f3f;
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

int seg[4 * N];
void update(int p, int l, int r, int j, int x) {
  if (r < j or l > j)
    return;
  if (l == r) {
    seg[p] = x;
  } else {
    update(2 * p, l, mid, j, x);
    update(2 * p + 1, mid + 1, r, j, x);
    seg[p] = max(seg[2 * p], seg[2 * p + 1]);
  }
};

int query(int p, int l, int r, int a, int b) {
  if (r < a or l > b)
    return 0;
  if (l >= a and r <= b)
    return seg[p];
  return max(query(2 * p, l, mid, a, b), query(2 * p + 1, mid + 1, r, a, b));
}

int a[N], f[N], lis[N]; // f eh a lis
vector<int> e[N];
void solve(int u, int best, int p = -1) {

  int x = a[u];
  assert(x <= 300000);
  // cout << u << ' ' << x << endl;
  int bef = query(1, 0, 200000, 0, x - 1);
  f[u] = bef + 1;
  lis[u] = max(f[u], best);

  int aux = query(1, 0, 200000, x, x);
  update(1, 0, 200000, x, max(f[u], aux));

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

  update(1, 0, 200000, x, aux);
}

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

  int n;
  cin >> n;
  vector<int> vec;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    vec.pb(a[i]);
  }

  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());

  for (int i = 1; i <= n; ++i) {
    int j = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin();
    a[i] = j;
  }
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    e[u].pb(v);
    e[v].pb(u);
  }

  solve(1, 0, -1);

  for (int u = 1; u <= n; ++u)
    cout << lis[u] << '\\n';
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define eb emplace_back

const int N = 1000007, inf = 0x3f3f3f3f;
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

struct segtree {
  vector<int> seg;
  int n;
  segtree() {}
  segtree(int a) {
    seg.assign(a + a, 0);
    n = a;
  }

  void update(int p, int val) {
    // cout << "Updating " << p << " to " << val << endl;
    for (seg[p += n] = val; p > 1; p >>= 1) {
      seg[p >> 1] = max(seg[p], seg[p ^ 1]);
    }
  }

  int query(int l, int r) {
    int ans = 0;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        ans = max(ans, seg[l++]);
      if (r & 1)
        ans = max(ans, seg[--r]);
    }
    return ans;
  }
};

int a[N], f[N]; // f eh a lis
vector<int> e[N];

segtree seg;
void solve(int u, int p = -1) {

  int x = a[u];
  assert(x <= 100000);
  // cout << u << ' ' << x << endl;
  int bef = seg.query(0, x);
  f[u] = bef + 1;

  int aux = seg.query(x, x + 1);
  seg.update(x, max(f[u], aux));

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

  seg.update(x, aux);
}

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

  int n;
  cin >> n;
  vector<int> vec;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    vec.pb(a[i]);
  }

  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());

  for (int i = 1; i <= n; ++i) {
    a[i] = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin();
  }
  seg = segtree(n + 5);

  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    e[u].pb(v);
    e[v].pb(u);
  }

  solve(1, -1);

  for (int u = 1; u <= n; ++u)
    cout << f[u] << '\\n';
}