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

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

using ll = long long;
using ld = long double;
using vi = vector<int>;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define PB push_back

struct edge {
  int to;
};
using graph = vector<vector<edge>>;

struct ST {
  int s;
  vector<ll> v;
  ST(int s) : s{s}, v(2 * s) {}
  void update(int p, ll d, bool assign) {
    p += s;
    if (assign)
      v[p] = d;
    else
      v[p] = max(v[p], d);
    for (p >>= 1; p >= 1; p >>= 1)
      v[p] = max(v[2 * p], v[2 * p + 1]);
  }
  ll query(int l, int r) {
    if (l > r)
      return 0;
    ll acc = 0;
    for (l += s, r += s; l <= r; (++l) >>= 1, (--r) >>= 1) {
      if (l & 1)
        acc = max(acc, v[l]);
      if (~r & 1)
        acc = max(v[r], acc);
    }
    return acc;
  }
};

vi sorted;
int getindex(int val) {
  assert(binary_search(all(sorted), val));
  return lower_bound(all(sorted), val) - begin(sorted);
}

void dfs(graph &g, int i, vi &a, ST &st, vi &res, int p = -1) {
  // cout << "At: " << i << " " << a[i] << endl;
  auto index = getindex(a[i]);
  assert(index >= 0 and index < sz(a));
  auto old = st.query(index, index);

  auto best = st.query(0, index - 1);
  assert(best >= 0);
  assert(best < sz(a));
  auto possible = best + 1;

  assert(possible >= old);

  res[i] = possible;
  st.update(index, possible, true);
  trav(e, g[i]) if (e.to != p) { dfs(g, e.to, a, st, res, i); }
  st.update(index, old, true);
}

void prop(graph &g, int i, auto &res, int p = -1, int best = 0) {
  res[i] = max(res[i], best);
  trav(e, g[i]) if (e.to != p) { prop(g, e.to, res, i, max(best, res[i])); }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  vi a(n);
  trav(i, a) {
    cin >> i;
    --i;
  }

  sorted = a;
  sort(all(sorted));
  sorted.erase(unique(all(sorted)), end(sorted));
  graph g(n);
  rep(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    g[u].PB({v});
    g[v].PB({u});
  }

  ST st(n);
  vi res(n, -1);
  dfs(g, 0, a, st, res);
  prop(g, 0, res);
  rep(i, 0, n) {
    assert(res[i] >= 0);
    cout << res[i] << '\\n';
  }
}


Error: 0

Fixed submission:

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

#define int long long

using ll = long long;
using ld = long double;
using vi = vector<int>;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define PB push_back

struct edge {
  int to;
};
using graph = vector<vector<edge>>;

struct ST {
  int s;
  vector<ll> v;
  ST(int s) : s{s}, v(2 * s) {}
  void update(int p, ll d, bool assign) {
    p += s;
    if (assign)
      v[p] = d;
    else
      v[p] = max(v[p], d);
    for (p >>= 1; p >= 1; p >>= 1)
      v[p] = max(v[2 * p], v[2 * p + 1]);
  }
  ll query(int l, int r) {
    ll acc = 0;
    for (l += s, r += s; l <= r; (++l) >>= 1, (--r) >>= 1) {
      if (l & 1)
        acc = max(acc, v[l]);
      if (~r & 1)
        acc = max(v[r], acc);
    }
    return acc;
  }
};

vi sorted;
int getindex(int val) {
  assert(binary_search(all(sorted), val));
  return lower_bound(all(sorted), val) - begin(sorted);
}

void dfs(graph &g, int i, vi &a, ST &st, vi &res, int p = -1) {
  auto index = getindex(a[i]);
  assert(index >= 0 and index < sz(a));
  auto old = st.query(index, index);

  auto best = st.query(0, index - 1);
  assert(best >= 0);
  assert(best < sz(a));
  auto possible = best + 1;
  assert(possible > old);
  res[i] = possible;
  st.update(index, possible, true);
  trav(e, g[i]) if (e.to != p) { dfs(g, e.to, a, st, res, i); }
  st.update(index, old, true);
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  vi a(n);
  trav(i, a) cin >> i;

  sorted = a;
  sort(all(sorted));
  sorted.erase(unique(all(sorted)), end(sorted));
  graph g(n);
  rep(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    g[u].PB({v});
    g[v].PB({u});
  }

  ST st(n);
  vi res(n, -1);
  dfs(g, 0, a, st, res);
  rep(i, 0, n) {
    assert(res[i] >= 0);
    cout << res[i] << '\\n';
  }
}