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

#include <bits/stdc++.h>
#define ls x << 1
#define rs x << 1 | 1
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x) & (-x)
#define all(x) (x).begin(), (x).end()
#define mem(a, b) memset(a, b, sizeof(a))
#define int long long
namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror = 0;
inline char nc() {
  static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
  if (p1 == pend) {
    p1 = buf;
    pend = buf + fread(buf, 1, BUF_SIZE, stdin);
    if (pend == p1) {
      IOerror = 1;
      return -1;
    }
  }
  return *p1++;
}
inline bool blank(char ch) {
  return ch == ' ' || ch == '\\n' || ch == '\\r' || ch == '\\t';
}
template <class T> inline bool read(T &x) {
  bool sign = 0;
  char ch = nc();
  x = 0;
  for (; blank(ch); ch = nc())
    ;
  if (IOerror)
    return false;
  if (ch == '-')
    sign = 1, ch = nc();
  for (; ch >= '0' && ch <= '9'; ch = nc())
    x = x * 10 + ch - '0';
  if (sign)
    x = -x;
  return true;
}
template <class T, class... U> bool read(T &h, U &...t) {
  return read(h) && read(t...);
}
#undef OUT_SIZE
#undef BUF_SIZE
}; // namespace FastIO
using namespace std;
using namespace FastIO;

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

const int INF = 0x3f3f3f3f;
const int N = 2e6 + 10;

struct node {
  int l, r, mid, ma;
} tree[N];

int a[N], ans[N];
vector<int> e[N];
int n;

void pp(int x) { tree[x].ma = max(tree[ls].ma, tree[rs].ma); }

void build(int x, int l, int r) {
  tree[x] = {l, r, (l + r) / 2, 0};
  if (l == r) {
    return;
  }
  int mid = tree[x].mid;
  build(ls, l, mid);
  build(rs, mid + 1, r);
}

void modify(int x, int pos, int k) {
  if (tree[x].l == pos && tree[x].r == pos) {
    tree[x].ma = k;
    return;
  }
  int mid = tree[x].mid;
  if (pos <= mid)
    modify(ls, pos, k);
  if (pos > mid)
    modify(rs, pos, k);
  pp(x);
}

int query(int x, int l, int r) {
  if (l <= tree[x].l && tree[x].r <= r) {
    return tree[x].ma;
  }
  int mid = tree[x].mid;
  int res = 0;
  if (l <= mid)
    res = max(res, query(ls, l, r));
  if (r > mid)
    res = max(res, query(rs, l, r));
  return res;
}

void dfs(int u, int fa) {
  int now = a[u];
  int sum = query(1, 1, now - 1) + 1;
  int pre = query(1, now, now);
  modify(1, now, max(sum, pre));
  ans[u] = query(1, 1, n + 5);
  for (auto v : e[u]) {
    if (v == fa)
      continue;
    dfs(v, u);
  }
  modify(1, now, pre);
}

signed main() {

#ifdef xiaofan
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
#endif

  read(n);
  vector<int> b;
  for (int i = 1; i <= n; i++) {
    read(a[i]);
    b.pb(a[i]);
  }
  sort(all(b));
  b.erase(unique(all(b)), b.end());
  for (int i = 1; i <= n; i++)
    a[i] = lower_bound(all(b), a[i]) - b.begin() + 2;
  for (int i = 1; i < n; i++) {
    int u, v;
    read(u, v);
    e[u].pb(v);
    e[v].pb(u);
  }
  build(1, 1, n + 5);
  dfs(1, 1);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << "\\n";

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ls x << 1
#define rs x << 1 | 1
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x) & (-x)
#define all(x) (x).begin(), (x).end()
#define mem(a, b) memset(a, b, sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror = 0;
inline char nc() {
  static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
  if (p1 == pend) {
    p1 = buf;
    pend = buf + fread(buf, 1, BUF_SIZE, stdin);
    if (pend == p1) {
      IOerror = 1;
      return -1;
    }
  }
  return *p1++;
}
inline bool blank(char ch) {
  return ch == ' ' || ch == '\\n' || ch == '\\r' || ch == '\\t';
}
template <class T> inline bool read(T &x) {
  bool sign = 0;
  char ch = nc();
  x = 0;
  for (; blank(ch); ch = nc())
    ;
  if (IOerror)
    return false;
  if (ch == '-')
    sign = 1, ch = nc();
  for (; ch >= '0' && ch <= '9'; ch = nc())
    x = x * 10 + ch - '0';
  if (sign)
    x = -x;
  return true;
}
template <class T, class... U> bool read(T &h, U &...t) {
  return read(h) && read(t...);
}
#undef OUT_SIZE
#undef BUF_SIZE
}; // namespace FastIO
using namespace std;
using namespace FastIO;

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

const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;

struct node {
  int l, r, mid, ma;
} tree[N];

int a[N], ans[N];
vector<int> e[N];

void pp(int x) { tree[x].ma = max(tree[ls].ma, tree[rs].ma); }

void build(int x, int l, int r) {
  tree[x] = {l, r, l + r >> 1, 0};
  if (l == r) {
    return;
  }
  int mid = l + r >> 1;
  build(ls, l, mid);
  build(rs, mid + 1, r);
}

void modify(int x, int pos, int k) {
  if (tree[x].l == pos && tree[x].r == pos) {
    tree[x].ma = k;
    return;
  }
  int mid = tree[x].mid;
  if (pos <= mid)
    modify(ls, pos, k);
  if (pos > mid)
    modify(rs, pos, k);
  pp(x);
}

int query(int x, int l, int r) {
  if (l <= tree[x].l && tree[x].r <= r) {
    return tree[x].ma;
  }
  int mid = tree[x].mid;
  int res = 0;
  if (l <= mid)
    res = max(res, query(ls, l, r));
  if (r > mid)
    res = max(res, query(rs, l, r));
  return res;
}

void dfs(int u, int fa) {
  int now = a[u];
  int sum = query(1, 1, now - 1) + 1;
  int pre = query(1, now, now);
  ans[u] = max(ans[u], sum);
  modify(1, now, sum);
  for (auto v : e[u]) {
    if (v == fa)
      continue;
    dfs(v, u);
  }
  modify(1, now, pre);
}

int main() {

#ifdef xiaofan
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
#endif

  int n;
  read(n);
  vector<int> b;
  for (int i = 1; i <= n; i++) {
    read(a[i]);
    b.pb(a[i]);
  }
  sort(all(b));
  b.erase(unique(all(b)), b.end());
  for (int i = 1; i <= n; i++)
    a[i] = lower_bound(all(b), a[i]) - b.begin() + 2;
  for (int i = 1; i < n; i++) {
    int u, v;
    read(u, v);
    e[u].pb(v);
    e[v].pb(u);
  }
  cout << n << endl;
  build(1, 1, n + 5);
  dfs(1, 1);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", ans[i]);

  return 0;
}