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

#include "bits/stdc++.h"

using namespace std;

#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define int long long
#define mp make_pair
#define ff first
#define ss second
#define pii pair<int, int>
#define INF LLONG_MAX
#define EPS 1e-9
#define all(v) (v).begin(), (v).end()
#define sq(x) ((x) * (x))
#define foreach(it, v) for (auto it = (v).begin(); it != (v).end(); ++it)
#define tr(...)                                                                \\
  cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \\
  trace(#__VA_ARGS__, __VA_ARGS__)

#define tr(...)                                                                \\
  cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \\
  trace(#__VA_ARGS__, __VA_ARGS__)

template <typename T> void trace(const char *name, T &&arg1) {
  cout << name << " : " << arg1 << endl;
}

template <typename T, typename... Args>
void trace(const char *names, T &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  trace(comma + 1, args...);
}

typedef long double dbl;
bool codejam = 0;

const int MOD = 1000 * 1000 * 1000 + 7;
const int NMAX = 1000 * 1000 + 11;

int arr[NMAX];
int n, m, p, q, k, a, b, c;
char ch;
vector<int> v;
vector<int> gr[200011];
multiset<int> s;
int ans[NMAX];

void dfs(int cur, int par) {

  s.insert(arr[cur]);

  auto it = s.lower_bound(arr[cur]);
  it++;
  int del = -1;

  if (it != s.end()) {
    del = *it;
    s.erase(it);
  }

  ans[cur] = s.size();

  for (auto ch : gr[cur]) {
    // cout<<ch<<" "<<cur<<" "<<par<<"\\n";
    if (ch != par) {
      dfs(ch, cur);
    }
  }
  if (del != -1)
    s.insert(del);

  s.erase(s.find(arr[cur]));
  return;
}

void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  int u, v;
  for (int i = 1; i <= n - 1; i++) {
    cin >> u >> v;
    gr[u].pb(v);
    gr[v].pb(u);
  }

  dfs(1, 1);

  for (int i = 1; i <= n; i++)
    cout << ans[i] << "\\n";
}

int32_t main() {
  fio;
  int t = 1;
  int cs = 1;
  // cin>>t;
  while (t--) {
    if (codejam) {
      cout << "Case #" << cs << ": ";
    }
    cs++;
    solve();
    cout << "\\n";
  }
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define pb push_back
#define int long long
#define mp make_pair
#define ff first
#define ss second
#define pii pair<int, int>
#define INF LLONG_MAX
#define EPS 1e-9
#define all(v) (v).begin(), (v).end()
#define sq(x) ((x) * (x))
#define foreach(it, v) for (auto it = (v).begin(); it != (v).end(); ++it)
#define tr(...)                                                                \\
  cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \\
  trace(#__VA_ARGS__, __VA_ARGS__)

#define tr(...)                                                                \\
  cout << __FUNCTION__ << ' ' << __LINE__ << " = ";                            \\
  trace(#__VA_ARGS__, __VA_ARGS__)

template <typename T> void trace(const char *name, T &&arg1) {
  cout << name << " : " << arg1 << endl;
}

template <typename T, typename... Args>
void trace(const char *names, T &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  trace(comma + 1, args...);
}

typedef long double dbl;
bool codejam = 0;

const int MOD = 1000 * 1000 * 1000 + 7;
const int NMAX = 1000 * 1000 + 11;

int arr[NMAX];
int n, m, p, q, k, a, b, c;
char ch;
vector<int> v;
vector<int> gr[200011];
set<int> s;
int ans[NMAX];

void dfs(int cur, int par) {

  s.insert(arr[cur]);

  auto it = s.lower_bound(arr[cur]);
  it++;
  int del = -1;

  if (it != s.end()) {
    del = *it;
    s.erase(it);
  }

  ans[cur] = s.size();

  for (auto ch : gr[cur]) {
    // cout<<ch<<" "<<cur<<" "<<par<<"\\n";
    if (ch != par) {
      dfs(ch, cur);
    }
  }
  if (del != -1)
    s.insert(del);

  s.erase(s.find(arr[cur]));
  return;
}

void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
  }
  int u, v;
  for (int i = 1; i <= n - 1; i++) {
    cin >> u >> v;
    gr[u].pb(v);
    gr[v].pb(u);
  }

  dfs(1, 1);

  for (int i = 1; i <= n; i++)
    cout << ans[i] << "\\n";
}

int32_t main() {
  fio;
  int t = 1;
  int cs = 1;
  // cin>>t;
  while (t--) {
    if (codejam) {
      cout << "Case #" << cs << ": ";
    }
    cs++;
    solve();
    cout << "\\n";
  }
}