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

// raja1999

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// setbase - cout << setbase (16)a; cout << 100 << endl; Prints 64
// setfill -   cout << setfill ('x') << setw (5); cout << 77 <<endl;prints xxx77
// setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
// cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
using namespace __gnu_pbds;
#define f(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) f(i, 0, n)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define ss second
#define ff first
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(a) a.size()
#define inf (1000 * 1000 * 1000 + 5)
#define all(a) a.begin(), a.end()
#define tri pair<int, pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000 * 1000 * 1000 + 7)
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vi, greater<int>>
// #define int ll

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

// std::ios::sync_with_stdio(false);
int c = 0;
vector<vi> adj(200005);
int st[2000005];
int a[200005], ans[200005];
int update(int s, int e, int node, int pos, int val) {
  if (s > pos || e < pos) {
    return 0;
  }
  if (s == e) {
    st[node] = val;
    return 0;
  }
  int mid = (s + e) / 2;
  update(s, mid, 2 * node, pos, val);
  update(mid + 1, e, 2 * node + 1, pos, val);
  st[node] = max(st[2 * node], st[2 * node + 1]);
  return 0;
}
int query(int s, int e, int node, int l, int r) {
  if (s > r || e < l) {
    return 0;
  }
  if (l <= s && r >= e) {
    return st[node];
  }
  int mid = (s + e) / 2;
  return max(query(s, mid, 2 * node, l, r),
             query(mid + 1, e, 2 * node + 1, l, r));
}
int dfs(int u, int p) {
  int q = query(0, c - 1, 1, 0, a[u] - 1);
  ans[u] = 1;
  if (p != -1) {
    ans[u] = max(ans[u], ans[p]);
  }
  int i, prev = query(0, c - 1, 1, a[u], a[u]);
  ans[u] = max(ans[u], q + 1);
  update(0, c - 1, 1, a[u], q + 1);
  rep(i, adj[u].size()) {
    if (adj[u][i] != p)
      dfs(adj[u][i], u);
  }
  update(0, c - 1, 1, a[u], prev);
  return 0;
}
map<int, int> mapi;
map<int, int>::iterator it;
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, i, u, v;
  cin >> n;
  rep(i, n) {
    cin >> a[i];
    mapi[a[i]] = 1;
  }
  c = 0;
  rep(i, n - 1) {
    cin >> u >> v;
    u--;
    v--;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  for (it = mapi.begin(); it != mapi.end(); it++) {
    it->ss = c++;
  }
  rep(i, n) { a[i] = mapi[a[i]]; }
  dfs(0, -1);
  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}


Error: 0

Fixed submission:

// raja1999

// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// setbase - cout << setbase (16)a; cout << 100 << endl; Prints 64
// setfill -   cout << setfill ('x') << setw (5); cout << 77 <<endl;prints xxx77
// setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
// cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
using namespace __gnu_pbds;
#define f(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) f(i, 0, n)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define ss second
#define ff first
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(a) a.size()
#define inf (1000 * 1000 * 1000 + 5)
#define all(a) a.begin(), a.end()
#define tri pair<int, pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000 * 1000 * 1000 + 7)
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vi, greater<int>>
// #define int ll

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

// std::ios::sync_with_stdio(false);
int c = 0;
vector<vi> adj(200005);
int st[2000005];
int a[200005], ans[200005];
int update(int s, int e, int node, int pos, int val) {
  if (s > pos || e < pos) {
    return 0;
  }
  if (s == e) {
    st[node] = val;
    return 0;
  }
  int mid = (s + e) / 2;
  update(s, mid, 2 * node, pos, val);
  update(mid + 1, e, 2 * node + 1, pos, val);
  st[node] = max(st[2 * node], st[2 * node + 1]);
  return 0;
}
int query(int s, int e, int node, int l, int r) {
  if (s > r || e < l) {
    return 0;
  }
  if (l <= s && r >= e) {
    return st[node];
  }
  int mid = (s + e) / 2;
  return max(query(s, mid, 2 * node, l, r),
             query(mid + 1, e, 2 * node + 1, l, r));
}
int dfs(int u, int p) {
  int q = query(0, c - 1, 1, 0, a[u] - 1);
  ans[u] = 1;
  if (p != -1) {
    ans[u] = max(ans[u], ans[p]);
  }
  int i, prev = query(0, c - 1, 1, a[u], a[u]);
  ans[u] = max(ans[u], q + 1);
  update(0, c - 1, 1, a[u], ans[u]);
  rep(i, adj[u].size()) {
    if (adj[u][i] != p)
      dfs(adj[u][i], u);
  }
  update(0, c - 1, 1, a[u], prev);
}
map<int, int> mapi;
map<int, int>::iterator it;
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, i, u, v;
  cin >> n;
  rep(i, n) {
    cin >> a[i];
    mapi[a[i]] = 1;
  }
  c = 0;
  rep(i, n - 1) {
    cin >> u >> v;
    u--;
    v--;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  for (it = mapi.begin(); it != mapi.end(); it++) {
    it->ss = c++;
  }
  rep(i, n) { a[i] = mapi[a[i]]; }
  dfs(0, -1);
  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}