Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 1e5 + 5;

typedef pair<int, int> pii;
#define x first
#define y second

pii a[sz];

vector<int> g[sz];

int tree[sz * 4], ans[sz];

void update(int node, int b, int e, int x, int v) {
  if (x < b || e < x)
    return;
  if (b == e) {
    tree[node] += v;
    return;
  }

  int l = node << 1, r = l + 1, m = b + e >> 1;

  update(l, b, m, x, v);
  update(r, m + 1, e, x, v);
  tree[node] = max(tree[l], tree[r]);
}

int segq(int node, int b, int e, int i, int j) {
  if (i > j || e < i || j < b)
    return 0;
  if (i <= b && e <= j)
    return tree[node];

  int l = node << 1, r = l + 1, m = b + e >> 1;

  return max(segq(l, b, m, i, j), segq(r, m + 1, e, i, j));
}

int heavy[sz], depth[sz], head[sz], par[sz], pos[sz], cpos;

int dfs(int u = 1) {
  int subc = 1, mxc = 0;
  for (int v : g[u])
    if (v ^ par[u]) {
      par[v] = u;
      depth[v] = depth[u] + 1;
      int nxtc = dfs(v);
      subc += nxtc;
      if (nxtc > mxc)
        mxc = nxtc, heavy[u] = v;
    }
  return subc;
}

void hfs(int u = 1, int h = 1) {
  head[u] = h, pos[u] = ++cpos;
  int hv = heavy[u];
  if (hv)
    hfs(hv, h);

  for (int v : g[u])
    if (v ^ par[u] && v ^ hv)
      hfs(v, v);
}

int hldq(int u, int v, int n) {
  int ans = 0;
  while (head[u] ^ head[v]) {
    if (depth[head[u]] < depth[head[v]])
      swap(u, v);
    ans = max(ans, segq(1, 1, n, pos[head[u]], pos[u]));
    u = par[head[u]];
  }

  if (depth[u] > depth[v])
    swap(u, v);
  return max(ans, segq(1, 1, n, pos[u], pos[v]));
}

void go(int u = 1, int p = 0) {
  for (int v : g[u])
    if (v ^ p) {
      ans[v] = max(ans[v], ans[u]);
      go(v, u);
    }
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i].x), a[i].y = i;
  sort(a + 1, a + n + 1);

  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }

  dfs();
  hfs();

  for (int i = 1; i <= n;) {
    vector<int> now;
    for (int j = i; i <= n && a[i].x == a[j].x; i++) {
      int u = a[i].y;
      now.push_back(u);
      ans[u] = hldq(1, u, n) + 1;
    }
    for (int u : now)
      update(1, 1, n, pos[u], ans[u]);
  }

  go();
  for (int i = 1; i <= n; i++)
    printf("%d\n", ans[i]);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 2e5 + 5;

typedef pair<int, int> pii;
#define x first
#define y second

pii a[sz];

vector<int> g[sz];

int tree[sz * 4], ans[sz];

void update(int node, int b, int e, int x, int v) {
  if (x < b || e < x)
    return;
  if (b == e) {
    tree[node] += v;
    return;
  }

  int l = node << 1, r = l + 1, m = b + e >> 1;

  update(l, b, m, x, v);
  update(r, m + 1, e, x, v);
  tree[node] = max(tree[l], tree[r]);
}

int segq(int node, int b, int e, int i, int j) {
  if (i > j || e < i || j < b)
    return 0;
  if (i <= b && e <= j)
    return tree[node];

  int l = node << 1, r = l + 1, m = b + e >> 1;

  return max(segq(l, b, m, i, j), segq(r, m + 1, e, i, j));
}

int heavy[sz], depth[sz], head[sz], par[sz], pos[sz], cpos;

int dfs(int u = 1) {
  int subc = 1, mxc = 0;
  for (int v : g[u])
    if (v ^ par[u]) {
      par[v] = u;
      depth[v] = depth[u] + 1;
      int nxtc = dfs(v);
      subc += nxtc;
      if (nxtc > mxc)
        mxc = nxtc, heavy[u] = v;
    }
  return subc;
}

void hfs(int u = 1, int h = 1) {
  head[u] = h, pos[u] = ++cpos;
  int hv = heavy[u];
  if (hv)
    hfs(hv, h);

  for (int v : g[u])
    if (v ^ par[u] && v ^ hv)
      hfs(v, v);
}

int hldq(int u, int v, int n) {
  int ans = 0;
  while (head[u] ^ head[v]) {
    if (depth[head[u]] < depth[head[v]])
      swap(u, v);
    ans = max(ans, segq(1, 1, n, pos[head[u]], pos[u]));
    u = par[head[u]];
  }

  if (depth[u] > depth[v])
    swap(u, v);
  return max(ans, segq(1, 1, n, pos[u], pos[v]));
}

void go(int u = 1, int p = 0) {
  for (int v : g[u])
    if (v ^ p) {
      ans[v] = max(ans[v], ans[u]);
      go(v, u);
    }
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i].x), a[i].y = i;
  sort(a + 1, a + n + 1);

  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }

  dfs();
  hfs();

  for (int i = 1; i <= n;) {
    vector<int> now;
    for (int j = i; i <= n && a[i].x == a[j].x; i++) {
      int u = a[i].y;
      now.push_back(u);
      ans[u] = hldq(1, u, n) + 1;
    }
    for (int u : now)
      update(1, 1, n, pos[u], ans[u]);
  }

  go();
  for (int i = 1; i <= n; i++)
    printf("%d\n", ans[i]);
}