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

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define debug(x)                                                               \\
  cout << #x << ":";                                                           \\
  cout << x << endl;
#define debugs(v)                                                              \\
  cout << #v << ":";                                                           \\
  for (auto x : v) {                                                           \\
    cout << x << ' ';                                                          \\
  }                                                                            \\
  cout << endl;
#define INF 1000000000
#define mod 1000000007
using ll = long long;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int n;
vector<ll> a;
vector<vector<int>> g;
vector<ll> ans;
vector<ll> lis(200010, LINF);
ll L;

void dfs(int par, int now) {

  // cout<<par<<" "<<now<<endl;

  auto ite = lower_bound(ALL(lis), a[now]);
  ll prea = *ite;
  bool f = false;
  if (*ite == LINF)
    f = true, L++;
  *ite = a[now];

  ans[now] = L;

  for (int to : g[now])
    if (par != to) {
      dfs(now, to);
    }
  if (f)
    L--;
  *ite = prea;
}

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

  cin >> n;
  a.resize(n);
  g.resize(n);
  ans.resize(n);
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  dfs(-1, 0);

  for (auto x : ans)
    cout << x << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define debug(x)                                                               \\
  cout << #x << ":";                                                           \\
  cout << x << endl;
#define debugs(v)                                                              \\
  cout << #v << ":";                                                           \\
  for (auto x : v) {                                                           \\
    cout << x << ' ';                                                          \\
  }                                                                            \\
  cout << endl;
#define INF 1000000000
#define mod 1000000007
using ll = long long;
const ll LINF = 1001002003004005006ll;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int n;
vector<ll> a;
vector<vector<int>> g;
vector<ll> ans;
vector<ll> lis;

void dfs(int par, int now) {

  // cout<<par<<" "<<now<<endl;

  auto ite = lower_bound(ALL(lis), a[now]);
  bool f = false;
  ll prea;
  if (ite == lis.end()) {
    f = true;
    lis.push_back(a[now]);
  } else {
    prea = *ite;
    *ite = a[now];
  }
  ans[now] = (int)lis.size();
  for (int to : g[now]) {
    if (par == to)
      continue;
    dfs(now, to);
  }
  if (f) {
    lis.pop_back();
  } else {
    *ite = prea;
  }
}

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

  cin >> n;
  a.resize(n);
  g.resize(n);
  ans.resize(n);
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  dfs(-1, 0);

  for (auto x : ans)
    cout << x << endl;
  return 0;
}