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

/*
[ ( ^ _ ^ ) ]
*/
// problem: atc/abc165_f
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1000000000

#define tep template <
#define tem template <typename T
#define tee typename... E
#define szn size_t N
#define rge int x1 = 0, int x2 = 1e9
#define tst string to_string(const
tem, szn > int SIZE(const T (&t)[N]) { return N; }
tem > int SIZE(const T &t) { return t.size(); }
tst string s, rge) {
  return x1 < int(s.size()) ? s.substr(x1, x2 - x1 + 1) : "";
}
tst char *s) {return to_string((string)s);}
tst bool b) {return b?"true":"false";}
tst char c) {return string({c});}
tep szn > tst bitset<N> &b, rge) {
  string t = "";
  for (int s = min(x1, SIZE(b)), e = min(x2, SIZE(b) - 1); s <= e; ++s) {
    t += b[s] + '0';
  }
  return t;
}
tem, tee > tst T (&v), rge, E... rem);
tem, typename E > tst pair<T, E> &p)
{
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
tem, tee > tst T (&v), int x1, int x2, E... rem) {
  string res = "[";
  bool first = true;
  x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v));
  auto l = begin(v);
  advance(l, x1);
  auto r = l;
  advance(r, (x2 - x1) + (x2 < SIZE(v)));
  for (auto e = l; e != r; e = next(e), first = false) {
    if (!first) {
      res += ", ";
    }
    res += to_string(*e, rem...);
  }
  res += "]";
  return res;
}
void dbgm() { ; }
tem, tee > void dbgm(T t, E... e) {
  cout << to_string(t) << ", ";
  dbgm(e...);
}
#define dbg(...)                                                               \\
  cout << "[ " << #__VA_ARGS__ << " : ";                                       \\
  cout << to_string(__VA_ARGS__) << " ]" << endl
#define dbgm(...)                                                              \\
  cout << "[ " << #__VA_ARGS__ << " : ";                                       \\
  dbgm(__VA_ARGS__);                                                           \\
  cout << " ]" << endl

const int nax = 200005;
const int sz = 4 * nax;
vector<int> segtree(sz);
vector<vector<int>> g(nax);
vector<int> a(nax), rs(nax);
int n;

void upd(int idx, int s, int e, int p, int v) {
  // dbgm("upd", idx, s, e, p, v);
  if (p < s || e < p || e < s)
    return;
  if (s == p && e == p) {
    segtree[idx] = v;
    return;
  }
  int mid = s + (e - s) / 2;
  int l = 2 * idx;
  int r = l + 1;
  if (p <= mid) {
    upd(l, s, mid, p, v);
  } else {
    upd(r, mid + 1, e, p, v);
  }
  segtree[idx] = max(segtree[l], segtree[r]);
}

int get(int idx, int s, int e, int l, int r) {
  // dbgm("get1", idx, s, e, l, r);
  if (e < s)
    return 0;
  if (r < s || e < l)
    return 0;
  if (l <= s && e <= r) {
    return segtree[idx];
  }
  int mid = s + (e - s) / 2;
  int lc = 2 * idx;
  int rc = lc + 1;
  int x = get(lc, s, mid, l, r);
  int y = get(rc, mid + 1, e, l, r);
  return max(x, y);
}

int get(int idx, int s, int e, int p) { return get(1, s, e, p, p); }

void dfs(int v, int p) {
  int mx = get(1, 0, n - 1, 0, a[v] - 1);
  rs[v] = mx + 1;
  // dbgm(v, 0, a[v]-1, mx);
  // dbgm(v, segtree);
  int prev = get(1, 0, n - 1, a[v]);
  upd(1, 0, n - 1, a[v], max(mx + 1, prev));
  // dbgm(v, segtree);
  // dbgm(prev);
  // upd(1, 0, n-1, a[v], prev);
  // dbgm(v, segtree);
  for (int u : g[v]) {
    if (u == p)
      continue;
    dfs(u, v);
  }
  upd(1, 0, n - 1, a[v], prev);
}

void dfs2(int v, int p) {
  for (int u : g[v]) {
    if (u == p)
      continue;
    rs[u] = max(rs[u], rs[v]);
    dfs2(u, v);
  }
}

int main() {
  scanf("%d", &n);
  vector<int> b(n + 1);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    b[i] = a[i];
  }
  sort(b.begin() + 1, b.end());
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(b.begin() + 1, b.end(), a[i]) - (b.begin() + 1) + 1;
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  // dbgm(a);
  dfs(1, -1);
  dfs2(1, -1);
  for (int i = 1; i <= n; i++) {
    printf("%d\\n", rs[i]);
  }
  // printf("\\n");
}


Error: 0

Fixed submission:

