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

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

#define rep(i, a, b) for (int 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
#define fst first
#define snd second

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;

struct Tree {
  typedef int T;
  static constexpr T unit = 0; // f(unit, b) must equal b
  T f(T a, T b) { return max(a, b); }
  vector<T> s;
  int n;
  Tree(int n = 0, T def = unit) : s(2 * n, def), n(n) {}
  void update(int pos, T val) {
    for (s[pos += n] = val; pos /= 2;)
      s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
  }
  T query(int b, int e) { // query [b, e)
    T ra = unit, rb = unit;
    for (b += n, e += n; b < e; b /= 2, e /= 2) {
      if (b % 2)
        ra = f(ra, s[b++]);
      if (e % 2)
        rb = f(s[--e], rb);
    }
    return f(ra, rb);
  }
};

vi a(212345);
vvi g(212345);
Tree tr(212345);
vi out(212345);

void dfs(int u = 1, int p = 1) {
  int b4 = tr.query(a[u], a[u] + 1);
  int lis = tr.query(0, a[u]);
  int upd = max(lis + 1, b4);
  upd = max(upd, 1);
  tr.update(a[u], upd);
  out[u] = upd;
  out[u] = max(out[u], out[p]);
  for (auto v : g[u]) {
    if (v == p)
      continue;
    dfs(v, u);
  }
  tr.update(a[u], b4);
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  int n;
  cin >> n;

  rep(i, 1, n + 1) cin >> a[i];
  set<int> s;
  rep(i, 1, n + 1) s.insert(a[i]);

  map<int, int> tr;
  int co = 0;
  for (auto x : s) {
    tr[x] = ++co;
  }
  rep(i, 1, n + 1) a[i] = tr[a[i]];

  rep(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }

  dfs();
  rep(i, 1, n + 1) cout << out[i] << '\\n';
}

Error: 0

Fixed submission:

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

#define rep(i, a, b) for (int 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
#define fst first
#define snd second

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
const ll mod = 11092019;
pii operator+(pii a, pii b) {
  if (a.fst > b.fst)
    return a;
  if (b.fst > a.fst)
    return b;
  return make_pair(a.fst, (a.snd + b.snd) % mod);
}
struct Tree {
  typedef pii T;
  static constexpr T unit = make_pair(0, 0); // f(unit, b) must equal b
  T f(T a, T b) { return a + b; }
  vector<T> s;
  int n;
  Tree(int n = 0, T def = unit) : s(2 * n, def), n(n) {}
  void update(int pos, T val) {
    for (s[pos += n] = val; pos /= 2;)
      s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
  }
  T query(int b, int e) { // query [b, e)
    T ra = unit, rb = unit;
    for (b += n, e += n; b < e; b /= 2, e /= 2) {
      if (b % 2)
        ra = f(ra, s[b++]);
      if (e % 2)
        rb = f(s[--e], rb);
    }
    return f(ra, rb);
  }
};

vii ans(212345);
vi a(212345);
vvi g(212345);
Tree tr(212345);
vi out(212345);

void dfs(int u = 1, int p = 1) {
  pii b4 = tr.query(a[u], a[u] + 1);

  pii lis = tr.query(0, a[u] + 1);
  pii upd = make_pair(lis.fst + 1, max(lis.snd, 1LL));
  tr.update(a[u], upd);
  ans[a[u]] = ans[a[u]] + upd;
  out[u] = upd.fst;
  for (auto v : g[u]) {
    if (v == p)
      continue;
    dfs(v, u);
  }
  tr.update(a[u], b4);
}

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);

  int n;
  cin >> n;

  rep(i, 1, n + 1) cin >> a[i];
  rep(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }

  dfs();
  rep(i, 1, n + 1) cout << out[i] << '\\n';
}