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

#include <bits/stdc++.h>

using namespace std;

const int N = 3e5 + 7;

int n;
vector<int> adj[N];
int a[N];
int tree[N * 4];
int ans[N];
int cnt;
vector<int> treev[N * 4], mxs[4 * N];

int gnode(int x) {
  int ret = 0;
  for (auto u : treev[x]) {
    ret = max(ret, u);
  }
  return ret;
}

void update(int node, int L, int R, int ix, int val) {

  if (L == R) {
    if (mxs[node].size() == 0) {
      mxs[node].push_back(0);
    }
    if (val > 0) {
      mxs[node].push_back(max(val, mxs[node].back()));
      treev[node].push_back(val);
    } else {
      treev[node].pop_back();
      mxs[node].pop_back();
    }
    tree[node] = gnode(node);
    // tree[node] = mxs[node].back() ;
    return;
  }

  int mid = (L + R) >> 1;
  if (ix <= mid) {
    update(node * 2 + 1, L, mid, ix, val);
  } else {
    update(node * 2 + 2, mid + 1, R, ix, val);
  }
  tree[node] = max(tree[node * 2 + 1], tree[node * 2 + 2]);
}

int get(int node, int L, int R, int l, int r) {
  if (l > R || r < L)
    return 0;
  if (L >= l && R <= r)
    return tree[node];
  int mid = (L + R) >> 1;
  int s1 = get(node * 2 + 1, L, mid, l, r);
  int s2 = get(node * 2 + 2, mid + 1, R, l, r);
  return max(s1, s2);
}

vector<int> liss = {0};
vector<int> mxl = {0};

void dfs(int x, int p) {

  int mxp = get(0, 1, N, 1, a[x] - 1);
  liss.push_back({mxp + 1});
  mxl.push_back(max(mxl.back(), liss.back()));
  ans[x] = mxl.back();
  update(0, 1, N, a[x], mxp + 1);

  for (auto u : adj[x]) {
    if (u == p)
      continue;
    dfs(u, x);
  }
  update(0, 1, N, a[x], -1);
  mxl.pop_back();
  liss.pop_back();
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("in.in" ,"r" , stdin) ;

  cin >> n;
  vector<pair<int, int>> dbl;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    dbl.push_back({x, i});
  }
  sort(dbl.begin(), dbl.end());
  int T = 1, l = 0;
  for (int i = 0; i < n; i++) {
    T += (dbl[i].first != l);
    a[dbl[i].second] = T;
    l = dbl[i].first;
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  dfs(1, 1);
  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 N = 2e5 + 7;

int n;
vector<int> adj[N];
int a[N];
int tree[N * 4];
int roots[N];
int ans[N];
int cnt;
vector<int> treev[N * 4], mxs[4 * N];

int calc(int x) {
  int ret = 0;
  for (auto u : treev[x]) {
    ret = max(ret, u);
  }
  return ret;
}

int update(int node, int L, int R, int ix, int val) {

  if (L == R) {
    if (val > 0) {
      if (mxs[node].size()) {
        mxs[node].push_back(max(val, mxs[node].back()));
      } else
        mxs[node].push_back(val);
      treev[node].push_back(val);
    } else {
      treev[node].pop_back();
      mxs[node].pop_back();
    }
    tree[node] = mxs[node].back();
    return 0;
  }

  int mid = (L + R) >> 1;
  if (ix <= mid) {
    update(node * 2 + 1, L, mid, ix, val);
  } else {
    update(node * 2 + 2, mid + 1, R, ix, val);
  }
  tree[node] = max(tree[node * 2 + 1], tree[node * 2 + 2]);
}

int get(int node, int L, int R, int l, int r) {
  if (l > R || r < L)
    return 0;
  if (L >= l && R <= r)
    return tree[node];
  int mid = (L + R) >> 1;
  int s1 = get(node * 2 + 1, L, mid, l, r);
  int s2 = get(node * 2 + 2, mid + 1, R, l, r);
  return max(s1, s2);
}

void dfs(int x, int p) {

  int mxp = get(0, 1, N, 1, a[x] - 1);

  ans[x] = mxp + 1;
  update(0, 1, N, a[x], mxp + 1);

  for (auto u : adj[x]) {
    if (u == p)
      continue;
    dfs(u, x);
  }
  update(0, 1, N, a[x], -1);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("in.in" ,"r" , stdin) ;

  cin >> n;
  vector<pair<int, int>> dbl;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    dbl.push_back({x, i});
  }
  sort(dbl.begin(), dbl.end());
  for (int i = 0; i < n; i++) {
    a[dbl[i].second] = i + 1;
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  dfs(1, 1);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << "\\n";

  return 0;
}