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

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;

template <class T> inline void checkmin(T &a, const T &b) {
  if (b < a)
    a = b;
}
template <class T> inline void checkmax(T &a, const T &b) {
  if (b > a)
    a = b;
}

const int inf = 1e9 + 10;
const int maxn = 2e5 + 10;

int dp[maxn], n, a[maxn];
vector<int> g[maxn];
vector<pair<int, int>> v;
int tree[maxn * 4], pos[maxn], l[maxn], ans[maxn];

void update(int x, int l, int r, int pos, int val) {
  if (l == r) {
    tree[x] = val;
    return;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid)
    update(x << 1, l, mid, pos, val);
  else
    update(x << 1 | 1, mid + 1, r, pos, val);
  tree[x] = max(tree[x << 1], tree[x << 1 | 1]);
}

int getval(int x, int l, int r, int cl, int cr) {
  if (r < cl || l > cr)
    return 0ll;
  if (cl <= l && r <= cr)
    return tree[x];
  int mid = (l + r) >> 1;
  int val1 = getval(x << 1, l, mid, cl, cr);
  int val2 = getval(x << 1 | 1, mid + 1, r, cl, cr);
  return max(val1, val2);
}

void dfs(int x, int fa) {
  if (fa != -1) {
    if (l[x] < 0) {
      dp[x] = 1;
    } else {
      int mx = getval(1, 0, n - 1, 0, l[x]);
      dp[x] = mx + 1;
    }
  }
  update(1, 0, n - 1, pos[x], dp[x]);
  ans[x] = getval(1, 0, n - 1, 0, n - 1);
  for (int i = 0; i < g[x].size(); i++) {
    int to = g[x][i];
    if (to == fa)
      continue;
    dfs(to, x);
  }
  update(1, 0, n - 1, pos[x], 0);
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    g[u].pb(v);
    g[v].pb(u);
  }
  for (int i = 0; i < n; i++)
    v.pb(mp(a[i], i));
  sort(v.begin(), v.end());
  int lst = -1;
  for (int i = 0; i < v.size(); i++) {
    pos[v[i].snd] = i;
    l[v[i].snd] = lst;
    if (i + 1 < n && v[i].fst != v[i + 1].fst)
      lst = i;
  }
  dp[0] = 1;
  ans[0] = 1;
  update(1, 0, n - 1, pos[0], dp[0]);
  dfs(0, -1);
  for (int i = 0; i < n; i++)
    printf("%d\\n", ans[i]);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;

template <class T> inline void checkmin(T &a, const T &b) {
  if (b < a)
    a = b;
}
template <class T> inline void checkmax(T &a, const T &b) {
  if (b > a)
    a = b;
}

const int inf = 1e9 + 10;
const int maxn = 2e5 + 10;

int dp[maxn], n, a[maxn];
vector<int> g[maxn];
vector<pair<int, int>> v;
int tree[maxn], pos[maxn], l[maxn];

void update(int x, int l, int r, int pos, int val) {
  if (l == r) {
    tree[x] = val;
    return;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid)
    update(x << 1, l, mid, pos, val);
  else
    update(x << 1 | 1, mid + 1, r, pos, val);
  tree[x] = max(tree[x << 1], tree[x << 1 | 1]);
}

int getval(int x, int l, int r, int cl, int cr) {
  if (r < cl || l > cr)
    return 0ll;
  if (cl <= l && r <= cr)
    return tree[x];
  int mid = (l + r) >> 1;
  int val1 = getval(x << 1, l, mid, cl, cr);
  int val2 = getval(x << 1 | 1, mid + 1, r, cl, cr);
  return max(val1, val2);
}

void dfs(int x, int fa) {
  if (fa != -1) {
    if (l[x] < 0) {
      dp[x] = 1;
    } else {
      int mx = getval(1, 0, n - 1, 0, l[x]);
      dp[x] = mx + 1;
    }
  }
  update(1, 0, n - 1, pos[x], dp[x]);
  for (int i = 0; i < g[x].size(); i++) {
    int to = g[x][i];
    if (to == fa)
      continue;
    dfs(to, x);
  }
  update(1, 0, n - 1, pos[x], 0);
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    g[u].pb(v);
    g[v].pb(u);
  }
  for (int i = 0; i < n; i++)
    v.pb(mp(a[i], i));
  sort(v.begin(), v.end());
  int lst = -1;
  for (int i = 0; i < v.size(); i++) {
    pos[v[i].snd] = i;
    l[v[i].snd] = lst;
    if (i + 1 < n && v[i].fst != v[i + 1].fst)
      lst = i;
  }
  dp[0] = 1;
  update(1, 0, n - 1, pos[0], dp[0]);
  dfs(0, -1);
  for (int i = 0; i < n; i++)
    printf("%d\\n", dp[i]);
  return 0;
}