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

#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <vector>

int const MAXN = 2e5;
int const MAXNN = 1 << 18;
int a[MAXN], ans[MAXN], N;
std::vector<int> G[MAXN];

template <typename T, int NN> struct bit {
  typedef std::function<T(T, T)> OP;
  T data_[NN]; // 0-base
  OP &op_;
  T e_;
  std::stack<std::vector<std::pair<int, T>>> ul_;
  bit(OP &op, T e) : op_(op), e_(e) { std::fill_n(data_, NN, e_); }
  int lb(int i) { return i & -i; }
  T query(int i) { // 1-base
    T res = e_;
    while (i > 0) {
      res = op_(res, data_[i - 1]);
      i -= lb(i);
    }
    return res;
  }
  void add(int i, T const &v) { // 1-base
    std::vector<std::pair<int, T>> save;
    while (i <= NN) {
      save.emplace_back(i - 1, data_[i - 1]);
      data_[i - 1] = op_(data_[i - 1], v);
      i += lb(i);
    }
    ul_.push(save);
  }
  void undo() {
    std::vector<std::pair<int, T>> &save = ul_.top();
    for (auto p : save)
      data_[p.first] = p.second;
    ul_.pop();
  }
};
bit<int, MAXNN>::OP op = [](int x, int y) { return std::max(x, y); };
bit<int, MAXNN> max_bit(op, 0);

void dfs(int v, int p) {
  max_bit.add(a[v], max_bit.query(a[v] - 1) + 1);
  ans[v] = max_bit.query(MAXN);
  for (auto c : G[v])
    if (c != p)
      dfs(c, v);
  max_bit.undo();
}

int main() {
  std::cin >> N;
  for (int i = 0; i < N; ++i)
    std::cin >> a[i];

  // compress a[]
  std::set<int> s(a, a + N);
  std::map<int, int> m;
  int fv = 0;
  for (int v : s)
    m[v] = ++fv;
  for (int i = 0; i < N; ++i)
    a[i] = m[a[i]];

  for (int i = 0; i < N - 1; ++i) {
    int u, v;
    std::cin >> u >> v;
    --u;
    --v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(0, -1);
  for (int i = 0; i < N; ++i)
    std::cout << ans[i] << "\\n";
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <vector>

int const MAXN = 2e5;
int const MAXNN = 1 << 18;
int a[MAXN], ans[MAXN], N;
std::vector<int> G[MAXN];

template <typename T, int NN> struct bit {
  typedef std::function<T(T, T)> OP;
  T data_[NN], e_; // 0-base
  OP &op_;
  std::stack<std::vector<std::pair<int, T>>> ul_;
  bit(OP &op, T e) : op_(op), e_(e) { std::fill_n(data_, NN, e_); }
  int lb(int i) { return i & -i; }
  T query(int i) { // 1-base
    T res = e_;
    while (i > 0) {
      res = op_(res, data_[i - 1]);
      i -= lb(i);
    }
    return res;
  }
  void add(int i, T const &v) { // 1-base
    std::vector<std::pair<int, T>> save;
    while (i <= NN) {
      save.emplace_back(i - 1, data_[i - 1]);
      data_[i - 1] = op_(data_[i - 1], v);
      i += lb(i);
    }
    ul_.push(save);
  }
  void undo() {
    std::vector<std::pair<int, T>> &save = ul_.top();
    for (auto p : save)
      data_[p.first] = p.second;
    ul_.pop();
  }
};
bit<int, MAXNN>::OP op = [](int x, int y) { return std::max(x, y); };
bit<int, MAXNN> max_bit(op, 0);

void dfs(int v, int p) {
  max_bit.add(a[v], max_bit.query(a[v] - 1) + 1);
  ans[v] = max_bit.query(MAXN);
  for (auto c : G[v])
    if (v != p)
      dfs(c, v);
  max_bit.undo();
}

int main() {
  std::cin >> N;
  for (int i = 0; i < N; ++i)
    std::cin >> a[i];

  // compress a[]
  std::set<int> s(a, a + N);
  std::map<int, int> m;
  int fv = 0;
  for (int v : s)
    m[v] = ++fv;
  for (int i = 0; i < N; ++i)
    a[i] = m[a[i]];

  for (int i = 0; i < N - 1; ++i) {
    int u, v;
    std::cin >> u >> v;
    --u;
    --v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(0, -1);
  for (int i = 0; i < N; ++i)
    std::cout << ans[i] << "\\n";
  return 0;
}