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

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FRE freopen("in.txt", "r", stdin);
#define endl "\\n"
#define PI acos(-1.0)
typedef long long LL;
typedef pair<int, int> PII;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 2e5 + 10, M = 17;

int a[N];
int dp[N];
vector<int> e[N];
vector<int> vec;

void dfs(int u, int fa) {
  for (int i = 0; i < e[u].size(); i++) {
    int v = e[u][i];
    if (v == fa)
      continue;
    int l = 0, r = vec.size() - 1, x = -1;
    while (l <= r) {
      int mid = l + r >> 1;
      if (vec[mid] >= a[v]) {
        x = mid;
        r = mid - 1;
      } else
        l = mid + 1;
    }
    int tmp;
    if (x == -1) {
      vec.push_back(a[v]);
      int num = vec.size();
      dp[v] = max(num, dp[u]);
    } else {
      tmp = vec[x];
      vec[x] = a[v];
      dp[v] = max(x + 1, dp[u]);
    }
    dfs(v, u);
    if (x == -1)
      vec.pop_back();
    else
      vec[x] = tmp;
  }
}

void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    e[u].push_back(v);
    e[v].push_back(u);
  }
  dp[1] = 1;
  vec.push_back(a[1]);
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", dp[i]);
}

int main() {
  //	FRE;
  //	int t; scanf("%d", &t); while(t--)
  solve();
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FRE freopen("in.txt", "r", stdin);
#define endl "\\n"
#define PI acos(-1.0)
typedef long long LL;
typedef pair<int, int> PII;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 1e5 + 10, M = 17;

int a[N];
int dp[N];
vector<int> e[N];
vector<int> vec;

void dfs(int u, int fa) {
  for (int i = 0; i < e[u].size(); i++) {
    int v = e[u][i];
    if (v == fa)
      continue;
    int l = 0, r = vec.size() - 1, x = -1;
    while (l <= r) {
      int mid = l + r >> 1;
      if (vec[mid] >= a[v]) {
        x = mid;
        r = mid - 1;
      } else
        l = mid + 1;
      int tmp;
      if (x == -1) {
        vec.push_back(a[v]);
        dp[v] = vec.size();
      } else {
        tmp = vec[x];
        vec[x] = a[v];
        dp[v] = x + 1;
      }
      dfs(v, u);
      if (x == -1)
        vec.pop_back();
      else
        vec[x] = tmp;
    }
  }
}

void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    e[u].push_back(v);
    e[v].push_back(u);
  }
  dp[1] = 1;
  vec.push_back(a[1]);
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%d\\n", dp[i]);
}

int main() {
  //	FRE;
  //	int t; scanf("%d", &t); while(t--)
  solve();
  return 0;
}