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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

vector<int> adj[200010];
int n, dp[800100], A[200010];

void build(int v, int tl, int tr) {
  if (tl == tr) {
    dp[v] = 0;
  } else {
    int tm = (tl + tr) / 2;
    build(v * 2, tl, tm);
    build(v * 2 + 1, tm + 1, tr);
    dp[v] = max(dp[v * 2], dp[v * 2 + 1]);
  }
}

int query(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    return dp[v];
  }
  int tm = (tl + tr) / 2;
  return max(query(v * 2, tl, tm, l, min(r, tm)),
             query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}

void update(int v, int tl, int tr, int pos, int new_val) {
  if (tl == tr) {
    dp[v] = new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2, tl, tm, pos, new_val);
    else
      update(v * 2 + 1, tm + 1, tr, pos, new_val);
    dp[v] = max(dp[v * 2], dp[v * 2 + 1]);
  }
}

int lis[200010];

struct CoordinateCompression {
  vector<ll> cc{};

  void insert(ll x) { cc.push_back(x); }

  void make() {
    sort(cc.begin(), cc.end());
    cc.resize(unique(cc.begin(), cc.end()) - cc.begin());
  }

  int get_pos(ll x) {
    return lower_bound(cc.begin(), cc.end(), x) - cc.begin();
  }

  ll get_val(int p) { return cc[p]; }
};

CoordinateCompression C;

void dfs(int u, int p) {
  // update dp pos
  int pos = C.get_pos(A[u]);
  int prev = query(1, 0, n - 1, pos, pos);

  int mx = query(1, 0, n - 1, 0, pos - 1) + 1;
  update(1, 0, n - 1, pos, mx);

  //    cout << "u " << u << endl;
  //    cout << "pos " << pos << endl;
  //    cout << "mx " << mx << endl;
  //    cout << endl;

  lis[u] = max(mx, query(1, 0, n - 1, 0, n - 1));

  for (auto v : adj[u]) {
    if (v == p)
      continue;
    dfs(v, u);
  }

  // rollback dp pos
  update(1, 0, n - 1, pos, prev);
}

int main() {
  ios_base::sync_with_stdio(false);

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> A[i];
    C.insert(A[i]);
  }

  C.make();

  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;

    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  dfs(0, -1);
  build(1, 0, n - 1);

  for (int i = 0; i < n; i++)
    cout << lis[i] << endl;
  cout << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

vector<int> adj[200010];
int n, dp[200010], A[200010];

void update(int p, int x) {
  if (p < 1)
    return;
  while (p <= n) {
    dp[p] = max(dp[p], x);
    p += p & -p;
  }
}

int query(int p) {
  if (p < 1)
    return 0;
  int ans = 0;

  while (p >= 1) {
    ans = max(ans, dp[p]);
    p -= p & -p;
  }

  return ans;
}

int lis[200010];

void dfs(int u, int p, int l) {
  // update dp pos
  int mx = query(A[u] - 1);
  update(l, mx + 1);

  //    cout << "u " << u << endl;
  //    cout << "l " << l << endl;
  //    cout << "mx " << mx << endl;
  //    cout << endl;

  lis[u] = mx + 1;

  for (auto v : adj[u]) {
    if (v == p)
      continue;
    dfs(v, u, l + 1);
  }
  // rollback dp pos
  update(l, 0);
}

int main() {
  ios_base::sync_with_stdio(false);

  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }

  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;

    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  dfs(1, 0, 1);

  for (int i = 1; i <= n; i++)
    cout << lis[i] << endl;
  cout << endl;
}