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

#include <algorithm>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define F first
#define S second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> ii;

const int N = 2e5 + 5;

struct node {
  int val;
  node *left, *right;
  node(int val = 0, node *left = NULL, node *right = NULL) {
    this->val = val;
    this->left = left;
    this->right = right;
  }
};

int ar[N];
int cnt = 0;
map<int, int> codex;
vector<int> lst[N];

node *root[N];

node *upd(node *bef, int target, int val, int l, int r) {
  if (l == r)
    return new node(val, NULL, NULL);
  int m = (l + r) / 2;
  if (target <= m) {
    node *left = upd(bef->left, target, val, l, m);
    return new node(max(bef->val, val), left, bef->right);
  }
  node *right = upd(bef->right, target, val, m + 1, r);
  return new node(max(bef->val, val), bef->left, right);
}

int get(node *cur, int x, int l, int r) {
  if (l >= x)
    return 0;
  if (r < x)
    return cur->val;
  int m = (l + r) / 2;
  return max(get(cur->left, x, l, m), get(cur->right, x, m + 1, r));
}

int ans[N];

void dfs(int idx, int par) {
  int cur_lis = get(root[par], ar[idx], 0, cnt - 1) + 1;
  root[idx] = upd(root[par], ar[idx], cur_lis, 0, cnt - 1);
  ans[idx] = root[idx]->val;
  for (auto nx : lst[idx]) {
    if (nx == par)
      continue;
    dfs(nx, idx);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> ar[i];
    codex[ar[i]];
  }

  for (int i = 1; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    lst[a].pb(b);
    lst[b].pb(a);
  }

  for (auto &x : codex) {
    x.S = cnt++;
  }

  for (int i = 1; i <= n; ++i)
    ar[i] = codex[ar[i]];

  root[0] = new node(0, NULL, NULL);
  root[0]->left = root[0]->right = root[0];
  dfs(1, 0);

  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define F first
#define S second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> ii;

const int N = 1e5 + 5;

struct node {
  int val;
  node *left, *right;
  node(int val = 0, node *left = NULL, node *right = NULL) {
    this->val = val;
    this->left = left;
    this->right = right;
  }
};

int ar[N];
int cnt = 0;
map<int, int> codex;
vector<int> lst[N];

node *root[N];

node *upd(node *bef, int target, int val, int l, int r) {
  if (l == r)
    return new node(val, NULL, NULL);
  int m = (l + r) / 2;
  if (target <= m) {
    node *left = upd(bef->left, target, val, l, m);
    return new node(max(bef->val, val), left, bef->right);
  }
  node *right = upd(bef->right, target, val, m + 1, r);
  return new node(max(bef->val, val), bef->left, right);
}

int get(node *cur, int x, int l, int r) {
  if (l >= x)
    return 0;
  if (r < x)
    return cur->val;
  int m = (l + r) / 2;
  return max(get(cur->left, x, l, m), get(cur->right, x, m + 1, r));
}

int ans[N];

void dfs(int idx, int par) {
  int cur_lis = get(root[par], ar[idx], 0, cnt - 1) + 1;
  root[idx] = upd(root[par], ar[idx], cur_lis, 0, cnt - 1);
  ans[idx] = root[idx]->val;
  for (auto nx : lst[idx]) {
    if (nx == par)
      continue;
    dfs(nx, idx);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> ar[i];
    codex[ar[i]];
  }

  for (int i = 1; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    lst[a].pb(b);
    lst[b].pb(a);
  }

  for (auto &x : codex) {
    x.S = cnt++;
  }

  for (int i = 1; i <= n; ++i)
    ar[i] = codex[ar[i]];

  root[0] = new node(0, NULL, NULL);
  root[0]->left = root[0]->right = root[0];
  dfs(1, 0);

  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << endl;
  }
}