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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ull B = 100000007;
ll mod = 1000000007;
int INF = 0x3f3f3f3f;

vector<vector<int>> G;
vector<int> a;
vector<int> a2;
vector<int> res;
vector<bool> used;

class ST {
public:
  vector<int> t;
  int n;

  int combine(int l, int r) { return max(l, r); }

  ST(int n) {
    this->n = n;
    t = vector<int>(n << 1, 0);
  }

  void update(int i, int value) {
    t[i += n] = value;
    i >>= 1;
    while (i > 0) {
      t[i] = combine(t[i << 1], t[i << 1 | 1]);
      i >>= 1;
    }
  }

  //[l, r)
  int query(int l, int r) {
    int resl = 0, resr = 0;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if ((l & 1) != 0)
        resl = combine(resl, t[l++]);
      if ((r & 1) != 0)
        resr = combine(t[--r], resr);
    }
    return combine(resl, resr);
  }
};
map<int, int> mp;
void dfs(ST &st, int id, int maxm) {
  int t1 = st.query(0, mp[a[id]]);
  maxm = max(maxm, t1 + 1);
  res[id] = maxm;
  used[id] = true;
  int t2 = st.query(mp[a[id]], mp[a[id]] + 1);
  st.update(mp[a[id]], t1 + 1);

  for (auto v : G[id]) {
    if (!used[v]) {
      dfs(st, v, maxm);
    }
  }

  st.update(mp[a[id]], t2);
}

int main() {
  int n;
  cin >> n;
  a = vector<int>(n + 1);
  res = vector<int>(n + 1);
  a2 = vector<int>(n + 1);
  used = vector<bool>(n + 1, false);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  G = vector<vector<int>>(n + 1, vector<int>());
  for (int i = 1; i <= n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  for (int i = 1; i <= n; ++i) {
    if (mp.find(a[i]) == mp.end()) {
      mp[a[i]] = 0;
    }
  }
  int id = 0;
  for (auto &v : mp) {
    v.second = id++;
  }

  ST st(mp.size());

  dfs(st, 1, 0);

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

  return 0;
}


Error: WA

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ull B = 100000007;
ll mod = 1000000007;
int INF = 0x3f3f3f3f;

vector<vector<int>> G;
vector<int> a;
vector<int> a2;
vector<int> res;
vector<bool> used;
int n;

class ST {
public:
  vector<int> t;
  int n;

  int combine(int l, int r) { return max(l, r); }

  ST(int n) {
    this->n = n;
    t = vector<int>(n << 1, 0);
  }

  void update(int i, int value) {
    t[i += n] = value;
    i >>= 1;
    while (i > 0) {
      t[i] = combine(t[i << 1], t[i << 1 | 1]);
      i >>= 1;
    }
  }

  //[l, r)
  int query(int l, int r) {
    int resl = 0, resr = 0;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if ((l & 1) != 0)
        resl = combine(resl, t[l++]);
      if ((r & 1) != 0)
        resr = combine(t[--r], resr);
    }
    return combine(resl, resr);
  }
};

void dfs(ST &st, int id) {
  int t1 = st.query(0, a2[id]);
  int t2 = st.query(0, a2[id] + 1);
  used[id] = true;
  st.update(a2[id], t1 + 1);
  res[id] = st.query(0, n);

  for (auto v : G[id]) {
    if (!used[v]) {
      dfs(st, v);
    }
  }

  st.update(a2[id], t2);
}

int main() {
  cin >> n;
  a = vector<int>(n + 1);
  res = vector<int>(n + 1);
  a2 = vector<int>(n + 1);
  used = vector<bool>(n + 1, false);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  G = vector<vector<int>>(n + 1, vector<int>());
  for (int i = 1; i <= n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  map<int, int> mp;
  for (int i = 1; i <= n; ++i) {
    if (mp.find(a[i]) == mp.end()) {
      mp[a[i]] = 0;
    }
  }
  int id = 0;
  for (auto &v : mp) {
    v.second = id++;
  }

  for (int i = 1; i <= n; ++i) {
    a2[i] = mp[a[i]];
  }

  ST st(mp.size());

  dfs(st, 1);

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

  return 0;
}