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

// Optional FAST

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native")

// Required Libraries

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>

// Required namespaces

using namespace std;
using namespace __gnu_pbds;

// Required defines

#define endl '\\n'

#define READ(X) cin >> X;
#define READV(X)                                                               \\
  long long X;                                                                 \\
  cin >> X;
#define READAR(A, N)                                                           \\
  long long A[N];                                                              \\
  for (long long i = 0; i < N; i++) {                                          \\
    cin >> A[i];                                                               \\
  }
#define rz(A, N) A.resize(N);
#define sz(X) (long long)(X.size())
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define trav(x, A) for (auto &x : A)
#define FORI(a, b, c) for (long long a = b; a < c; a++)
#define FORD(a, b, c) for (long long a = b; a > c; a--)

// Required typedefs

typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<long long, null_type, greater<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set1;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> veci;
typedef vector<long long> vecl;

// Required Constants

const long long inf = (long long)1e18;
const long long MOD = (long long)(1e9 + 7);
const long long INIT = (long long)(1e6 + 1);

// Required Functions

ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2) % MOD;
  p = (p * p) % MOD;

  return (y % 2 == 0) ? p : (x * p) % MOD;
}
ll modInverse(ll a) { return power(a, MOD - 2); }

// Work
vector<vector<ll>> ADJ;
vector<ll> val;
vector<ll> ans;
vector<ll> d;

void dfs(ll u, ll p = -1) {
  ll prev = *lower_bound(d.begin(), d.end(), val[u]);
  *lower_bound(d.begin(), d.end(), val[u]) = val[u];
  ans[u] = distance(d.begin(), lower_bound(d.begin(), d.end(), inf));
  for (auto v : ADJ[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
  *lower_bound(d.begin(), d.end(), val[u]) = prev;
}

int main() {
#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    // freopen ("OUTPUT.txt" , "w" , stdout);
  }
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  clock_t clk;
  clk = clock();
  //-----------------------------------------------------------------------------------------------------------//

  READV(N);
  val.resize(N + 1);
  ADJ.resize(N + 1);
  ans.resize(N + 1);
  d.resize(N + 1, inf);
  FORI(i, 1, N + 1) { READ(val[i]); }
  FORI(i, 0, N - 1) {
    READV(u);
    READV(v);
    ADJ[u].pb(v);
    ADJ[v].pb(u);
  }
  dfs(1);
  FORI(i, 0, N) { cout << ans[i + 1] << endl; }

  //-----------------------------------------------------------------------------------------------------------//

  clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC
       << endl;

  return 0;
}

Error: 0
Time: 0.000072


Fixed submission:

// Optional FAST

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native")

// Required Libraries

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>

// Required namespaces

using namespace std;
using namespace __gnu_pbds;

// Required defines

#define endl '\\n'

#define READ(X) cin >> X;
#define READV(X)                                                               \\
  long long X;                                                                 \\
  cin >> X;
#define READAR(A, N)                                                           \\
  long long A[N];                                                              \\
  for (long long i = 0; i < N; i++) {                                          \\
    cin >> A[i];                                                               \\
  }
#define rz(A, N) A.resize(N);
#define sz(X) (long long)(X.size())
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define trav(x, A) for (auto &x : A)
#define FORI(a, b, c) for (long long a = b; a < c; a++)
#define FORD(a, b, c) for (long long a = b; a > c; a--)

// Required typedefs

typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<long long, null_type, greater<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set1;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> veci;
typedef vector<long long> vecl;

// Required Constants

const long long inf = (long long)1e18;
const long long MOD = (long long)(1e9 + 7);
const long long INIT = (long long)(1e6 + 1);

// Required Functions

ll power(ll x, ll y) {
  if (y == 0)
    return 1;
  ll p = power(x, y / 2) % MOD;
  p = (p * p) % MOD;

  return (y % 2 == 0) ? p : (x * p) % MOD;
}
ll modInverse(ll a) { return power(a, MOD - 2); }

// Work
vector<vector<ll>> ADJ;
vector<ll> val;
vector<ll> ans;
set<pll> Z;

void dfs(ll u, ll p = -1) {
  auto it = Z.lower_bound({val[u], -1});
  it--;
  ans[u] = (*it).se + 1;
  Z.insert({val[u], ans[u]});
  for (auto v : ADJ[u]) {
    if (v != p) {
      dfs(v, u);
    }
  }
  Z.erase(Z.find({val[u], ans[u]}));
}

int main() {
#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    // freopen ("OUTPUT.txt" , "w" , stdout);
  }
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  clock_t clk;
  clk = clock();
  //-----------------------------------------------------------------------------------------------------------//

  READV(N);
  val.resize(N + 1);
  ADJ.resize(N + 1);
  ans.resize(N + 1);
  FORI(i, 1, N + 1) { READ(val[i]); }
  FORI(i, 0, N - 1) {
    READV(u);
    READV(v);
    ADJ[u].pb(v);
    ADJ[v].pb(u);
  }
  Z.insert({-1, 0});
  dfs(1);
  FORI(i, 0, N) { cout << ans[i + 1] << endl; }

  //-----------------------------------------------------------------------------------------------------------//

  clk = clock() - clk;
  cerr << fixed << setprecision(6) << "Time: " << ((double)clk) / CLOCKS_PER_SEC
       << endl;

  return 0;
}