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

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll MAX = 2e5 + 10;
ll n;
ll len;
ll u0, v0;
ll a[MAX];
ll ans[MAX];
vector<ll> adj[MAX];
vector<stack<ll>> dp(MAX);

void dfs(ll cur, ll par) {
  bool flag = 0;
  ll ind;
  if (a[cur] <= dp[1].top()) {
    dp[1].push(a[cur]);
    ind = 1;
  } else if (a[cur] > dp[len].top()) {
    len++;
    flag = true;
    dp[len].push(a[cur]);
    ind = len;
  } else {
    ll left = 2;
    ll right = len;
    while (left <= right) {
      ll mid = (left + right) / 2;
      if (dp[mid].top() >= a[cur]) {
        right = mid - 1;
      } else
        left = mid + 1;
    }
    ind = right + 1;
    dp[ind].push(a[cur]);
  }
  ans[cur] = len;
  for (ll i = 0; i < adj[cur].size(); i++) {
    if (adj[cur][i] == par)
      continue;
    dfs(adj[cur][i], cur);
  }
  dp[ind].pop();
  len -= flag;
}
int main() {
  /// freopen("path_02.txt","r",stdin);
  /// freopen("path_02_out.txt","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (ll i = 1; i < n; i++) {
    cin >> u0 >> v0;
    adj[u0].push_back(v0);
    adj[v0].push_back(u0);
  }
  dp[1].push(a[1]);
  len = 1;
  ans[1] = 1;
  for (ll j = 0; j < adj[1].size(); j++) {
    dfs(adj[1][j], 1);
  }
  for (ll j = 1; j <= n; j++) {
    cout << ans[j] << endl;
  }
  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
#define endl '\\n'
#define ll long long
using namespace std;
const ll MAX = 2e5 + 100;
ll u0, v0;
vector<vector<ll>> adj(MAX);
vector<ll> a(MAX);
vector<ll> ans(MAX);
vector<stack<ll>> dp(MAX);
void dfs(ll cur, ll par, ll len) {
  ll ind;
  if (a[cur] <= dp[1].top()) {
    dp[1].top() = a[cur];
    ind = 1;
  } else if (a[cur] > dp[len].top()) {
    len++;
    dp[len].push(a[cur]);
    ind = len;
  } else {
    ll left = 1;
    ll right = len;
    while (left <= right) {
      ll mid = (left + right) / 2;
      if (dp[mid].top() > a[cur]) {
        right = mid - 1;
      } else
        left = mid + 1;
    }
    dp[right + 1].push(a[cur]);
    ind = right + 1;
  }
  ans[cur] = len;
  for (ll i = 0; i < adj[cur].size(); i++) {
    if (adj[cur][i] == par)
      continue;
    dfs(adj[cur][i], cur, len);
  }
  dp[ind].pop();
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (ll i = 1; i < n; i++) {
    cin >> u0 >> v0;
    adj[u0].push_back(v0);
    adj[v0].push_back(u0);
  }
  dp[1].push(a[1]);
  ans[1] = 1;
  for (ll i = 0; i < adj[1].size(); i++) {
    dfs(adj[1][i], 1, 1);
  }
  for (ll i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

/**#include<bits/stdc++.h>
#define ll long long
#define endl '\\n'
using namespace std;
const ll MAX = 2e5+10;
ll res[MAX];
ll n;
map<ll,ll> m;
vector<ll> a;
vector<ll> t(4*MAX);
ll u0,v0;
vector<ll> adj[MAX];
ll max_num(vector<ll> &tree,ll v,ll tl,ll tr,ll l,ll r)
{
    if(tl > r || tr < l)
    {
        return 0;
    }
    if(tl >= l && tr <= r)
    {
        return tree[v];
    }
    ll mid = (tl+tr)/2;
    return max(max_num(tree,v*2,tl,mid,l,r),max_num(tree,v*2+1,mid+1,tr,l,r));
}
void update(vector<ll> &tree,ll v,ll tl,ll tr,ll l,ll r,ll new_val)
{
    if(tl>r || tr<l)
    {
        return;
    }
    if(tl == tr)
    {
        tree[v] = max(tree[v],new_val);
        return;
    }
    ll mid = (tl+tr)/2;
    update(tree,v*2,tl,mid,l,r,new_val);
    update(tree,v*2+1,mid+1,tr,l,r,new_val);
    tree[v] = max(tree[v*2],tree[v*2+1]);
}
void dfs(vector<ll> tree,ll cur,ll par,ll ans)
{
    ll maxi=1;
    if(m[a[cur]]>0)maxi = max_num(tree,1,0,n-1,0,m[a[cur]]-1)+1;
    update(tree,1,0,n-1,m[a[cur]],m[a[cur]],maxi);
    ans = max(ans,maxi);
    res[cur] = ans;
    for(ll i=0; i<adj[cur].size(); i++)
    {
        if(adj[cur][i] == par)continue;
        else
        {
            dfs(tree,adj[cur][i],cur,ans);
        }
    }
}
ll main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin>>n;
    set< pair<ll,ll> > seq;
    a.resize(n+1);
    for(ll i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<ll> b(n+1);
    b = a;
    sort(b.begin()+1,b.end());
    ll ind = 0;
    for(ll i=1;i<=n;i++)
    {
        if(m[b[i]]>0)continue;
        m[b[i]] = ind;
        ind++;
    }
    for(ll i=1;i<n;i++)
    {
        cin>>u0>>v0;
        adj[u0].push_back(v0);
        adj[v0].push_back(u0);
    }
    dfs(t,1,-1,0);
    res[1] = 1;
    for(ll i=1;i<=n;i++)
    {
        cout<<res[i]<<endl;
    }
    cout<<endl;
    return 0;
}
*/