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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define mod ((ll)1e9 + 7)
#define inf 0x3f3f3f3f
#define infl 0x3f3f3f3f3f3f3f3f
#define Ninf -0x3e3e3e3f
#define Ninfl -0x3e3e3e3e3e3e3e3f
#define maxx ((ll)2e5 + 5)
#define endl "\\n"
#define time(i)                                                                \\
  cout << "\\nTime Elapsed " << i << " : " << 1.0 * clock() / CLOCKS_PER_SEC    \\
       << " sec\\n";
#define usefile                                                                \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define test                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
ll exp(ll x, ll y, ll p) {
  ll res = 1;
  while (y) {
    if (y % 2)
      res = (res * x % p) % p;
    x = (x * x) % p;
    y /= 2;
  }
  return res;
}
ll expm(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y % 2)
      res = (res * x % mod) % mod;
    x = (x * x) % mod;
    y /= 2;
  }
  return res;
}
ll add(ll x, ll y) {
  x += y;
  if (x >= mod)
    return x - mod;
  return x;
}
ll sub(ll x, ll y) {
  x -= y;
  if (x < 0)
    return x + mod;
  return x;
}
ll mult(ll x, ll y) { return (x * y) % mod; }
ll rev(ll x) { return expm(x, mod - 2); }
/*
bool sv[maxx]={0};    // returns 0 if a num is prime
int sp[maxx]={0};     // gives smallest prime factor of the number
void Sieve() {for(int i= 2; i< maxx; i+= 2) sv[i]=1,sp[i] = 2;sv[2]=0;for(ll i =
3; i < maxx; i += 2){if (!sv[i]){sp[i] = i;for (ll j = i; (j*i) < maxx; j +=
2){if (!sv[j*i])sv[j*i] = true, sp[j*i] = i;}}}} ll fact[maxx]={0}; // returns
factorial of that number void facto() {fact[0]=1;fact[1]=1;for(int
i=2;i<maxx;i++)fact[i]=(fact[i-1]*i)%mod;} ll ncrm(ll n,ll r) {ll res=1;
res=fact[n]; res=(res*(expm(fact[r],mod-2)))%mod;
res=(res*(expm(fact[n-r],mod-2)))%mod; return res;} ll inv[maxx];   // returns
the modular inverse of number void inversify( ll m){inv[1]=1;for(int
i=2;i<maxx;i++){inv[i]= m- m/i*inv[m%i]%m;}} ll ncr(ll n,ll r,ll m){int
ans=1;for (int i = 0; i < r; i++){ans=ans*(n-i)%m*inv[i+1]%m;}return ans;}
*/
vector<int> val(maxx);
vector<int> adj[maxx];
vector<int> dp(maxx, infl);
vector<int> ans(maxx);
int n;
void func(int u, int par) {
  auto it = lower_bound(dp.begin(), dp.end(), val[u]);
  ans[u] = max(ans[par], it - dp.begin() + 1LL);
  int pre = *it;
  *it = val[u];
  for (auto v : adj[u]) {
    if (v == par)
      continue;
    func(v, u);
  }
  *it = pre;
}

signed main() {
  fast;
  // test
  {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> val[i];
    }
    for (int i = 0; i < n - 1; i++) {
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      // cerr<<x<<" "<<y<<endl;
      adj[x].push_back(y);
      adj[y].push_back(x);
    }
    func(0, 0);
    for (int i = 0; i < n; i++) {
      cout << ans[i] << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define mod ((ll)1e9 + 7)
#define inf 0x3f3f3f3f
#define infl 0x3f3f3f3f3f3f3f3f
#define Ninf -0x3e3e3e3f
#define Ninfl -0x3e3e3e3e3e3e3e3f
#define maxx ((ll)2e5 + 5)
#define endl "\\n"
#define time(i)                                                                \\
  cout << "\\nTime Elapsed " << i << " : " << 1.0 * clock() / CLOCKS_PER_SEC    \\
       << " sec\\n";
#define usefile                                                                \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define fast                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define test                                                                   \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
ll exp(ll x, ll y, ll p) {
  ll res = 1;
  while (y) {
    if (y % 2)
      res = (res * x % p) % p;
    x = (x * x) % p;
    y /= 2;
  }
  return res;
}
ll expm(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y % 2)
      res = (res * x % mod) % mod;
    x = (x * x) % mod;
    y /= 2;
  }
  return res;
}
ll add(ll x, ll y) {
  x += y;
  if (x >= mod)
    return x - mod;
  return x;
}
ll sub(ll x, ll y) {
  x -= y;
  if (x < 0)
    return x + mod;
  return x;
}
ll mult(ll x, ll y) { return (x * y) % mod; }
ll rev(ll x) { return expm(x, mod - 2); }
/*
bool sv[maxx]={0};    // returns 0 if a num is prime
int sp[maxx]={0};     // gives smallest prime factor of the number
void Sieve() {for(int i= 2; i< maxx; i+= 2) sv[i]=1,sp[i] = 2;sv[2]=0;for(ll i =
3; i < maxx; i += 2){if (!sv[i]){sp[i] = i;for (ll j = i; (j*i) < maxx; j +=
2){if (!sv[j*i])sv[j*i] = true, sp[j*i] = i;}}}} ll fact[maxx]={0}; // returns
factorial of that number void facto() {fact[0]=1;fact[1]=1;for(int
i=2;i<maxx;i++)fact[i]=(fact[i-1]*i)%mod;} ll ncrm(ll n,ll r) {ll res=1;
res=fact[n]; res=(res*(expm(fact[r],mod-2)))%mod;
res=(res*(expm(fact[n-r],mod-2)))%mod; return res;} ll inv[maxx];   // returns
the modular inverse of number void inversify( ll m){inv[1]=1;for(int
i=2;i<maxx;i++){inv[i]= m- m/i*inv[m%i]%m;}} ll ncr(ll n,ll r,ll m){int
ans=1;for (int i = 0; i < r; i++){ans=ans*(n-i)%m*inv[i+1]%m;}return ans;}
*/
vector<int> val(maxx);
vector<int> adj[maxx];
vector<int> dp(maxx, infl);
vector<int> ans(maxx);
int n;
void func(int u, int par) {
  int pre = dp[u];
  auto it = lower_bound(dp.begin(), dp.end(), val[u]);
  ans[u] = max(ans[par], it - dp.begin() + 1LL);
  *it = val[u];
  for (auto v : adj[u]) {
    if (v == par)
      continue;
    func(v, u);
  }
  dp[u] = pre;
}

signed main() {
  fast;
  // test
  {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> val[i];
    }
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      x--;
      y--;
      adj[x].push_back(y);
      adj[y].push_back(x);
    }
    func(0, 0);
    for (int i = 0; i < n; i++) {
      cout << ans[i] << endl;
    }
  }
  return 0;
}