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

/* Simplicity and Goodness */

#include <bits/stdc++.h>
using namespace std;
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld ", n)
#define pri(n) printf("%d ", (int)n)
#define prin(n) printf("%d\\n", (int)n)
#define lprin(n) printf("%lld\\n", n)
#define deb(x) cout << #x << ' ' << x << '\\n'
#define rep(i, ini, n) for (int i = ini; i < (int)n; i++)
#define per(i, ini) for (int i = ini; i >= 0; i--)
#define tc                                                                     \\
  int tt;                                                                      \\
  scn(tt);                                                                     \\
  while (tt--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using ll = long long;
const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 998244353;
const int N = 2e5 + 2;

vector<int> v[N];
set<int> s;
int val[N], ans[N], removed[N];

void dfs(int node, int par) {
  auto i = s.lower_bound(val[node]);

  if (i != s.end()) {
    removed[node] = *i;
    s.erase(*i);
  } else
    removed[node] = -1;

  s.insert(val[node]);

  ans[node] = (int)s.size();

  for (int i : v[node])
    if (i != par)
      dfs(i, node);

  s.erase(val[node]);

  if (removed[node] != -1)
    s.insert(removed[node]);
}

int main() {
  int n;
  scn(n);

  rep(i, 1, n + 1) scn(val[i]);

  rep(i, 1, n) {
    int a, b;
    scn(a);
    scn(b);

    v[a].pb(b);
    v[b].pb(a);
  }

  dfs(1, -1);

  rep(i, 1, n + 1) prin(ans[i]);

  return 0;
}

Error: 0

Fixed submission:

/* Simplicity and Goodness */

#include <bits/stdc++.h>
using namespace std;
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define lpri(n) printf("%lld ", n)
#define pri(n) printf("%d ", (int)n)
#define prin(n) printf("%d\\n", (int)n)
#define lprin(n) printf("%lld\\n", n)
#define deb(x) cout << #x << ' ' << x << '\\n'
#define rep(i, ini, n) for (int i = ini; i < (int)n; i++)
#define per(i, ini) for (int i = ini; i >= 0; i--)
#define tc                                                                     \\
  int tt;                                                                      \\
  scn(tt);                                                                     \\
  while (tt--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using ll = long long;
const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 998244353;
const int N = 1e5 + 2;

vector<int> v[N];
set<int> s;
int val[N], ans[N], removed[N];

void dfs(int node, int par) {
  auto i = s.lower_bound(val[node]);

  if (i != s.end()) {
    removed[node] = *i;
    s.erase(*i);
  } else
    removed[node] = -1;

  s.insert(val[node]);

  ans[node] = (int)s.size();

  for (int i : v[node])
    if (i != par)
      dfs(i, node);

  s.erase(val[node]);

  if (removed[node] != -1)
    s.insert(removed[node]);
}

int main() {
  int n;
  scn(n);

  rep(i, 1, n + 1) scn(val[i]);

  rep(i, 1, n) {
    int a, b;
    scn(a);
    scn(b);

    v[a].pb(b);
    v[b].pb(a);
  }

  dfs(1, -1);

  rep(i, 1, n + 1) prin(ans[i]);

  return 0;
}