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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

// #pragma comment(linker, "/STACK:16777216")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("unswitch-loops")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,abm,mmx,avx,avx2,popcnt,tune=native")

using namespace std;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

#define ll long long
#define ull unsigned long long
// #define double long double
#define int ll
// #define int ull
// #define int short
// #define int unsigned short
// #define int unsigned int
#define mp make_pair
#define F first
#define S second
#define pii pair<int, int>
#define hash hash228
const int SZ = 2e5 + 100;
const double EPS = 1e-7;
const int mod = 163577857;
const int hashpow = 2;
const int inf = 2e18;

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    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);
  }
};

int n;
int a[SZ];
vector<int> g[SZ];

void read() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
}

vector<int> x[SZ];
int len = 0;
int ans[SZ];

void dfs(int u, int p = -1) {
  int lo = -1, hi = len;
  while (hi - lo > 1) {
    int m = lo + ((hi - lo) >> 1);

    if (x[m].back() >= a[u]) {
      hi = m;
    } else {
      lo = m;
    }
  }

  if (x[hi].back() == inf) {
    ++len;
  }
  bool z = false;
  if (x[hi].back() != a[u]) {
    x[hi].push_back(a[u]);
    z = true;
  }

  ans[u] = len;

  for (int v : g[u]) {
    if (v == p)
      continue;
    dfs(v, u);
  }

  if (z)
    x[hi].pop_back();
  if (x[hi].back() == inf)
    --len;
}

void solve() {
  for (int i = 0; i < SZ; ++i) {
    x[i].push_back(inf);
  }
  dfs(0);
  for (int i = 0; i < n; ++i) {
    cout << ans[i] << "\\n";
  }
}

signed main() {

#ifdef __LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif

  //    int t;
  //    cin >> t;
  //    for (int i = 0; i < t; ++i) {
  read();
  solve();
  //    }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

// #pragma comment(linker, "/STACK:16777216")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("unswitch-loops")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,abm,mmx,avx,avx2,popcnt,tune=native")

using namespace std;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

#define ll long long
#define ull unsigned long long
// #define double long double
#define int ll
// #define int ull
// #define int short
// #define int unsigned short
// #define int unsigned int
#define mp make_pair
#define F first
#define S second
#define pii pair<int, int>
#define hash hash228
const int SZ = 2e5 + 100;
const double EPS = 1e-7;
const int mod = 163577857;
const int hashpow = 2;
const int inf = 2e18;

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    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);
  }
};

int n;
int a[SZ];
vector<int> g[SZ];

void read() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
}

vector<int> x[SZ];
int len = 0;
int ans[SZ];

void dfs(int u, int p = -1) {
  int lo = -1, hi = len;
  while (hi - lo > 1) {
    int m = lo + ((hi - lo) >> 1);

    if (x[m].back() >= a[u]) {
      hi = m;
    } else {
      lo = m;
    }
  }

  if (x[hi].back() == inf) {
    ++len;
  }
  bool z = true;
  if (x[hi].back() != a[u]) {
    x[hi].push_back(a[u]);
    z = false;
  }

  ans[u] = len;

  for (int v : g[u]) {
    if (v == p)
      continue;
    dfs(v, u);
  }

  if (z)
    x[hi].pop_back();
  if (x[hi].back() == inf)
    --len;
}

void solve() {
  for (int i = 0; i < SZ; ++i) {
    x[i].push_back(inf);
  }
  dfs(0);
  for (int i = 0; i < n; ++i) {
    cout << ans[i] << "\\n";
  }
}

signed main() {

#ifdef __LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif

  //    int t;
  //    cin >> t;
  //    for (int i = 0; i < t; ++i) {
  read();
  solve();
  //    }

  return 0;
}