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

#include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int)v.size()
#define pr(v)                                                                  \\
  For(i, 0, sz(v)) { cout << v[i] << " "; }                                    \\
  cout << endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for (ll i = st; i < en; i++)
#define tr(x) for (auto it = x.begin(); it != x.end(); it++)
#define fast                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define ll long long
#define int long long
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define MOD 1000000007
#define INF 1000000000000000007LL

const int N = 300005;

// it's swapnil07 ;)

#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
int begtime = clock();
#define end_routine()                                                          \\
  cout << "\\n\\nTime elapsed: " << (clock() - begtime) * 1000 / CLOCKS_PER_SEC  \\
       << " ms\\n\\n";
#else
#define endl '\\n'
#define trace(...)
#define end_routine()
#endif

int a[N], ans[N];
vector<int> adj[N];

vector<int> vect;

void dfs(int u, int p) {
  bool fl = false;
  int val;
  int pos = lower_bound(all(vect), a[u]) - vect.begin();
  if (pos != sz(vect)) {
    fl = true;
    val = vect[pos];
    vect[pos] = a[u];
  } else {
    vect.pb(a[u]);
  }
  ans[u] = sz(vect);

  for (int v : adj[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
  if (!fl) {
    vect.pop_back();
  } else {
    vect[pos] = val;
  }
}

void solve() {
  int n;
  cin >> n;
  For(i, 1, n + 1) { cin >> a[i]; }
  For(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  dfs(1, 0);
  For(i, 1, n + 1) { cout << ans[i] << "\\n"; }
}

signed main() {
  fast
#ifdef SWAPNIL07
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define sz(v) (int)v.size()
#define pr(v)                                                                  \\
  For(i, 0, sz(v)) { cout << v[i] << " "; }                                    \\
  cout << endl;
#define slld(x) scanf("%lld", &x)
#define all(x) x.begin(), x.end()
#define For(i, st, en) for (ll i = st; i < en; i++)
#define tr(x) for (auto it = x.begin(); it != x.end(); it++)
#define fast                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define ll long long
#define int long long
#define mp make_pair
#define F first
#define S second
typedef pair<int, int> pii;
typedef vector<int> vi;
#define MOD 1000000007
#define INF 1000000000000000007LL

const int N = 200005;

// it's swapnil07 ;)

#ifdef SWAPNIL07
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
int begtime = clock();
#define end_routine()                                                          \\
  cout << "\\n\\nTime elapsed: " << (clock() - begtime) * 1000 / CLOCKS_PER_SEC  \\
       << " ms\\n\\n";
#else
#define endl '\\n'
#define trace(...)
#define end_routine()
#endif

int a[N], ans[N];
vector<int> adj[N];

vector<int> vect;

void dfs(int u, int p) {
  bool fl = false;
  int val;
  auto it = lower_bound(all(vect), a[u]);
  if (it != vect.end()) {
    fl = true;
    val = *it;
    *it = a[u];
  } else {
    vect.pb(a[u]);
  }
  ans[u] = sz(vect);

  for (int v : adj[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
  if (!fl) {
    vect.pop_back();
  } else {
    *it = val;
  }
}

void solve() {
  int n;
  cin >> n;
  For(i, 1, n + 1) { cin >> a[i]; }
  For(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  dfs(1, 0);
  For(i, 1, n + 1) { cout << ans[i] << "\\n"; }
}

signed main() {
  fast
#ifdef SWAPNIL07
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}