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

#include <bits/stdc++.h> //Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(), (v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define MOD (1000000007)
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  return out << "(" << a.first << "," << a.second << ")";
}
template <class A> ostream &operator<<(ostream &out, const vector<A> &a) {
  for (const A &it : a)
    out << it << " ";
  return out;
}
template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
  return in >> a.first >> a.second;
}
template <class A> istream &operator>>(istream &in, vector<A> &a) {
  for (A &i : a)
    in >> i;
  return in;
}
ifstream cinn("input.txt");
ofstream coutt("output.txt");

int n, a[200005], ans[200005];
vi adj[200005];
multiset<int> st;

void dfs(int cur, int par) {

  st.insert(a[cur]);
  auto it = st.lower_bound(a[cur]);
  int del = 0;
  if (++it != st.end()) {
    del = *it;
    st.erase(st.find(del));
  }

  ans[cur] = st.size();

  for (int ch : adj[cur]) {
    if (ch == par)
      continue;
    dfs(ch, cur);
  }

  if (del)
    st.insert(del);
  st.erase(st.find(a[cur]));
}

signed main() {
  FASTER;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(1, -1);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << "\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h> //Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(), (v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define MOD (1000000007)
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  return out << "(" << a.first << "," << a.second << ")";
}
template <class A> ostream &operator<<(ostream &out, const vector<A> &a) {
  for (const A &it : a)
    out << it << " ";
  return out;
}
template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
  return in >> a.first >> a.second;
}
template <class A> istream &operator>>(istream &in, vector<A> &a) {
  for (A &i : a)
    in >> i;
  return in;
}
ifstream cinn("input.txt");
ofstream coutt("output.txt");

int n, a[100005], ans[100005];
vi adj[100005];
multiset<int> st;

void dfs(int cur, int par) {

  st.insert(a[cur]);
  auto it = st.upper_bound(a[cur]);
  int del = 0;
  if (it != st.end()) {
    del = *it;
    st.erase(st.find(del));
  }

  ans[cur] = st.size();

  for (int ch : adj[cur]) {
    if (ch == par)
      continue;
    dfs(ch, cur);
  }

  if (del)
    st.insert(del);
  st.erase(st.find(a[cur]));
}

signed main() {
  FASTER;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(1, -1);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << "\\n";
}