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

#include <bits/stdc++.h>
using namespace std;

const int N = 200005;

int n;
int a[N];

vector<int> edges[N];

struct SegTree {
  int maxi[N << 2];

  void update(int node, int l, int r, int x, int val) {
    if (l > x || r < x)
      return;
    if (l == x && r == x) {
      maxi[node] = val;
      return;
    }

    int mid = (l + r) >> 1;

    update(node * 2 + 1, l, mid + 0, x, val);
    update(node * 2 + 2, mid + 1, r, x, val);
    maxi[node] = max(maxi[node * 2 + 1], maxi[node * 2 + 2]);
  }

  int query(int node, int l, int r, int ll, int rr) {
    if (l > rr || r < ll)
      return 0;
    if (l >= ll && r <= rr)
      return maxi[node];

    int mid = (l + r) >> 1;

    return max(query(node * 2 + 1, l, mid + 0, ll, rr),
               query(node * 2 + 2, mid + 1, r, ll, rr));
  }

  inline void update(int x, int val) { update(0, 0, n - 1, x, val); }

  inline int query(int l, int r) { return query(0, 0, n - 1, l, r); }
} tree;

void compress(int n, int a[]) {
  vector<int> v(a, a + n);
  sort(v.begin(), v.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
  for (int i = 0; i < n; ++i) {
    a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
  }
}

int ans[N];
void dfs(int u, int p, int maxi) {
  int tq = tree.query(a[u], a[u]);
  int nq = tree.query(0, a[u] - 1) + 1;
  if (nq > tq)
    tree.update(a[u], nq);
  maxi = max(maxi, nq);

  ans[u] = maxi;

  for (int v : edges[u]) {
    if (v == p)
      continue;
    dfs(v, u, maxi);
  }

  if (nq > tq)
    tree.update(a[u], tq);
}

int solve() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  compress(n, a);

  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    --u, --v;

    edges[u].push_back(v);
    edges[v].push_back(u);
  }

  dfs(0, 0, 0);

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

  return 0;
}

int main() {
  int t = 1;
  // scanf("%d", &t);
  while (t--)
    solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int N = 200005;

int n;
int a[N];

vector<int> edges[N];

struct SegTree {
  int maxi[N];

  void update(int node, int l, int r, int x, int val) {
    if (l > x || r < x)
      return;
    if (l == x && r == x) {
      maxi[node] = val;
      return;
    }

    int mid = (l + r) >> 1;

    update(node * 2 + 1, l, mid + 0, x, val);
    update(node * 2 + 2, mid + 1, r, x, val);
    maxi[node] = max(maxi[node * 2 + 1], maxi[node * 2 + 2]);
  }

  int query(int node, int l, int r, int ll, int rr) {
    if (l > rr || r < ll)
      return 0;
    if (l >= ll && r <= rr)
      return maxi[node];

    int mid = (l + r) >> 1;

    return max(query(node * 2 + 1, l, mid + 0, ll, rr),
               query(node * 2 + 2, mid + 1, r, ll, rr));
  }

  inline void update(int x, int val) { update(0, 0, n - 1, x, val); }

  inline int query(int l, int r) { return query(0, 0, n - 1, l, r); }
} tree;

void compress(int n, int a[]) {
  vector<int> v(a, a + n);
  sort(v.begin(), v.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
  for (int i = 0; i < n; ++i) {
    a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
  }
}

int ans[N];
void dfs(int u, int p, int maxi) {
  int tq = tree.query(a[u], a[u]);
  int nq = tree.query(0, a[u] - 1) + 1;
  if (nq > tq)
    tree.update(a[u], nq);
  maxi = max(maxi, nq);

  ans[u] = maxi;

  for (int v : edges[u]) {
    if (v == p)
      continue;
    dfs(v, u, maxi);
  }

  if (nq > tq)
    tree.update(a[u], tq);
}

int solve() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  compress(n, a);

  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    --u, --v;

    edges[u].push_back(v);
    edges[v].push_back(u);
  }

  dfs(0, 0, 0);

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

  return 0;
}

int main() {
  int t = 1;
  // scanf("%d", &t);
  while (t--)
    solve();
  return 0;
}