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

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits.h>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
const int NAX = 2e5 + 11;
vector<int> G[NAX];
int ans[NAX], A[NAX];
vector<stack<int>> lis(NAX);

int upperBound(int x, int N) {
  int L = -1, R = N, M;
  while (L < R) {
    M = L + (R - L + 1) / 2;
    if (lis[M].top() <= x) {
      L = M;
    } else {
      R = M - 1;
    }
  }
  return L + 1;
}

int lowerBound(int x, int N) {
  int L = -1, R = N, M;
  while (L < R) {
    M = L + (R - L + 1) / 2;
    if (lis[M].top() >= x) {
      R = M - 1;
    } else {
      L = M;
    }
  }
  return L + 1;
}

void dfs(int u, int N, int pi = -1) {
  int place = lowerBound(A[u], N);
  lis[place].push(A[u]);
  ans[u] = lowerBound(INT_MAX, N) - 1;
  for (int &v : G[u]) {
    if (v == pi) {
      continue;
    }
    dfs(v, N, u);
  }
  lis[place].pop();
}

int main() {
  ios::sync_with_stdio(0);
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    lis[i + 1].push(INT_MAX);
  }
  lis[N + 1].push(INT_MAX);
  lis[N + 2].push(INT_MAX);
  lis[N + 3].push(INT_MAX);
  lis[0].push(INT_MIN);
  for (int i = 0; i + 1 < N; ++i) {
    int x, y;
    cin >> x >> y;
    G[x - 1].push_back(y - 1);
    G[y - 1].push_back(x - 1);
  }
  dfs(0, N);
  for (int i = 0; i < N; ++i) {
    cout << ans[i] << "\\n";
  }
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits.h>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
const int NAX = 2e5 + 3;
vector<int> G[NAX];
int ans[NAX], A[NAX];
vector<stack<int>> lis(NAX);

int upperBound(int x, int N) {
  int L = -1, R = N, M;
  while (L < R) {
    M = L + (R - L + 1) / 2;
    if (lis[M].top() <= x) {
      L = M;
    } else {
      R = M - 1;
    }
  }
  return L + 1;
}

int lowerBound(int x, int N) {
  int L = -1, R = N, M;
  while (L < R) {
    M = L + (R - L + 1) / 2;
    if (lis[M].top() >= x) {
      R = M - 1;
    } else {
      L = M;
    }
  }
  return L + 1;
}

void dfs(int u, int N, int pi = -1) {
  int place = lowerBound(A[u], N);
  lis[place].push(A[u]);
  ans[u] = lowerBound(INT_MAX, N) - 1;
  for (int &v : G[u]) {
    if (v == pi) {
      continue;
    }
    dfs(v, N, u);
  }
  lis[place].pop();
}

int main() {
  ios::sync_with_stdio(0);
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    lis[i + 1].push(INT_MAX);
  }
  lis[N + 1].push(INT_MAX);
  lis[N + 2].push(INT_MAX);
  lis[N + 3].push(INT_MAX);
  lis[0].push(INT_MIN);
  for (int i = 0; i + 1 < N; ++i) {
    int x, y;
    cin >> x >> y;
    G[x - 1].push_back(y - 1);
    G[y - 1].push_back(x - 1);
  }
  dfs(0, N);
  for (int i = 0; i < N; ++i) {
    cout << ans[i] << "\\n";
  }
  return 0;
}