/*
[ ( ^ _ ^ ) ]
*/
// problem: atc/abc165_f
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1000000000

#define tep template <
#define tem template <typename T
#define tee typename... E
#define szn size_t N
#define rge int x1 = 0, int x2 = 1e9
#define tst string to_string(const
tem, szn > int SIZE(const T (&t)[N]) { return N; }
tem > int SIZE(const T &t) { return t.size(); }
tst string s, rge) {
  return x1 < int(s.size()) ? s.substr(x1, x2 - x1 + 1) : "";
}
tst char *s) {return to_string((string)s);}
tst bool b) {return b?"true":"false";}
tst char c) {return string({c});}
tep szn > tst bitset<N> &b, rge) {
  string t = "";
  for (int s = min(x1, SIZE(b)), e = min(x2, SIZE(b) - 1); s <= e; ++s) {
    t += b[s] + '0';
  }
  return t;
}
tem, tee > tst T (&v), rge, E... rem);
tem, typename E > tst pair<T, E> &p)
{
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
tem, tee > tst T (&v), int x1, int x2, E... rem) {
  string res = "[";
  bool first = true;
  x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v));
  auto l = begin(v);
  advance(l, x1);
  auto r = l;
  advance(r, (x2 - x1) + (x2 < SIZE(v)));
  for (auto e = l; e != r; e = next(e), first = false) {
    if (!first) {
      res += ", ";
    }
    res += to_string(*e, rem...);
  }
  res += "]";
  return res;
}
void dbgm() { ; }
tem, tee > void dbgm(T t, E... e) {
  cout << to_string(t) << ", ";
  dbgm(e...);
}
#define dbg(...)                                                               \\
  cout << "[ " << #__VA_ARGS__ << " : ";                                       \\
  cout << to_string(__VA_ARGS__) << " ]" << endl
#define dbgm(...)                                                              \\
  cout << "[ " << #__VA_ARGS__ << " : ";                                       \\
  dbgm(__VA_ARGS__);                                                           \\
  cout << " ]" << endl

const int nax = 12; // 200005;
const int sz = 48;  // 4 * nax;
vector<int> segtree(sz);
vector<vector<int>> g(nax);
vector<int> a(nax), o(nax), rs(nax);
int n;

void upd(int idx, int s, int e, int p, int v) {
  // dbgm("upd", idx, s, e, p, v);
  if (p < s || e < p || e < s)
    return;
  if (s == p && e == p) {
    segtree[idx] = v;
    return;
  }
  int mid = s + (e - s) / 2;
  int l = 2 * idx, r = l + 1;
  if (p <= mid) {
    upd(l, s, mid, p, v);
  } else {
    upd(r, mid + 1, e, p, v);
  }
  segtree[idx] = max(segtree[l], segtree[r]);
}

int get(int idx, int s, int e, int l, int r) {
  // dbgm("get1", idx, s, e, l, r);
  if (e < s)
    return 0;
  if (r < s || e < l)
    return 0;
  if (l <= s && e <= r) {
    return segtree[idx];
  }
  int mid = s + (e - s) / 2;
  int lc = 2 * idx, rc = lc + 1;
  int x = get(lc, s, mid, l, r);
  int y = get(rc, mid + 1, e, l, r);
  return max(x, y);
}

int get(int idx, int s, int e, int p) {
  // dbgm("get2", idx, s, e, p);
  if (s == p && e == p) {
    return segtree[idx];
  }
  int mid = s + (e - s) / 2;
  int l = 2 * idx, r = l + 1;
  if (p <= mid) {
    return get(l, s, mid, p);
  }
  return get(r, mid + 1, e, p);
}

void dfs(int v, int p) {
  int mx = get(1, 0, n - 1, 0, a[v] - 1);
  rs[v] = mx + 1;
  // dbgm(v, 0, a[v]-1, mx);
  // dbgm(v, segtree);
  int prev = get(1, 0, n - 1, a[v]);
  upd(1, 0, n - 1, a[v], max(mx + 1, prev));
  // dbgm(v, segtree);
  // dbgm(prev);
  // upd(1, 0, n-1, a[v], prev);
  // dbgm(v, segtree);
  for (int u : g[v]) {
    if (u == p)
      continue;
    dfs(u, v);
  }
  upd(1, 0, n - 1, a[v], prev);
}

int main() {
  scanf("%d", &n);
  vector<int> b(n + 1);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    b[i] = a[i];
    o[i] = a[i];
  }
  sort(b.begin() + 1, b.end());
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(b.begin() + 1, b.end(), a[i]) - (b.begin() + 1) + 1;
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  // dbgm(a);
  dfs(1, -1);
  for (int i = 1; i <= n; i++) {
    printf("%d\\n", rs[i]);
  }
}