Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll, ll>
#define iii pair<ii, ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;

#define rep(x, start, end)                                                     \
  for (auto x = (start) - ((start) > (end)); x != (end) - ((start) > (end));   \
       ((start) < (end) ? x++ : x--))
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

ll MAX(ll a) { return a; }
ll MIN(ll a) { return a; }
template <typename... Args> ll MAX(ll a, Args... args) {
  return max(a, MAX(args...));
}
template <typename... Args> ll MIN(ll a, Args... args) {
  return min(a, MIN(args...));
}

#define indexed_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

int n;
int w[200005];
vector<int> al[100005];
int ans[200005];

vector<int> lis{-1};
vector<ii> changes;
//(-1,-1) is insert new element
//(pos,val) is overwrite pos which used to contain val

void dfs(int i, int p) {
  if (lis.back() < w[i]) {
    lis.push_back(w[i]);
    changes.push_back(ii(-1, -1));
  } else {
    int iter = lower_bound(all(lis), w[i]) - lis.begin();
    changes.push_back(ii(iter, lis[iter]));
    lis[iter] = w[i];
  }

  ans[i] = lis.size();

  for (auto &it : al[i]) {
    if (it == p)
      continue;

    dfs(it, i);
  }

  if (changes.back() == ii(-1, -1))
    lis.pop_back();
  else {
    lis[changes.back().fi] = changes.back().se;
  }
  changes.pop_back();
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n;
  rep(x, 1, n + 1) cin >> w[x];

  int a, b;
  rep(x, 1, n) {
    cin >> a >> b;
    al[a].push_back(b);
    al[b].push_back(a);
  }

  dfs(1, -1);

  rep(x, 1, n + 1) cout << ans[x] - 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll, ll>
#define iii pair<ii, ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;

#define rep(x, start, end)                                                     \
  for (auto x = (start) - ((start) > (end)); x != (end) - ((start) > (end));   \
       ((start) < (end) ? x++ : x--))
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

ll MAX(ll a) { return a; }
ll MIN(ll a) { return a; }
template <typename... Args> ll MAX(ll a, Args... args) {
  return max(a, MAX(args...));
}
template <typename... Args> ll MIN(ll a, Args... args) {
  return min(a, MIN(args...));
}

#define indexed_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

int n;
int w[200005];
vector<int> al[200005];
int ans[200005];

vector<int> lis{-1};
vector<ii> changes;
//(-1,-1) is insert new element
//(pos,val) is overwrite pos which used to contain val

void dfs(int i, int p) {
  if (lis.back() < w[i]) {
    lis.push_back(w[i]);
    changes.push_back(ii(-1, -1));
  } else {
    int iter = lower_bound(all(lis), w[i]) - lis.begin();
    changes.push_back(ii(iter, lis[iter]));
    lis[iter] = w[i];
  }

  ans[i] = lis.size();

  for (auto &it : al[i]) {
    if (it == p)
      continue;

    dfs(it, i);
  }

  if (changes.back() == ii(-1, -1))
    lis.pop_back();
  else {
    lis[changes.back().fi] = changes.back().se;
  }
  changes.pop_back();
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n;
  rep(x, 1, n + 1) cin >> w[x];

  int a, b;
  rep(x, 1, n) {
    cin >> a >> b;
    al[a].push_back(b);
    al[b].push_back(a);
  }

  dfs(1, -1);

  rep(x, 1, n + 1) cout << ans[x] - 1 << endl;
}