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

#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
#define all(v) v.begin(), v.end()

const int N = 200000;
int tr[4 * N + 1];
int n;
int ans[N + 1], pos[N + 1], a[N + 1];
vector<int> v[N + 1];

void update(int pos, int val, int st = 0, int en = n - 1, int p = 1) {
  if (st > pos || en < pos)
    return;
  assert(p <= 4 * N);
  if (st == en) {
    tr[p] = val;
    return;
  }
  int mid = (st + en) / 2;
  update(pos, val, st, mid, 2 * p);
  update(pos, val, mid + 1, en, 2 * p + 1);
  tr[p] = max(tr[2 * p], tr[2 * p + 1]);
}
int query(int l, int r, int st = 0, int en = n - 1, int p = 1) {
  if (st > r || en < l)
    return 0;
  assert(p <= 4 * N);
  if (st >= l && en <= r) {
    return tr[p];
  }
  int mid = (st + en) / 2;
  int left = query(l, r, st, mid, 2 * p);
  int right = query(l, r, mid + 1, en, 2 * p + 1);
  return max(left, right);
}

vector<int> cords;
void dfs(int u, int p = 0) {
  int ns = lower_bound(all(cords), a[u]) - cords.begin();
  // cout<<u<<' '<<ns<<endl;
  int val = ((ns != 0) ? (query(0, ns - 1)) : 0) + 1;
  update(pos[u], val);
  ans[u] = query(0, n - 1);
  for (auto e : v[u]) {
    if (e != p) {
      dfs(e, u);
    }
  }
  update(pos[u], 0);
}

int main() {

  fast();
  cin >> n;
  vector<pair<int, int>> b;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b.pb({a[i], i});
  }
  sort(all(b));
  for (int i = 0; i < n; i++) {
    pos[b[i].ss] = i;
    cords.pb(b[i].ff);
  }

  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    v[x].pb(y);
    v[y].pb(x);
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
#define MOD 1000000007
#define INF 1000000000
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
#define endl '\\n'
#define pl cout << endl;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
const long long INFll = 1ll * INF * INF;
const long double PI =
    3.141592653589793238462643383279502884197169399375105820974944;
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define debug(x) cout << x << endl;
#define sz(a) int((a).size())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
  // unordered_map<long long, int, custom_hash>
  // gp_hash_table<long long, int, custom_hash>
};
long long genrand(long long L, long long R) {
  return uniform_int_distribution<long long>(L, R)(rng);
}
int mul(int a, int b, int mod = MOD) { return int(a * 1ll * b % mod); }
int norm(int a, int mod = MOD) {
  while (a >= mod)
    a -= mod;
  while (a < 0)
    a += mod;
  return a;
}
int powmod(int x, int y, int mod = MOD) {
  int res = 1;
  while (y > 0) {
    if (y & 1)
      res = mul(res, x, mod);
    x = mul(x, x, mod);
    y = y >> 1;
  }
  return res;
}
int inv(int a, int mod = MOD) { return powmod(a, mod - 2); }
const int N = 200000;
int tr[4 * N + 1];
int n;
int ans[N], pos[N], a[N];
vector<int> v[N];
void update(int pos, int val, int st = 0, int en = n - 1, int p = 1) {
  if (st > pos || en < pos)
    return;
  assert(p <= 4 * N);
  if (st == en) {
    tr[p] = val;
    return;
  }
  int mid = (st + en) / 2;
  update(pos, val, st, mid, 2 * p);
  update(pos, val, mid + 1, en, 2 * p + 1);
  tr[p] = max(tr[2 * p], tr[2 * p + 1]);
}
int query(int l, int r, int st = 0, int en = n - 1, int p = 1) {
  if (st > r || en < l)
    return 0;
  assert(p <= 4 * N);
  if (st >= l && en <= r) {
    return tr[p];
  }
  int mid = (st + en) / 2;
  return max(query(l, r, st, mid, 2 * p), query(l, r, mid + 1, en, 2 * p + 1));
}

vector<int> cords;
void dfs(int u, int p = 0) {
  int ns = lower_bound(all(cords), a[u]) - cords.begin();
  int val = query(0, ns - 1) + 1;
  update(pos[u], val);
  // if(u==5){
  // 	cout<<"SP"<<query(7, 7)<<endl;
  // }
  // cout<<pos[u]<<' '<<val<<endl;
  ans[u] = val;
  for (auto e : v[u]) {
    if (e != p) {
      dfs(e, u);
    }
  }
  // cout<<"- "<<pos[u]<<' '<<1<<endl;
  update(pos[u], 1);
}
int main() {

  fast();
  cin >> n;
  vector<pair<int, int>> b;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b.pb({a[i], i});
  }
  sort(all(b));
  for (int i = 0; i < n; i++) {
    pos[b[i].ss] = i;
    cords.pb(b[i].ff);
  }

  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    v[x].pb(y);
    v[y].pb(x);
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}