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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_ll.hpp>
// typedef boost::multiprecision::cpp_ll ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E18;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define endl "\\n"
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

ll n;
ll a[200002];
vector<ll> v[200002];
ll ans[200002];
ll dp[200002];
ll maxi;

void hoge(ll x) {
  ll l = 0;
  ll r = n;
  while (r - l > 1) {
    ll mid = (r + l) / 2;
    if (dp[mid] < a[x]) {
      l = mid;
    } else {
      r = mid;
    }
  }
  ll k = l + 1;
  ll memo = dp[k];
  if (memo == inf) {
    maxi++;
  }
  ans[x] = maxi;
  dp[k] = a[x];
  rep(i, 0, (ll)v[x].size() - 1) {
    if (ans[v[x][i]] == 0) {
      hoge(v[x][i]);
    }
  }
  dp[k] = memo;
  if (memo == inf) {
    maxi--;
  }
}

int main() {
  fastio ll u, t;
  cin >> n;
  rep(i, 1, n) { cin >> a[i]; }
  rep(i, 0, n - 2) {
    cin >> u >> t;
    v[u].pb(t);
    v[t].pb(u);
  }
  rep(i, 0, n) { dp[i] = inf; }
  dp[0] = 0;
  maxi = 0;
  hoge(1);
  rep(i, 1, n) { cout << ans[i] << endl; }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_ll.hpp>
// typedef boost::multiprecision::cpp_ll ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E18;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define endl "\\n"
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

ll n;
ll a[200002];
vector<ll> v[200002];
ll ans[200002];
ll dp[200002];

void hoge(ll x) {
  ll *it = upper_bound(dp, dp + n, a[x]);
  ll k = it - dp;
  // cout<<k<<endl;
  ll memo = dp[k];
  ans[x] = k;
  dp[k] = a[x];
  rep(i, 0, (ll)v[x].size() - 1) {
    if (ans[v[x][i]] == 0) {
      hoge(v[x][i]);
    }
  }
  dp[k] = memo;
}

int main() {
  fastio ll u, t;
  cin >> n;
  rep(i, 1, n) { cin >> a[i]; }
  rep(i, 0, n - 2) {
    cin >> u >> t;
    v[u].pb(t);
    v[t].pb(u);
  }
  rep(i, 0, n) { dp[i] = inf; }
  dp[0] = 0;
  hoge(a[1]);
  rep(i, 1, n) { cout << ans[i] << endl; }

  return 0;
}