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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define st(arr, a) memset(arr, a, sizeof arr)
#define nl cout << '\\n'
#define bitcount(x) __builtin_popcountll(x)
const int MOD = 1000000007ll;
#define forn(i, a, b) for (int i = a; i <= b; i++)
#define rfor(i, a, b) for (int i = a; i >= b; i--)
#define all(x) x.begin(), x.end()
#define pi pair<int, int>
#define X first
#define Y second
#define sz(A) (int)A.size()
#define N 414159
#define vi vector<int>
#define v vector
#define die exit(0)
#define alln(arr) arr + 1, arr + 1 + n
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;

// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).

#define in input_int()
#define ins input_string()
int input_int() {
  int a;
  cin >> a;
  return a;
}
string input_string() {
  string s;
  cin >> s;
  return s;
}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int t[N * 8];

int ans[N];

vi adj[N];
map<int, int> m;

int sum(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    return t[v];
  }
  int tm = (tl + tr) / 2;
  return max(sum(v * 2, tl, tm, l, min(r, tm)),
             sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}

void update(int v, int tl, int tr, int pos, int new_val) {
  if (tl == tr) {
    t[v] = new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2, tl, tm, pos, new_val);
    else
      update(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}

int n, k, a, b, c, d, x, y, arr[N];
string s;

void dfs(int u, int p) {
  int x = sum(1, 1, N, 1, arr[u] - 1);
  int y = sum(1, 1, N, arr[u], arr[u]);
  ans[u] = max(x + 1, t[1]);
  update(1, 1, N, arr[u], x + 1);
  for (auto i : adj[u]) {
    if (i == p)
      continue;
    dfs(i, u);
  }
  update(1, 1, N, arr[u], y);
}

void solve() {
  cin >> n;
  forn(i, 1, n) cin >> arr[i];

  vi A;
  forn(i, 1, n) A.pb(arr[i]);
  sort(all(A));
  int k = 2;
  forn(i, 0, n - 1) {
    if (!m[A[i]])
      m[A[i]] = k++;
  }

  forn(i, 1, n) arr[i] = m[arr[i]];

  // forn(i,1,n)cout<<arr[i]<<" ";nl;

  forn(i, 1, n - 1) {
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  dfs(1, 0);
  forn(i, 1, n) cout << ans[i] << "\\n";
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin>>t;
  forn(i, 1, t) {
    solve();
    nl;
  }

  return 0;
}
//       clang++ c.cpp ; ./a.out < a.txt ;

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define st(arr, a) memset(arr, a, sizeof arr)
#define nl cout << '\\n'
#define bitcount(x) __builtin_popcountll(x)
const int MOD = 1000000007ll;
#define forn(i, a, b) for (int i = a; i <= b; i++)
#define rfor(i, a, b) for (int i = a; i >= b; i--)
#define all(x) x.begin(), x.end()
#define pi pair<int, int>
#define X first
#define Y second
#define sz(A) (int)A.size()
#define N 1014159
#define vi vector<int>
#define v vector
#define die exit(0)
#define alln(arr) arr + 1, arr + 1 + n
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;

// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).

#define in input_int()
#define ins input_string()
int input_int() {
  int a;
  cin >> a;
  return a;
}
string input_string() {
  string s;
  cin >> s;
  return s;
}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int t[N * 4];

int ans[N];

vi adj[N];
map<int, int> m;

int sum(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    return t[v];
  }
  int tm = (tl + tr) / 2;
  return max(sum(v * 2, tl, tm, l, min(r, tm)),
             sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}

void update(int v, int tl, int tr, int pos, int new_val) {
  if (tl == tr) {
    t[v] = new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2, tl, tm, pos, new_val);
    else
      update(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[v] = max(t[v * 2], t[v * 2 + 1]);
  }
}

int n, k, a, b, c, d, x, y, arr[N];
string s;

void dfs(int u, int p) {
  int x = sum(1, 1, n, 1, arr[u] - 1);
  int y = sum(1, 1, n, arr[u], arr[u]);
  ans[u] = x + 1;
  update(1, 1, n, arr[u], x + 1);
  for (auto i : adj[u]) {
    if (i == p)
      continue;
    dfs(i, u);
  }
  update(1, 1, n, arr[u], y);
}

void solve() {
  cin >> n;
  forn(i, 1, n) cin >> arr[i];

  vi A;
  forn(i, 1, n) A.pb(arr[i]);
  sort(all(A));
  int k = 2;
  forn(i, 0, n - 1) {
    if (!m[A[i]])
      m[A[i]] = k++;
  }

  forn(i, 1, n) arr[i] = m[arr[i]];

  forn(i, 1, n - 1) {
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  dfs(1, 0);
  forn(i, 1, n) cout << ans[i] << "\\n";
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin>>t;
  forn(i, 1, t) {
    solve();
    nl;
  }

  return 0;
}
//       clang++ c.cpp ; ./a.out < a.txt ;