Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fine cout << "fine" << endl;
#define ll long long int
#define pb push_back
#define FASTIO                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
const ll inf = (ll)2e18;
const int mod = 1e9 + 7;
bool overflow(ll a, ll b) {
  if (a <= (inf + b - 1) / b)
    return false;
  return true;
}
const int NAX = 2e5 + 5;
int n, a[NAX];
int ans[NAX];
vector<vector<int>> dp;
int bSearch(int x) {
  int start = 0, last = dp.size() - 1, mid, ans = -1;
  while (start <= last) {
    mid = (start + last) / 2;
    if (dp[mid][dp[mid].size() - 1] < x)
      start = mid + 1;
    else {
      ans = mid;
      last = mid - 1;
    }
  }
  return ans;
}
map<int, int> m;
stack<int> s;
void dfs(vector<vector<int>> &v, int i, int par) {
  int ind = bSearch(a[i]);
  if (ind == -1) {
    dp.pb({mod, a[i]});
    m[i] = dp.size() - 1;
    ans[i] = dp.size();
    s.push(ans[i]);
  } else {
    ans[i] = max(s.top(), ind + 1);
    s.push(ans[i]);
    dp[ind].pb(a[i]);
  }
  for (auto it : v[i]) {
    if (it != par) {
      dfs(v, it, i);
    }
  }
  dp[m[i]].pop_back();
  s.pop();
}
int main() {
  FASTIO;
  cin >> n;
  vector<vector<int>> v(n + 1);
  dp.reserve(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    v[x].pb(y);
    v[y].pb(x);
  }
  dfs(v, 1, -1);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fine cout << "fine" << endl;
#define ll long long int
#define pb push_back
#define FASTIO                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
const ll inf = (ll)2e18;
const int mod = 1e9 + 7;
bool overflow(ll a, ll b) {
  if (a <= (inf + b - 1) / b)
    return false;
  return true;
}
const int NAX = 2e5 + 5;
int n, a[NAX];
int ans[NAX];
vector<vector<int>> dp;
int bSearch(int x) {
  int start = 0, last = dp.size() - 1, mid, ans = -1;
  while (start <= last) {
    mid = (start + last) / 2;
    if (dp[mid][dp[mid].size() - 1] < x)
      start = mid + 1;
    else {
      ans = mid;
      last = mid - 1;
    }
  }
  return ans;
}
map<int, int> m;
stack<int> s;
void dfs(vector<vector<int>> &v, int i, int par) {
  int ind = bSearch(a[i]);
  if (ind == -1) {
    dp.pb({mod, a[i]});
    m[i] = dp.size() - 1;
    ans[i] = dp.size();
    s.push(ans[i]);
  } else {
    ans[i] = max(s.top(), ind + 1);
    m[i] = ind;
    s.push(ans[i]);
    dp[ind].pb(a[i]);
  }
  for (auto it : v[i]) {
    if (it != par) {
      dfs(v, it, i);
    }
  }
  dp[m[i]].pop_back();
  s.pop();
}
int main() {
  FASTIO;
  cin >> n;
  vector<vector<int>> v(n + 1);
  dp.reserve(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    v[x].pb(y);
    v[y].pb(x);
  }
  dfs(v, 1, -1);
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }
}