Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// using namespace __gnu_pbds;
using namespace std;
// template <typename T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
//     tree_order_statistics_node_update>;

typedef long long int ll;
const ll N = 1e5 + 9;
const ll p = 31;
// const ll m = 1e9 + 7;
const ll inf = 1e14;
const ll mod = 1e9 + 7;
#define x first
#define y second
#define pb push_back
#define mp make_pair
using pii = pair<ll, ll>;
ll powm(ll a, ll b) {
  a = a % mod;
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
std::vector<ll> adj[N];
ll t[4 * N], lis[N], ans[N];
map<ll, ll> comp;
ll a[N], b[N];

void build(ll v, ll tl, ll tr) {
  if (tl == tr) {
    t[v] = lis[tl];
  } else {
    ll tm = (tl + tr) / 2;
    build(2 * v, tl, tm);
    build(2 * v + 1, tm + 1, tr);
    t[v] = max(t[2 * v], t[2 * v + 1]);
  }
}
ll query(ll v, ll tl, ll tr, ll l, ll r) {
  if (l > r) {
    return 0;
  }
  if (l == tl && r == tr) {
    return t[v];
  }
  ll tm = (tl + tr) / 2;
  return max(query(2 * v, tl, tm, l, min(tm, r)),
             query(2 * v + 1, tm + 1, tr, max(tm + 1, l), r));
}
void update(ll v, ll tl, ll tr, ll pos, ll val) {
  if (tl == tr) {
    t[v] = val;
  } else {
    ll tm = (tl + tr) / 2;
    if (pos <= tm) {
      update(2 * v, tl, tm, pos, val);
    } else {
      update(2 * v + 1, tm + 1, tr, pos, val);
    }

    t[v] = max(t[2 * v], t[2 * v + 1]);
  }
}
void dfs(ll v, ll p, ll ct) {
  ll cur = query(1, 0, ct, comp[a[v]], comp[a[v]]);
  ll ma = query(1, 0, ct, 0, comp[a[v]] - 1);
  update(1, 0, ct, comp[a[v]], ma + 1);
  ll an = query(1, 0, ct, 0, ct);
  // cout<<v<<" "<<an<<" "<<ma<<'\n';
  ans[v] = an;
  for (auto u : adj[v]) {
    if (u != p) {
      dfs(u, v, ct);
    }
  }
  update(1, 0, ct, comp[a[v]], cur);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  memset(lis, 0, sizeof(lis));
  ll T = 1;
  // cin >> T;
  while (T--) {
    ll n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      b[i] = a[i];
    }
    for (int i = 0; i < n - 1; i++) {
      ll u, v;
      cin >> u >> v;
      u--;
      v--;
      adj[u].pb(v);
      adj[v].pb(u);
    }
    ll ct = 0;
    sort(b, b + n);
    comp[b[0]] = ct++;
    for (int i = 1; i < n; i++) {
      // cout<<b[i]<<'\n';
      if (b[i] != b[i - 1]) {
        comp[b[i]] = ct++;
      }
    }
    build(1, 0, ct);
    dfs(0, -1, ct);
    // update(1,0,ct,0,4);
    // cout<<query(1,0,ct,0,ct)<<'\n';
    for (int i = 0; i < n; i++) {
      cout << ans[i] << " ";
    }
    cout << '\n';
  }
}

Fixed submission:
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// using namespace __gnu_pbds;
using namespace std;
// template <typename T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
//     tree_order_statistics_node_update>;

typedef long long int ll;
const ll N = 2e5 + 9;
const ll p = 31;
// const ll m = 1e9 + 7;
const ll inf = 1e14;
const ll mod = 1e9 + 7;
#define x first
#define y second
#define pb push_back
#define mp make_pair
using pii = pair<ll, ll>;
ll powm(ll a, ll b) {
  a = a % mod;
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
std::vector<ll> adj[N];
ll t[4 * N], lis[N], ans[N];
map<ll, ll> comp;
ll a[N], b[N];

void build(ll v, ll tl, ll tr) {
  if (tl == tr) {
    t[v] = lis[tl];
  } else {
    ll tm = (tl + tr) / 2;
    build(2 * v, tl, tm);
    build(2 * v + 1, tm + 1, tr);
    t[v] = max(t[2 * v], t[2 * v + 1]);
  }
}
ll query(ll v, ll tl, ll tr, ll l, ll r) {
  if (l > r) {
    return 0;
  }
  if (l == tl && r == tr) {
    return t[v];
  }
  ll tm = (tl + tr) / 2;
  return max(query(2 * v, tl, tm, l, min(tm, r)),
             query(2 * v + 1, tm + 1, tr, max(tm + 1, l), r));
}
void update(ll v, ll tl, ll tr, ll pos, ll val) {
  if (tl == tr) {
    t[v] = val;
  } else {
    ll tm = (tl + tr) / 2;
    if (pos <= tm) {
      update(2 * v, tl, tm, pos, val);
    } else {
      update(2 * v + 1, tm + 1, tr, pos, val);
    }

    t[v] = max(t[2 * v], t[2 * v + 1]);
  }
}
void dfs(ll v, ll p, ll ct) {
  ll cur = query(1, 0, ct, comp[a[v]], comp[a[v]]);
  ll ma = query(1, 0, ct, 0, comp[a[v]] - 1);
  update(1, 0, ct, comp[a[v]], ma + 1);
  ll an = query(1, 0, ct, 0, ct);
  // cout<<v<<" "<<an<<" "<<ma<<'\n';
  ans[v] = an;
  for (auto u : adj[v]) {
    if (u != p) {
      dfs(u, v, ct);
    }
  }
  update(1, 0, ct, comp[a[v]], cur);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  memset(lis, 0, sizeof(lis));
  ll T = 1;
  // cin >> T;
  while (T--) {
    ll n;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      b[i] = a[i];
    }
    for (int i = 0; i < n - 1; i++) {
      ll u, v;
      cin >> u >> v;
      u--;
      v--;
      adj[u].pb(v);
      adj[v].pb(u);
    }
    ll ct = 0;
    sort(b, b + n);
    comp[b[0]] = ct++;
    for (int i = 1; i < n; i++) {
      // cout<<b[i]<<'\n';
      if (b[i] != b[i - 1]) {
        comp[b[i]] = ct++;
      }
    }
    build(1, 0, ct);
    dfs(0, -1, ct);
    // update(1,0,ct,0,4);
    // cout<<query(1,0,ct,0,ct)<<'\n';
    for (int i = 0; i < n; i++) {
      cout << ans[i] << " ";
    }
    cout << '\n';
  }
}