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

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
const ll INF = 9e18;
const int inf = 2e9;

void print() { cout << endl; }
template <typename Head, typename... Tail> void print(Head head, Tail... tail) {
  int size = sizeof...(Tail);
  cout << head;
  if (size > 0) {
    cout << " ";
  }
  print(tail...);
}

void print0() {}
template <typename Head, typename... Tail>
void print0(Head head, Tail... tail) {
  cout << head;
  print0(tail...);
}

using edge = pair<int, int>;
void dfs(int start, vector<vector<edge>> &edges, vector<int> &a,
         vector<bool> &done, vector<stack<int>> &dpstack, vector<int> &result) {
  int pre_depth = -1;
  stack<pair<int, int>> tasks; // depth,node
  stack<int> inserts;          // toinsert
  tasks.push({0, start});
  inserts.push(0);
  while (!tasks.empty()) {
    pair<int, int> task = tasks.top();
    tasks.pop();
    int depth = task.first;
    if (depth <= pre_depth) {
      for (int j = 0; j <= (pre_depth - depth); j++) {
        int removes = inserts.top();
        // print(dpstack[removes].size(), inserts.size(), removes);
        inserts.pop();
        dpstack[removes].pop();
      }
    }

    int u = task.second;
    done[u] = true;

    int left = 0;
    int right = dpstack.size() - 1;
    while (left <= right) {
      int mid = (left + right) / 2;
      int mval = dpstack[mid].size() > 0 ? dpstack[mid].top() : inf;
      if (mval >= a[u]) {
        right = mid - 1;
      } else {
        left = mid + 1;
      }
    }
    int toinsert = -1;
    for (int i = left - 2; i <= left + 2; i++) {
      if (i >= 0 && i < (int)dpstack.size() &&
          (!dpstack[i].size() || dpstack[i].top() >= a[u])) {
        toinsert = i;
        break;
      }
    }
    if (toinsert >= 0) {
      inserts.push(toinsert);
      dpstack[toinsert].push(a[u]);
    }

    left = 0;
    right = dpstack.size() - 1;
    while (left <= right) {
      int mid = (left + right) / 2;
      if (!dpstack[mid].size()) {
        right = mid - 1;
      } else {
        left = mid + 1;
      }
    }
    result[u] = dpstack.size();
    for (int i = left - 2; i <= left + 2; i++) {
      if (i >= 0 && i < (int)dpstack.size() && !dpstack[i].size()) {
        //                print(u, i);
        result[u] = i;
        break;
      }
    }

    for (auto e : edges[u]) {
      if (!done[e.second]) {
        tasks.push({depth + 1, e.second});
      }
    }
    pre_depth = depth;
  }
}

int main() {
  int N;
  cin >> N;
  vector<int> a(N, -1);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  vector<vector<edge>> edges(N);
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    edges[u].push_back(make_pair(u, v));
    edges[v].push_back(make_pair(v, u));
  }
  vector<bool> done(N, false);
  vector<int> result(N, -1);

  vector<stack<int>> dpstack(N);
  dfs(0, edges, a, done, dpstack, result);
  for (int i = 0; i < N; i++) {
    print(result[i]);
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
const ll INF = 9e18;
const int inf = 2e9;

void print() { cout << endl; }
template <typename Head, typename... Tail> void print(Head head, Tail... tail) {
  int size = sizeof...(Tail);
  cout << head;
  if (size > 0) {
    cout << " ";
  }
  print(tail...);
}

void print0() {}
template <typename Head, typename... Tail>
void print0(Head head, Tail... tail) {
  cout << head;
  print0(tail...);
}

using edge = pair<int, int>;
void dfs(int start, vector<vector<edge>> &edges, vector<int> &a,
         vector<bool> &done, vector<stack<int>> &dpstack, vector<int> &result) {
  int pre_depth = -1;
  stack<pair<int, int>> tasks; // depth,node
  stack<int> inserts;          // toinsert
  tasks.push({0, start});
  inserts.push(0);
  while (!tasks.empty()) {
    pair<int, int> task = tasks.top();
    tasks.pop();
    int depth = task.first;
    if (depth <= pre_depth) {
      for (int j = 0; j <= (pre_depth - depth); j++) {
        int removes = inserts.top();
        // print(dpstack[removes].size(), inserts.size(), removes);
        inserts.pop();
        dpstack[removes].pop();
      }
    }

    int u = task.second;
    done[u] = true;

    int left = 0;
    int right = dpstack.size() - 1;
    while (left <= right) {
      int mid = (left + right) / 2;
      int mval = dpstack[mid].size() > 0 ? dpstack[mid].top() : inf;
      if (mval > a[u]) {
        right = mid - 1;
      } else {
        left = mid + 1;
      }
    }
    int toinsert = -1;
    for (int i = left - 2; i <= left + 2; i++) {
      if (i >= 0 && i < (int)dpstack.size() &&
          (!dpstack[i].size() || dpstack[i].top() > a[u])) {
        toinsert = i;
        break;
      }
    }
    if (toinsert >= 0) {
      inserts.push(toinsert);
      dpstack[toinsert].push(a[u]);
    }

    left = 0;
    right = dpstack.size() - 1;
    while (left <= right) {
      int mid = (left + right) / 2;
      if (!dpstack[mid].size()) {
        right = mid - 1;
      } else {
        left = mid + 1;
      }
    }
    result[u] = dpstack.size();
    for (int i = left - 2; i <= left + 2; i++) {
      if (i >= 0 && i < (int)dpstack.size() && !dpstack[i].size()) {
        //                print(u, i);
        result[u] = i;
        break;
      }
    }

    for (auto e : edges[u]) {
      if (!done[e.second]) {
        tasks.push({depth + 1, e.second});
      }
    }
    pre_depth = depth;
  }
}

int main() {
  int N;
  cin >> N;
  vector<int> a(N, -1);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  vector<vector<edge>> edges(N + 1);
  for (int i = 0; i < N; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    edges[u].push_back(make_pair(u, v));
    edges[v].push_back(make_pair(v, u));
  }
  vector<bool> done(N, false);
  vector<int> result(N, -1);

  vector<stack<int>> dpstack(N);
  dfs(0, edges, a, done, dpstack, result);
  for (int i = 0; i < N; i++) {
    print(result[i]);
  }
}