Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
ll qpow(ll a, ll b) {
  ll res = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
struct graph {
  int head[maxn], nxt[maxn << 1], to[maxn << 1], w[maxn << 1], sz;
  void init() { memset(head, -1, sizeof(head)); }
  graph() { init(); }
  void push(int a, int b, int c) {
    nxt[sz] = head[a], to[sz] = b, w[sz] = c, head[a] = sz++;
  }
  int &operator[](const int a) { return to[a]; }
} g;
int n, a[maxn], b[maxn], m;
struct BIT {
  int bit[maxn];
  vector<pair<int, int>> update(int x, int k) {
    vector<pair<int, int>> v;
    while (x <= m) {
      if (bit[x] < k) {
        v.push_back(make_pair(x, bit[x]));
        bit[x] = k;
      }
      x += x & -x;
    }
    return v;
  }
  int query(int x) {
    int tmp = 0;
    while (x) {
      tmp = max(tmp, bit[x]);
      x -= x & -x;
    }
    return tmp;
  }
} bit;
int ans[maxn];
void dfs(int now, int pre) {
  ans[now] = bit.query(a[now] - 1) + 1;
  vector<pair<int, int>> v = bit.update(a[now], ans[now]);
  ;
  ans[now] = max(ans[now], ans[pre]);
  for (int i = g.head[now]; ~i; i = g.nxt[i]) {
    if (g[i] == pre)
      continue;
    dfs(g[i], now);
  }
  for (int i = 0; i < v.size(); ++i) {
    bit.bit[v[i].first] = v[i].second;
  }
}
int main() {
  freopen("creatTree10.txt", "r", stdin);
  freopen("simple.out", "w", stdout);
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]), b[i] = a[i];
  sort(b + 1, b + 1 + n);
  m = unique(b + 1, b + 1 + n) - b - 1;
  for (int i = 1; i <= n; ++i)
    a[i] = lower_bound(b + 1, b + 1 + m, a[i]) - b;
  for (int i = 1; i < n; ++i) {
    int c, d;
    scanf("%d%d", &c, &d);
    g.push(c, d, 0), g.push(d, c, 0);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; ++i)
    printf("%d\n", ans[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
ll qpow(ll a, ll b) {
  ll res = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
struct graph {
  int head[maxn], nxt[maxn << 1], to[maxn << 1], w[maxn << 1], sz;
  void init() { memset(head, -1, sizeof(head)); }
  graph() { init(); }
  void push(int a, int b, int c) {
    nxt[sz] = head[a], to[sz] = b, w[sz] = c, head[a] = sz++;
  }
  int &operator[](const int a) { return to[a]; }
} g;
int n, a[maxn], b[maxn], m;
struct BIT {
  int bit[maxn];
  vector<pair<int, int>> update(int x, int k) {
    vector<pair<int, int>> v;
    while (x <= m) {
      if (bit[x] < k) {
        v.push_back(make_pair(x, bit[x]));
        bit[x] = k;
      }
      x += x & -x;
    }
    return v;
  }
  int query(int x) {
    int tmp = 0;
    while (x) {
      tmp = max(tmp, bit[x]);
      x -= x & -x;
    }
    return tmp;
  }
} bit;
int ans[maxn];
void dfs(int now, int pre) {
  ans[now] = bit.query(a[now] - 1) + 1;
  vector<pair<int, int>> v = bit.update(a[now], ans[now]);
  ;
  ans[now] = max(ans[now], ans[pre]);
  for (int i = g.head[now]; ~i; i = g.nxt[i]) {
    if (g[i] == pre)
      continue;
    dfs(g[i], now);
  }
  for (int i = 0; i < v.size(); ++i) {
    bit.bit[v[i].first] = v[i].second;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]), b[i] = a[i];
  sort(b + 1, b + 1 + n);
  m = unique(b + 1, b + 1 + n) - b - 1;
  for (int i = 1; i <= n; ++i)
    a[i] = lower_bound(b + 1, b + 1 + m, a[i]) - b;
  for (int i = 1; i < n; ++i) {
    int c, d;
    scanf("%d%d", &c, &d);
    g.push(c, d, 0), g.push(d, c, 0);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; ++i)
    printf("%d\n", ans[i]);
  return 0;
}