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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;

#define lp(var, start, end) for (ll var = start; var < end; ++var)
#define rlp(var, start, end) for (ll var = start; var >= end; var--)
#define pb push_back
#define mp make_pair
#define pf push_front
#define ff first
#define ss second
#define vll vector<ll>
#define vld vector<ld>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define vpll vector<pll>
#define vpld vector<pld>
#define all(X) X.begin(), X.end()
#define endl "\\n"
#define sz(x) ((ll)((x).size()))
const ll N = 2e5 + 5, inf = 1e9;
ll ary[N];
ll ans[N];
vll G[N];
ll n;
ll tree[4 * N];
multiset<ll> res;
ll query(ll nd, ll st, ll ed, ll l, ll r) {
  if (st > r || ed < l || st > ed)
    return 0;
  if (st >= l && ed <= r)
    return tree[nd];
  ll mid = (st + ed) / 2;
  return max(query(2 * nd, st, mid, l, r),
             query(2 * nd + 1, mid + 1, ed, l, r));
}
ll update(ll nd, ll st, ll ed, ll id, ll val) {
  if (st > id || ed < id)
    return tree[nd];
  if (st >= id && ed <= id) {
    tree[nd] = val;
    return tree[nd];
  }
  ll mid = (st + ed) / 2;
  tree[nd] = max(update(2 * nd, st, mid, id, val),
                 update(2 * nd + 1, mid + 1, ed, id, val));
  return tree[nd];
}

ll dfs(ll a, ll p) {
  ll x = query(1, 0, n - 1, 0, ary[a - 1] - 1);
  ll pre = query(1, 0, n - 1, ary[a - 1], ary[a - 1]);
  res.insert(x + 1);
  update(1, 0, n - 1, ary[a - 1], x + 1);
  ans[a] = *(--res.end());
  lp(i, 0, G[a].size()) {
    ll x = G[a][i];
    if (x != p)
      dfs(x, a);
  }
  update(1, 0, n - 1, ary[a - 1], pre);
  res.erase(res.find(x + 1));
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  vll v;
  lp(i, 0, n) {
    cin >> ary[i];
    v.pb(ary[i]);
  }
  map<ll, ll> M;
  sort(all(v));
  ll c = 0;
  lp(i, 0, n) {
    if (M.count(v[i]) == 0) {
      M[v[i]] = c++;
    }
  }
  lp(i, 0, n) { ary[i] = M[ary[i]]; }
  lp(i, 0, n - 1) {
    ll u, v;
    cin >> u >> v;
    G[u].pb(v);
    G[v].pb(u);
  }
  dfs(1, 0);
  lp(i, 0, n) { cout << ans[i + 1] << endl; }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;

#define lp(var, start, end) for (ll var = start; var < end; ++var)
#define rlp(var, start, end) for (ll var = start; var >= end; var--)
#define pb push_back
#define mp make_pair
#define pf push_front
#define ff first
#define ss second
#define vll vector<ll>
#define vld vector<ld>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define vpll vector<pll>
#define vpld vector<pld>
#define all(X) X.begin(), X.end()
#define endl "\\n"
#define sz(x) ((ll)((x).size()))
const ll N = 2e5 + 5, inf = 1e9;
ll ary[N];
ll ans[N];
vll G[N];
ll n;
ll tree[4 * N];
multiset<ll> res;
ll query(ll nd, ll st, ll ed, ll l, ll r) {
  if (st > r || ed < l || st > ed)
    return 0;
  if (st >= l && ed <= r)
    return tree[nd];
  ll mid = (st + ed) / 2;
  return max(query(2 * nd, st, mid, l, r),
             query(2 * nd + 1, mid + 1, ed, l, r));
}
void update(ll nd, ll st, ll ed, ll id, ll val) {
  if (st >= id && ed <= id) {
    tree[nd] = val;
  }
  ll mid = (st + ed) / 2;
  if (id <= mid)
    update(2 * nd, st, mid, id, val);
  else
    update(2 * nd + 1, mid + 1, ed, id, val);
  tree[nd] = max(tree[2 * nd], tree[2 * nd] + 1);
}

ll dfs(ll a, ll p) {
  ll x = query(1, 0, n - 1, 0, ary[a - 1] - 1);
  ll pre = query(1, 0, n - 1, ary[a - 1], ary[a - 1]);
  res.insert(x + 1);
  update(1, 0, n - 1, ary[a - 1], x + 1);
  ans[a] = *(--res.end());
  lp(i, 0, G[a].size()) {
    ll x = G[a][i];
    if (x != p)
      dfs(x, a);
  }
  update(1, 0, n - 1, ary[a - 1], pre);
  res.erase(res.find(x + 1));
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  vll v;
  lp(i, 0, n) {
    cin >> ary[i];
    v.pb(ary[i]);
  }
  map<ll, ll> M;
  sort(all(v));
  ll c = 0;
  lp(i, 0, n) {
    if (M.count(v[i]) == 0) {
      M[v[i]] = c++;
    }
  }
  lp(i, 0, n) { ary[i] = M[ary[i]]; }
  lp(i, 0, n - 1) {
    ll u, v;
    cin >> u >> v;
    G[u].pb(v);
    G[v].pb(u);
  }
  dfs(1, 0);
  lp(i, 0, n) { cout << ans[i + 1] << endl; }
  return 0;
}