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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// #include<boost/multiprecision/cpp_int.hpp>
// #include<boost/multiprecision/cpp_dec_float.hpp>
// namespace mp=boost::multiprecision;
// #define mulint mp::cpp_int
// #define mulfloat mp::cpp_dec_float_100
using namespace std;
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
constexpr int MOD = 1000000007;
// constexpr int MOD=998244353;
#define INF (1 << 30)
#define LINF (lint)(1LL << 56)
#define endl "\\n"
#define rep(i, n) for (lint(i) = 0; (i) < (n); (i)++)
#define reprev(i, n) for (lint(i) = (n - 1); (i) >= 0; (i)--)
#define Flag(x) (1 << (x))
#define Flagcount(x) __builtin_popcountll(x)
#define pint pair<int, int>
#define pdouble pair<double, double>
#define plint pair<lint, lint>
#define fi first
#define se second
typedef long long lint;
int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int MAX_N = 2e5 + 5;
// struct edge{lint to,num;};

int A[200000];
vector<int> edge[200000];
int ans[200000];
bool reach[200000];
int dp[200000];
stack<pint> save;

void dfs(int now) {
  if (reach[now])
    return;
  reach[now] = true;
  int put = lower_bound(dp, dp + 200000, A[now]) - dp;
  save.push(pint(put, dp[put]));
  dp[put] = A[now];
  ans[now] = lower_bound(dp, dp + 200000, INF) - dp;
  rep(i, edge[now].size()) dfs(edge[now][i]);
  pint rev = save.top();
  save.pop();
  dp[rev.fi] = rev.se;
}

int main(void) {
  int N;
  cin >> N;
  rep(i, N) cin >> A[i];
  rep(i, 200000) dp[i] = INF;
  rep(i, N - 1) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  dfs(0);
  rep(i, N) cout << ans[i] << endl;
}

Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
constexpr int MOD = 1000000007;
#define INF (1 << 30)
#define LINF (lint)(1LL << 56)
#define endl "\\n"
#define rep(i, n) for (lint(i) = 0; (i) < (n); (i)++)
#define reprev(i, n) for (lint(i) = (n - 1); (i) >= 0; (i)--)
#define Flag(x) (1 << (x))
#define Flagcount(x) __builtin_popcount(x)
#define pint pair<int, int>
#define pdouble pair<double, double>
#define plint pair<lint, lint>
#define fi first
#define se second
typedef long long lint;
int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int MAX_N = 2e5 + 5;

int N;
int A[MAX_N];
int ans[MAX_N];
int DP[MAX_N];

vector<int> edge[MAX_N];
bool reach[MAX_N];
vector<int> dp;

void dfs(int now, int depth) {
  if (reach[now])
    return;
  reach[now] = true;
  *lower_bound(dp.begin(), dp.begin() + depth, A[now]) = A[now];
  ans[now] = lower_bound(dp.begin(), dp.begin() + depth, INF) - dp.begin();
  dp[depth] = INF;
  rep(i, edge[now].size()) dfs(edge[now][i], depth + 1);
}

int main(void) {
  cin >> N;
  rep(i, N) cin >> A[i];
  rep(i, N) DP[i] = INF;
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  dp.push_back(INF);
  dfs(0, 1);
  rep(i, N) cout << ans[i] << endl;
}