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

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define S second
#define F first
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define lb lower_bound
#define si(c) (int)((c).size())
#define lcm(a, b) (a * (b / __gcd(a, b)))
#define inf (long long)(1e10 + 1)
#define endl '\\n'
#define mp make_pair
#define time(s) (double(clock() - s) / double(CLOCKS_PER_SEC))
#define debug(args...) _F(#args, args)
#define vi std::vector<int>
#define pii pair<int, int>
#define vpi vector<pii>

clock_t start;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

template <typename T> void _F(const char *name, T arg1) {
  cerr << name << " = " << arg1 << endl;
}
template <typename T, typename... Args>
void _F(const char *names, T arg1, Args... args) {
  const char *name = strchr(names, ',');
  cerr.write(names, name - names) << " = " << arg1 << endl;
  _F(name + 2, args...);
}
template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &q) {
  in >> q.F >> q.S;
  return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> &q) {
  out << q.F << " " << q.S;
  return out;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2) {
  return {p1.F + p2.F, p1.S + p2.S};
}
template <typename T1, typename T2>
pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2) {
  return {p1.F - p2.F, p1.S - p2.S};
}

const int N = 2e5 + 1;
int t[4 * N];
vi g[N];
multiset<int> s[N];
int n, u, v, vis[N], a[N], x, ans[N];

int get_first(int v, int lv, int rv, int l, int r, int x) {
  if (lv > r || rv < l)
    return -1;
  if (l <= lv && rv <= r) {
    // cout << t[v] << " " << x << endl;
    if (t[v] >= x)
      return -1;
    while (lv != rv) {
      int mid = lv + (rv - lv) / 2;
      if (t[2 * v + 1] < x) {
        v = 2 * v + 1;
        lv = mid + 1;
      } else {
        v = 2 * v;
        rv = mid;
      }
    }
    return lv;
  }
  int mid = lv + (rv - lv) / 2;
  int rs = get_first(2 * v, lv, mid, l, r, x);
  if (rs != -1)
    return rs;
  return get_first(2 * v + 1, mid + 1, rv, l, r, x);
}

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] = min(t[v * 2], t[v * 2 + 1]);
  }
}

void dfs(int v) {
  vis[v] = 1;
  int id = get_first(1, 1, n, 1, n, x);
  ans[v] = max(ans[v], id);
  id = get_first(1, 1, n, 1, n, a[v]);
  ans[v] = max({ans[v], id + 1, (int)1});
  if (!si(s[max(id + 1, (int)1)])) {
    update(1, 1, n, max(id + 1, (int)1), a[v]);
  } else if (*begin(s[max(id + 1, (int)1)]) > a[v]) {
    update(1, 1, n, max(id + 1, (int)1), a[v]);
  }
  s[max((int)1, id + 1)].insert(a[v]);
  for (auto u : g[v]) {
    if (vis[u])
      continue;
    dfs(u);
  }
  if (*begin(s[max(id + 1, (int)1)]) == a[v]) {
    update(1, 1, n, max(id + 1, (int)1), inf);
  }
  s[max(id + 1, (int)1)].erase(s[max(id + 1, (int)1)].find(a[v]));
  if (si(s[max(id + 1, (int)1)])) {
    update(1, 1, n, max(id + 1, (int)1), *begin(s[max(id + 1, (int)1)]));
  }
}

void solve() {
  for (int i = 0; i < 4 * N; i++)
    t[i] = inf;
  x = 1e9 + 1;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }
}

signed main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int t = 1;
  start = clock();

  cout << fixed << setprecision(20);

  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // freopen("error.txt", "w", stderr);
  // #endif

  // cin >> t;

  for (int i = 1; i <= t; ++i) {
    // cout << "Case #" << i << ": ";
    solve();
    // cout << endl;
  }
  // cerr << time(start);
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define S second
#define F first
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define lb lower_bound
#define si(c) (int)((c).size())
#define lcm(a, b) (a * (b / __gcd(a, b)))
#define inf (long long)(1e10 + 1)
#define endl '\\n'
#define mp make_pair
#define time(s) (double(clock() - s) / double(CLOCKS_PER_SEC))
#define debug(args...) _F(#args, args)
#define vi std::vector<int>
#define pii pair<int, int>
#define vpi vector<pii>

clock_t start;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

template <typename T> void _F(const char *name, T arg1) {
  cerr << name << " = " << arg1 << endl;
}
template <typename T, typename... Args>
void _F(const char *names, T arg1, Args... args) {
  const char *name = strchr(names, ',');
  cerr.write(names, name - names) << " = " << arg1 << endl;
  _F(name + 2, args...);
}
template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &q) {
  in >> q.F >> q.S;
  return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> &q) {
  out << q.F << " " << q.S;
  return out;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2) {
  return {p1.F + p2.F, p1.S + p2.S};
}
template <typename T1, typename T2>
pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2) {
  return {p1.F - p2.F, p1.S - p2.S};
}

const int N = 2e5 + 1;
int t[4 * N];
vi g[N];
multiset<int> s[N];
int n, u, v, vis[N], a[N], x, ans[N];

int get_first(int v, int lv, int rv, int l, int r, int x) {
  if (lv > r || rv < l)
    return -1;
  if (l <= lv && rv <= r) {
    if (t[v] <= x)
      return -1;
    while (lv != rv) {
      int mid = lv + (rv - lv) / 2;
      if (t[2 * v] < x) {
        v = 2 * v;
        rv = mid;
      } else {
        v = 2 * v + 1;
        lv = mid + 1;
      }
    }
    return lv;
  }

  int mid = lv + (rv - lv) / 2;
  int rs = get_first(2 * v, lv, mid, l, r, x);
  if (rs != -1)
    return rs;
  return get_first(2 * v + 1, mid + 1, rv, l, r, x);
}

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] = min(t[v * 2], t[v * 2 + 1]);
  }
}

void dfs(int v) {
  vis[v] = 1;
  ans[v] = 1;
  int id = get_first(1, 1, n, 1, n, x);
  ans[v] = max(ans[v], id);
  id = get_first(1, 1, n, 1, n, a[v]);
  ans[v] = max(ans[v], id + 1);
  if (!si(s[max(id + 1, (int)1)])) {
    update(1, 1, n, max(id + 1, (int)1), a[v]);
  } else if (*begin(s[max(id + 1, (int)1)]) > a[v]) {
    update(1, 1, n, max(id + 1, (int)1), a[v]);
  }
  s[max((int)1, id + 1)].insert(a[v]);
  for (auto u : g[v]) {
    if (vis[u])
      continue;
    dfs(u);
  }
  if (*begin(s[max(id + 1, (int)1)]) == a[v]) {
    update(1, 1, n, max(id + 1, (int)1), inf);
  }
  s[max(id + 1, (int)1)].erase(s[max(id + 1, (int)1)].find(a[v]));
  if (si(s[max(id + 1, (int)1)])) {
    update(1, 1, n, max(id + 1, (int)1), *begin(s[max(id + 1, (int)1)]));
  }
}

void solve() {
  for (int i = 0; i < 4 * N; i++)
    t[i] = inf;
  x = 1e9 + 1;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }
}

signed main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int t = 1;
  start = clock();

  cout << fixed << setprecision(20);

  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // freopen("error.txt", "w", stderr);
  // #endif

  // cin >> t;

  for (int i = 1; i <= t; ++i) {
    // cout << "Case #" << i << ": ";
    solve();
    // cout << endl;
  }
  // cerr << time(start);
  return 0;
}