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

#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <vector>

// BE CAREFUL WITH HASH TABLE & UNORDERED MAP
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;

#define MOD 998244353
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define ll long long
#define pii pair<int, int>
#define add(a, b) ((int)(((ll)(a) + (b)) % MOD))
#define mul(a, b) ((int)(((ll)(a) * (b)) % MOD))
#define PB emplace_back
#define MP make_pair
#define F first
#define S second

// ifstream cin ("cinput.in");
// ofstream cout ("coutput.out");

int ct = 0;
int N, a, b;
int vals[200000];
int ans[200000];
vector<int> graph[200000];
map<int, int> dp;
pii insd[200000];
pii deld[200000];

// ins. if same val exists, should already be optimal

int ins(int node) { // insert and delete suboptimal
  auto it = dp.lower_bound(vals[node]);
  if (it->F != vals[node]) {
    if (it == dp.begin()) {
      insd[node] = MP(vals[node], 1);
      dp[vals[node]] = 1;
    } else if (prev(it)->S < vals[node]) {
      int asdf = prev(it)->S + 1;
      insd[node] = MP(vals[node], asdf);
      dp[vals[node]] = asdf;
    }
    // cout << it->S << '\\n';
    if (it != dp.end() && it->S <= prev(it)->S) {
      int sd = it->F;
      int df = it->S;
      deld[node] = MP(sd, df);
      dp.erase(it);
    }
  }
  int sdf = prev(dp.end())->S;
  return sdf;
}

void dfs(int node, int par) {
  ans[node] = ins(node);
  ct++;
  for (int ne : graph[node]) {
    if (ne == par)
      continue;
    dfs(ne, node);
  }
  if (deld[node].F != -1)
    dp[deld[node].F] = deld[node].S;
  if (insd[node].F != -1)
    dp.erase(dp.find(insd[node].F));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N;
  F0R(i, N) {
    insd[i] = MP(-1, -1);
    deld[i] = MP(-1, -1);
  }
  F0R(i, N) cin >> vals[i];
  F0R(i, N - 1) {
    cin >> a >> b;
    a--;
    b--;
    graph[a].PB(b);
    graph[b].PB(a);
  }
  dfs(0, -1);
  F0R(i, N) cout << ans[i] << '\\n';
}

Error: 0

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <vector>

// BE CAREFUL WITH HASH TABLE & UNORDERED MAP
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;

#define MOD 998244353
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
#define ll long long
#define pii pair<int, int>
#define add(a, b) ((int)(((ll)(a) + (b)) % MOD))
#define mul(a, b) ((int)(((ll)(a) * (b)) % MOD))
#define PB emplace_back
#define MP make_pair
#define F first
#define S second

// ifstream cin ("cinput.in");
// ofstream cout ("coutput.out");

int N, a, b;
int vals[200000];
int ans[200000];
vector<int> graph[200000];
map<int, int> dp;
vector<pii> insd[200000];
vector<pii> deld[200000];

// ins. if same val exists, should already be optimal

int ins(int node) { // insert and delete suboptimal
  auto it = dp.lower_bound(vals[node]);
  if (it->F != vals[node]) {
    if (it == dp.begin()) {
      insd[node].PB(MP(vals[node], 1));
      dp[vals[node]] = 1;
    } else if (prev(it)->S < vals[node]) {
      insd[node].PB(MP(vals[node], prev(it)->S + 1));
      int asdf = prev(it)->S + 1;
      dp[vals[node]] = asdf;
    }
    if (it != dp.end()) {
      for (auto it2 = it; it2 != dp.end(); it2++) {
        if (it2->S > prev(it)->S)
          break;
        deld[node].PB(MP(it2->F, it2->S));
        dp.erase(it2++);
        if (it2 == dp.end())
          break;
      }
    }
  }
  return prev(dp.end())->S;
}

void del(int node) {
  for (pii dd : deld[node])
    dp[dd.F] = dd.S;
  for (pii ad : insd[node])
    dp.erase(dp.find(ad.F));
}

void dfs(int node, int par) {
  // cout << "\\n\\nNODE: " << node << "\\n\\n";
  ans[node] = ins(node);
  for (auto it = dp.begin(); it != dp.end(); it++) {
    // cout << it->F << ' ' << it->S << '\\n';
  }
  for (int ne : graph[node]) {
    if (ne == par)
      continue;
    dfs(ne, node);
  }
  del(node);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N;
  F0R(i, N) cin >> vals[i];
  F0R(i, N - 1) {
    cin >> a >> b;
    a--;
    b--;
    graph[a].PB(b);
    graph[b].PB(a);
  }
  dfs(0, -1);
  F0R(i, N) cout << ans[i] << '\\n';
}