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

#include <algorithm>
#include <cstdio>
#include <set>
#include <vector>
using namespace std;
vector<vector<int>> vt;
int n;
int a[200010];
vector<int> vt1;
multiset<int> st[200010];
int seg[800000];
int dap[200010];
int gap[200010];
int getidx(int x) {
  return lower_bound(vt1.begin(), vt1.end(), x) - vt1.begin();
}
int update(int pos, int val, int node, int x, int y) {
  if (pos < x || y < pos)
    return seg[node];
  if (x == y)
    return seg[node] = val;
  int mid = (x + y) / 2;
  return seg[node] = max(update(pos, val, node * 2, x, mid),
                         update(pos, val, node * 2 + 1, mid + 1, y));
}
int query(int lo, int hi, int node, int x, int y) {
  if (hi < x || y < lo)
    return 0;
  if (lo <= x && y <= hi)
    return seg[node];
  int mid = (x + y) / 2;
  return max(query(lo, hi, node * 2, x, mid),
             query(lo, hi, node * 2 + 1, mid + 1, y));
}
void dfs(int here, int par) {
  int idx = getidx(a[here]);
  if (idx == 0)
    dap[here] = 1;
  else
    dap[here] = query(0, idx - 1, 1, 0, vt.size()) + 1;
  st[idx].insert(dap[here]);
  auto it = st[idx].end();
  it--;
  update(idx, *it, 1, 0, vt.size());
  gap[here] = query(0, vt.size(), 1, 0, vt.size());
  for (int i = 0; i < vt[here].size(); i++) {
    int next = vt[here][i];
    if (next == par)
      continue;
    dfs(next, here);
  }
  it = lower_bound(st[idx].begin(), st[idx].end(), dap[here]);
  st[idx].erase(it);
  if (st[idx].size()) {
    it = st[idx].end();
    it--;
    update(idx, *it, 1, 0, vt.size());
  } else
    update(idx, 0, 1, 0, vt.size());
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    vt1.push_back(a[i]);
  }
  sort(vt1.begin(), vt1.end());
  vt1.erase(unique(vt1.begin(), vt1.end()), vt1.end());
  vt.resize(n + 1);
  for (int i = 0; i < n - 1; i++) {
    int q, w;
    scanf("%d %d", &q, &w);
    vt[q].push_back(w);
    vt[w].push_back(q);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", gap[i]);
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <set>
#include <vector>
using namespace std;
vector<vector<int>> vt;
int n;
int a[200010];
vector<int> vt1;
multiset<int> st[200010];
int seg[800000];
int dap[200010];
int getidx(int x) {
  return lower_bound(vt1.begin(), vt1.end(), x) - vt1.begin();
}
int update(int pos, int val, int node, int x, int y) {
  if (pos < x || y < pos)
    return seg[node];
  if (x == y)
    return seg[node] = val;
  int mid = (x + y) / 2;
  return seg[node] = max(update(pos, val, node * 2, x, mid),
                         update(pos, val, node * 2 + 1, mid + 1, y));
}
int query(int lo, int hi, int node, int x, int y) {
  if (hi < x || y < lo)
    return 0;
  if (lo <= x && y <= hi)
    return seg[node];
  int mid = (x + y) / 2;
  return max(query(lo, hi, node * 2, x, mid),
             query(lo, hi, node * 2 + 1, mid + 1, y));
}
void dfs(int here, int par) {
  int idx = getidx(a[here]);
  if (idx == 0)
    dap[here] = 1;
  else
    dap[here] = query(0, idx - 1, 1, 0, vt.size()) + 1;
  auto it = st[idx].end();
  it--;
  update(idx, *it, 1, 0, vt.size());
  st[idx].insert(dap[here]);
  for (int i = 0; i < vt[here].size(); i++) {
    int next = vt[here][i];
    if (next == par)
      continue;
    dfs(next, here);
  }
  it = lower_bound(st[idx].begin(), st[idx].end(), dap[here]);
  st[idx].erase(it);
  if (st[idx].size()) {
    it = st[idx].end();
    it--;
    update(idx, *it, 1, 0, vt.size());
  } else
    update(idx, 0, 1, 0, vt.size());
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    vt1.push_back(a[i]);
  }
  sort(vt1.begin(), vt1.end());
  vt1.erase(unique(vt1.begin(), vt1.end()), vt1.end());
  vt.resize(n + 1);
  for (int i = 0; i < n - 1; i++) {
    int q, w;
    scanf("%d %d", &q, &w);
    vt[q].push_back(w);
    vt[w].push_back(q);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", dap[i]);
}