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

#include <bits/stdc++.h>

using namespace std;

const int MAX = 2e5 + 10;

struct segment_tree {
  vector<int> v;
  vector<long long> tree;
  multiset<long long> tree2[4 * MAX];
  int sz;
  bool flag;
  void init(vector<int> v2, bool t) {
    if (t == 0)
      flag = 0;
    else
      flag = 1;
    sz = (int)v2.size();
    v = v2;
    sort(v.begin(), v.end());
    tree.resize(sz * 4 + 10);
    for (int i = 0; i < sz * 4 + 10; ++i)
      tree[i] = 0;
  }
  int getidx1(long long idx) {
    int idx2 = lower_bound(v.begin(), v.end(), idx) - v.begin();
    return idx2;
  }
  int getidx2(long long idx) {
    int idx2 = lower_bound(v.begin(), v.end(), idx) - v.begin();
    idx2--;
    return idx2;
  }
  void update(int node, int l, int r, int idx, long long v) {
    if (idx > r || idx < l || idx < 0)
      return;
    if (l == r) {
      if (v < 0)
        tree2[node].erase(tree2[node].find(-1ll * v));
      else
        tree2[node].insert(v);
      if (tree2[node].size() > 0)
        tree[node] = *tree2[node].rbegin();
      else
        tree[node] = 0;
      return;
    }
    int mid = (l + r) >> 1;
    update(node << 1, l, mid, idx, v);
    update(node << 1 | 1, mid + 1, r, idx, v);
    if (flag == 0)
      tree[node] = max(tree[node << 1], tree[node << 1 | 1]);
    else
      tree[node] = (tree[node << 1] + tree[node << 1 | 1]);
  }

  long long query(int node, int l, int r, int from, int to) {
    if (from > r || to < l || from > to)
      return 0;
    if (l >= from && r <= to)
      return tree[node];
    int mid = (l + r) >> 1;
    long long a = query(node << 1, l, mid, from, to);
    long long b = query(node << 1 | 1, mid + 1, r, from, to);
    if (flag == 0)
      return max(a, b);
    else
      return (a + b);
  }

  void update(long long idx, long long val) {
    idx = getidx1(idx);
    update(1, 0, sz, idx, val);
  }

  long long query(long long idx) {
    idx = getidx2(idx);
    return query(1, 0, sz, 0, idx);
  }
};

int arr[MAX], ans[MAX];
int n;

vector<vector<int>> adj(MAX);

segment_tree tree;

void dfs(int node, int par, int Max) {
  int x = tree.query(arr[node]) + 1;
  ans[node] = max(Max, x);
  tree.update(arr[node], x);
  for (auto &child : adj[node]) {
    if (child == par)
      continue;
    dfs(child, node, ans[node]);
  }
  tree.update(arr[node], -1 * x);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> arr[i];
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  vector<int> v;
  for (int i = 1; i <= n; ++i)
    v.push_back(arr[i]);
  tree.init(v, 0);
  dfs(1, -1, 0);
  for (int i = 1; i <= n; ++i)
    cout << ans[i] << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int MAX = 2e5 + 10;

int arr[MAX], ans[MAX];
int n;

vector<vector<int>> adj(MAX);
vector<int> v;

void dfs(int node, int par) {
  bool flag = false;
  int prv = -1;
  auto it = lower_bound(v.begin(), v.end(), arr[node]);
  if (it != v.end()) {
    prv = *it;
    *it = arr[node];
  } else
    v.push_back(arr[node]), flag = true;
  ans[node] = v.size();
  for (auto &child : adj[node]) {
    if (child == par)
      continue;
    dfs(child, node);
  }
  if (flag)
    v.pop_back();
  else
    *it = prv;
  return;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> arr[i];
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  dfs(1, -1);
  for (int i = 1; i <= n; ++i)
    cout << ans[i] << "\\n";
  return 0;
}