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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ll long long
using namespace std;
using namespace __gnu_pbds;

typedef tree<pair<long long int, long long int>, null_type,
             less<pair<long long int, long long int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

bool v[210002];
int ans[210002];
vector<int> adj[210002];
vector<int> a;
vector<int> curr;
stack<pair<int, int>> s;

void dfs(int node) {
  v[node] = 1;
  int idx = lower_bound(curr.begin(), curr.end(), a[node - 1]) - curr.begin();
  int init_val = curr[idx];
  curr[idx] = a[node - 1];
  ans[node] = lower_bound(curr.begin(), curr.end(), INT_MAX) - curr.begin() - 1;
  for (int i = 0; i < adj[node].size(); i++) {
    if (!v[adj[node][i]])
      dfs(adj[node][i]);
  }
  curr[idx] = init_val;
}
void go() {
  memset(v, 0, sizeof(v));
  memset(ans, 1, sizeof(ans));
  int n;
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    int p, q;
    cin >> p >> q;
    adj[p].push_back(q);
    adj[q].push_back(p);
  }
  curr.resize(n + 1, INT_MAX);
  curr[0] = INT_MIN;
  dfs(1);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
}
int main() {
  FIO;
  //    int t;
  //    cin>>t;
  //    while(t--)
  go();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ll long long
using namespace std;
using namespace __gnu_pbds;

typedef tree<pair<long long int, long long int>, null_type,
             less<pair<long long int, long long int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

int v[200001];
int ans[200001];
vector<int> adj[200001];
vector<int> a;

void dfs(int node, vector<int> curr) {
  v[node] = 1;
  int idx = lower_bound(curr.begin(), curr.end(), a[node - 1]) - curr.begin();
  int init_val = curr[idx];
  curr[idx] = a[node - 1];
  ans[node] = idx;
  for (int i = 0; i < adj[node].size(); i++) {
    if (!v[adj[node][i]])
      dfs(adj[node][i], curr);
  }
  curr[idx] = init_val;
}
void go() {
  memset(v, 0, sizeof(v));
  int n;
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    int p, q;
    cin >> p >> q;
    adj[p].push_back(q);
    adj[q].push_back(p);
  }
  vector<int> temp(n + 1, INT_MAX);
  temp[0] = INT_MIN;
  dfs(1, temp);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
}
int main() {
  FIO;
  //    int t;
  //    cin>>t;
  //    while(t--)
  go();
  return 0;
}