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

#include <bits/stdc++.h>
using namespace std;
using P = pair<long long, long long>;
int main() {
  long long N;
  unordered_map<long long, long long> mp;
  unordered_map<long long, long long> mp0;
  cin >> N;
  vector<vector<long long>> tree(N, vector<long long>(0));
  vector<long long> answer(N);
  vector<long long> LIS(N);
  vector<long long> A(N);
  for (long long i = 0; i < N; i++) {
    cin >> A.at(i);
    LIS.at(i) = 1000000007;
  }
  for (long long i = 0; i < N - 1; i++) {
    long long a, b;
    cin >> a >> b;
    tree.at(a - 1).push_back(b - 1);
    tree.at(b - 1).push_back(a - 1);
  }
  vector<long long> dist(N, -1);
  vector<long long> pre(N, -1);
  vector<long long> countmemo(N, -1);
  stack<long long> stk;
  stack<P> stk2;
  dist.at(0) = 0;
  stk.push(0);
  pre.at(0) = 0;
  stk2.push(make_pair(0, 1000000007));
  LIS.at(0) = A.at(0);
  answer.at(0) = 1;
  long long ans = 0;
  long long count = 0;
  int lastv = -1;
  while (!stk.empty()) {
    long long v = stk.top();
    stk.pop();
    // cout<<v+1<<"   "<<count<<" "<<dist[v]<<" "<<countmemo.at(dist[v])<<endl;
    if (v > 0) {
      if (mp[v] == 1) {
        int memo = count;
        // count=memo;
        // cout<<v<<" "<<memo<<endl;
        for (long long i = 0; i < dist[lastv] - dist[v] + 1; i++) {
          // cout<<v+1<<"  "<<count-dist[v]+1<<endl;
          long long a = stk2.top().first, b = stk2.top().second;
          // cout<<1+v<<"  pop  "<<a<<" "<<b<<endl;
          stk2.pop();
          LIS.at(a) = b;
          // count=0;
        }
      }
      long long left = 0, right = N - 1, check = 1;
      ans = 0;
      while (left <= right) {
        long long mid = left + (right - left) / 2;
        if (LIS.at(mid) > A.at(v)) {
          right = mid - 1;
          check = 0;
        }
        if (LIS.at(mid) == A.at(v)) {
          break;
        }
        if (LIS.at(mid) < A.at(v)) {
          left = mid + 1;
          ans = max(ans, mid + 1);
        }
      }
      stk2.push(make_pair(ans, LIS.at(ans)));
      // cout<<1+v<<"  add  "<<ans<<" "<<LIS.at(ans)<<endl;
      if (check == 0 && LIS.at(ans) > A.at(v)) {
        // cout<<v<<" "<<ans<<"    "<<LIS.at(ans)<<" "<<A.at(v)<<endl;
        LIS.at(ans) = A.at(v);
      }
      count++;
    }
    // if(v>0)cout<<1+v<<"   "<<count<<" "<<countmemo.at(dist[v])<<endl;
    countmemo.at(v) = count;
    /*
    cout<<v+1<<endl;
    for(int i=0;i<N;i++){
            cout<<LIS.at(i)<<" ";
    }
    cout<<endl;
    */
    lastv = v;
    if (mp0[v] == 1) {
      count = 1;
    }
    if (v > 0) {
      answer.at(v) = max(answer.at(pre.at(v)), ans + 1);
    }
    if (tree.at(v).size() >= 3) {
      // count=0;
    }
    long long rock;
    for (long long i = 0; i < (long long)tree.at(v).size(); i++) {
      long long nv = tree.at(v).at(i);
      if (dist.at(nv) != -1) {
        continue;
      }
      if (v == 0) {
        mp0[nv] = 1;
      }
      stk.push(nv);
      dist.at(nv) = dist.at(v) + 1;
      pre.at(nv) = v;
      if (i < (long long)tree.at(v).size() - 1) {
        mp[nv] = 1;
      }
    }
    // cout<<endl;
  }
  for (long long i = 0; i < N; i++) {
    // cout<<i<<" "<<mp[i]<<endl;
    cout << answer.at(i) << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using P = pair<long long, long long>;
int main() {
  long long N;
  unordered_map<long long, long long> mp;
  unordered_map<long long, long long> mp0;
  cin >> N;
  vector<vector<long long>> tree(N, vector<long long>(0));
  vector<long long> answer(N);
  vector<long long> LIS(N);
  vector<long long> A(N);
  for (long long i = 0; i < N; i++) {
    cin >> A.at(i);
    LIS.at(i) = 1000000007;
  }
  for (long long i = 0; i < N - 1; i++) {
    long long a, b;
    cin >> a >> b;
    tree.at(a - 1).push_back(b - 1);
    tree.at(b - 1).push_back(a - 1);
  }
  vector<long long> dist(N, -1);
  vector<long long> pre(N, -1);
  vector<long long> countmemo(N, -1);
  stack<long long> stk;
  stack<P> stk2;
  dist.at(0) = 0;
  stk.push(0);
  pre.at(0) = 0;
  stk2.push(make_pair(0, 1000000007));
  LIS.at(0) = A.at(0);
  answer.at(0) = 1;
  long long ans = 0;
  long long count = 0;
  while (!stk.empty()) {
    long long v = stk.top();
    stk.pop();
    // cout<<v+1<<"   "<<count<<" "<<dist[v]<<" "<<countmemo.at(dist[v])<<endl;
    if (v > 0) {
      if (mp[v] == 1) {
        int memo = count;
        // count=memo;
        // cout<<v<<" "<<memo<<endl;
        for (long long i = 0; i < count - dist[v] + 1; i++) {
          // cout<<v+1<<"  "<<count-dist[v]+1<<endl;
          long long a = stk2.top().first, b = stk2.top().second;
          // cout<<v<<"  add  "<<a<<" "<<b<<endl;
          stk2.pop();
          LIS.at(a) = b;
          // count=0;
        }
      }
      long long left = 0, right = N - 1, check = 1;
      ans = 0;
      while (left <= right) {
        long long mid = left + (right - left) / 2;
        if (LIS.at(mid) > A.at(v)) {
          right = mid - 1;
          check = 0;
        }
        if (LIS.at(mid) == A.at(v)) {
          break;
        }
        if (LIS.at(mid) < A.at(v)) {
          left = mid + 1;
          ans = max(ans, mid + 1);
        }
      }
      stk2.push(make_pair(ans, LIS.at(ans)));
      if (check == 0 && LIS.at(ans) > A.at(v)) {
        // cout<<v<<" "<<ans<<"    "<<LIS.at(ans)<<" "<<A.at(v)<<endl;
        LIS.at(ans) = A.at(v);
      }
      count++;
    }
    // if(v>0)cout<<1+v<<"   "<<count<<" "<<countmemo.at(dist[v])<<endl;
    countmemo.at(v) = count;
    /*
    cout<<v+1<<endl;
    for(int i=0;i<N;i++){
            cout<<LIS.at(i)<<" ";
    }
    cout<<endl;
    */
    if (mp0[v] == 1) {
      count = 1;
    }
    if (v > 0) {
      answer.at(v) = max(answer.at(pre.at(v)), ans + 1);
    }
    if (tree.at(v).size() >= 3) {
      // count=0;
    }
    long long rock;
    for (long long i = 0; i < (long long)tree.at(v).size(); i++) {
      long long nv = tree.at(v).at(i);
      if (dist.at(nv) != -1) {
        continue;
      }
      if (v == 0) {
        mp0[nv] = 1;
      }
      stk.push(nv);
      dist.at(nv) = dist.at(v) + 1;
      pre.at(nv) = v;
      if (i < (long long)tree.at(v).size() - 1) {
        mp[nv] = 1;
      }
    }
  }
  for (long long i = 0; i < N; i++) {
    // cout<<i<<" "<<mp[i]<<endl;
    cout << answer.at(i) << endl;
  }
}