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

#include <bits/stdc++.h>
#include <cmath>
#include <sstream>
#include <string>
// #include <unordered_map>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORD(i, a, b) for (int i = (a - 1); i >= (b); i--)
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
typedef long long int ll;
using namespace std;
ll mod = 1000000007;
// ll mod=67280421310721;
// ll mod=998244353;
ll INF = 1e18;
ll po(ll a, ll b) {
  ll re = 1;
  while (b) {
    if (b % 2 == 1) {
      re = (re * a);
      re %= mod;
    }
    a = (a * a);
    a %= mod;
    b >>= 1;
  }
  return re;
}
int t[800100];
void update(int v, int tl, int tr, int pos, int new_val) {
  if (tl == tr) {
    t[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);
    t[v] = max(t[2 * v], t[2 * v + 1]);
  }
  return;
}
int sum(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    return t[v];
  }
  int tm = (tl + tr) / 2;
  return max(sum(v * 2, tl, tm, l, min(r, tm)),
             sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}
void dfs(int i, int j, vector<int> tree[], int a[], int ans[], int ul) {
  int tem = sum(1, 0, ul, a[i], a[i]);
  int u = sum(1, 0, ul, 0, a[i] - 1) + 1;
  update(1, 0, ul, a[i], u);
  ans[i] = sum(1, 0, ul, 0, ul);
  FOR(k, 0, tree[i].size()) {
    if (tree[i][k] != j)
      dfs(tree[i][k], i, tree, a, ans, ul);
  }
  update(1, 0, ul, a[i], tem);
  return;
}
bool che(pair<ll, int> a, pair<ll, int> b) { return a.second < b.second; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int test = 1;
  //	cin>>test;
  while (test--) {
    int n;
    cin >> n;
    pair<ll, int> a[n];
    FOR(i, 0, n) {
      cin >> a[i].first;
      a[i].second = i;
    }
    sort(a, a + n);
    ll j = 1, p = a[0].first, ul;
    FOR(i, 0, n) {
      if (a[i].first == p)
        a[i].first = j;
      else {
        j++;
        p = a[i].first;
        a[i].first = j;
      }
    }
    sort(a, a + n, che);
    ul = j + 5;
    int u, v;
    vector<int> tree[n];
    FOR(i, 1, n) {
      cin >> u >> v;
      tree[u - 1].pb(v - 1);
      tree[v - 1].pb(u - 1);
    }
    int ans[n];
    int b[n];
    FOR(i, 0, n)
    b[i] = a[i].first;
    dfs(0, -1, tree, b, ans, ul);
    FOR(i, 0, n) { cout << ans[i] << '\\n'; }
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <sstream>
#include <string>
// #include <unordered_map>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORD(i, a, b) for (int i = (a - 1); i >= (b); i--)
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
typedef long long int ll;
using namespace std;
ll mod = 1000000007;
// ll mod=67280421310721;
// ll mod=998244353;
ll INF = 1e18;
ll po(ll a, ll b) {
  ll re = 1;
  while (b) {
    if (b % 2 == 1) {
      re = (re * a);
      re %= mod;
    }
    a = (a * a);
    a %= mod;
    b >>= 1;
  }
  return re;
}
vector<multiset<int>> t;
void update(int v, int tl, int tr, int pos, int new_val) {
  if (tl == tr) {
    t[v].insert(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);
    t[v].insert(new_val);
  }
  return;
}
void rem(int v, int tl, int tr, int pos, int new_val) {
  if (tl == tr) {
    t[v].erase(t[v].find(new_val));
  } else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      rem(v * 2, tl, tm, pos, new_val);
    else
      rem(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[v].erase(t[v].find(new_val));
  }
  return;
}
int sum(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    return *(--t[v].end());
  }
  int tm = (tl + tr) / 2;
  return max(sum(v * 2, tl, tm, l, min(r, tm)),
             sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}
void dfs(int i, int j, vector<int> tree[], int a[], int ans[], int ul) {
  ans[i] = sum(1, 0, ul, 0, a[i] - 1) + 1;
  update(1, 0, ul, a[i], ans[i]);
  FOR(k, 0, tree[i].size()) {
    if (tree[i][k] != j)
      dfs(tree[i][k], i, tree, a, ans, ul);
  }
  rem(1, 0, ul, a[i], ans[i]);
  return;
}
bool che(pair<ll, int> a, pair<ll, int> b) { return a.second < b.second; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int test = 1;
  //	cin>>test;
  while (test--) {
    int n;
    cin >> n;
    pair<ll, int> a[n];
    t.resize(4 * n + 4);
    FOR(i, 0, n) {
      cin >> a[i].first;
      a[i].second = i;
    }
    sort(a, a + n);
    ll j = 1, p = a[0].first, ul;
    FOR(i, 0, n) {
      if (a[i].first == p)
        a[i].first = j;
      else {
        j++;
        p = a[i].first;
        a[i].first = j;
      }
    }
    sort(a, a + n, che);
    ul = j + 5;
    int u, v;
    vector<int> tree[n];
    FOR(i, 1, n) {
      cin >> u >> v;
      tree[u - 1].pb(v - 1);
      tree[v - 1].pb(u - 1);
    }
    int ans[n];
    int b[n];
    FOR(i, 0, n)
    b[i] = a[i].first;
    dfs(0, -1, tree, b, ans, ul);
    FOR(i, 0, n) { cout << ans[i] << '\\n'; }
  }
  return 0;
}