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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

#define REP(i, n) for (int i = 0; (i) < (n); ++(i))
#define FOR(i, n) for (int i = 1; (i) <= (n); ++(i))
#define dump(x) cout << #x << " = " << (x) << endl;
#define pb push_back
#define int long long

const int INF = 1e18;
const int MOD = 1e9 + 7;
// const lint LINF = 1e18;
const double eps = 0.000000001; // もとの値の10^(-16)まで

using namespace std;

// typedef pair<int, int> P;
// priority_queue< P, vector<P>, greater<P> >
// q;//ダイクストラの時、greaterで小さい順 cout << fixed << setprecision(10) <<
// ans << endl; int gcd(int a,int b){return b?gcd(b,a%b):a;}

// 最長増加部分列(Longest Increasing Subsequence) を求める
/*
signed main(){
    MAX_N = 200000;
    int lis_dp[MAX_N];
    int n;
    cin >> n;
    REP(i,n){
        cin >> a[i];
    }
    fill(lis_dp,lis_dp+n,INF);
    REP(i,n){
        *lower_bound(lis_dp,lis_dp+n,a[i]) = a[i];
    }
    int lis_ans = lower_bound(lis_dp,lis_dp+n,INF)-lis_dp;
    cout << lis_ans << endl;
    return 0;
}
*/

/*
lower_boundnのtips
探索したいkey以上のイテレータを返す
vector<int> a = {1, 4, 4, 7, 7, 8, 8, 11, 13, 19};

    //イテレータを返す
    auto Iter1 = lower_bound(ALL(a), 4);
    auto Iter2 = lower_bound(ALL(a), 6);
    auto Iter3 = lower_bound(ALL(a), 7);
    auto Iter4 = lower_bound(ALL(a), 19);
    auto Iter5 = lower_bound(ALL(a), 20);

    //値の表示
    cout << "----------value----------" << endl;
    cout << "Iter1 = " << *Iter1 << endl; //Iter1 = 4
    cout << "Iter2 = " << *Iter2 << endl; //Iter2 = 7
    cout << "Iter3 = " << *Iter3 << endl; //Iter3 = 7
    cout << "Iter4 = " << *Iter4 << endl; //Iter4 = 19
    cout << "Iter5 = " << *Iter5 << endl; //Iter5 = 1326115891

    //先頭からの距離
    cout << "----------from----------" << endl;
    cout << "Iter1 = " << Iter1 - a.begin() << endl; //Iter1 = 1
    cout << "Iter2 = " << Iter2 - a.begin() << endl; //Iter2 = 3
    cout << "Iter3 = " << Iter3 - a.begin() << endl; //Iter3 = 3
    cout << "Iter4 = " << Iter4 - a.begin() << endl; //Iter4 = 9
    cout << "Iter5 = " << Iter5 - a.begin() << endl; //Iter5 = 10

    //末尾までの距離
    cout << "----------to----------" << endl;
    cout << "Iter1 = " << a.end() - Iter1 << endl; //Iter1 = 9
    cout << "Iter2 = " << a.end() - Iter2 << endl; //Iter2 = 7
    cout << "Iter3 = " << a.end() - Iter3 << endl; //Iter3 = 7
    cout << "Iter4 = " << a.end() - Iter4 << endl; //Iter4 = 1
    cout << "Iter5 = " << a.end() - Iter5 << endl; //Iter5 = 0

    return 0;

*/
int n;
int a[300000];
int lis_dp[300000];
vector<int> edge[300000];
int ans[300000];

void dfs(int s, int p) {
  int nowbasyo = lower_bound(lis_dp, lis_dp + n, a[s]) - lis_dp;
  int nowatai = *lower_bound(lis_dp, lis_dp + n, a[s]);
  *lower_bound(lis_dp, lis_dp + n, a[s]) = a[s];
  ans[s] = lower_bound(lis_dp, lis_dp + n, INF) - lis_dp;
  REP(i, edge[s].size()) {
    if (edge[s][i] == p)
      continue;
    dfs(edge[s][i], s);
  }
  lis_dp[nowbasyo] = nowatai;
}

signed main() {
  cin >> n;
  REP(i, n) { cin >> a[i]; }
  fill(lis_dp, lis_dp + n, INF);
  REP(i, n - 1) {
    int u1, v1;
    cin >> u1 >> v1;
    u1--;
    v1--;
    edge[u1].pb(v1);
    edge[v1].pb(u1);
  }
  dfs(0, 0);
  REP(i, n) {
    cout << ans[i] << endl;
    // cout << lis_dp[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

#define REP(i, n) for (int i = 0; (i) < (n); ++(i))
#define FOR(i, n) for (int i = 1; (i) <= (n); ++(i))
#define dump(x) cout << #x << " = " << (x) << endl;
#define pb push_back
#define int long long

const int INF = 1e18;
const int MOD = 1e9 + 7;
// const lint LINF = 1e18;
const double eps = 0.000000001; // もとの値の10^(-16)まで

using namespace std;

// typedef pair<int, int> P;
// priority_queue< P, vector<P>, greater<P> >
// q;//ダイクストラの時、greaterで小さい順 cout << fixed << setprecision(10) <<
// ans << endl; int gcd(int a,int b){return b?gcd(b,a%b):a;}

// 最長増加部分列(Longest Increasing Subsequence) を求める
/*
signed main(){
    MAX_N = 200000;
    int lis_dp[MAX_N];
    int n;
    cin >> n;
    REP(i,n){
        cin >> a[i];
    }
    fill(lis_dp,lis_dp+n,INF);
    REP(i,n){
        *lower_bound(lis_dp,lis_dp+n,a[i]) = a[i];
    }
    int lis_ans = lower_bound(lis_dp,lis_dp+n,INF)-lis_dp;
    cout << lis_ans << endl;
    return 0;
}
*/

/*
lower_boundnのtips
探索したいkey以上のイテレータを返す
vector<int> a = {1, 4, 4, 7, 7, 8, 8, 11, 13, 19};

    //イテレータを返す
    auto Iter1 = lower_bound(ALL(a), 4);
    auto Iter2 = lower_bound(ALL(a), 6);
    auto Iter3 = lower_bound(ALL(a), 7);
    auto Iter4 = lower_bound(ALL(a), 19);
    auto Iter5 = lower_bound(ALL(a), 20);

    //値の表示
    cout << "----------value----------" << endl;
    cout << "Iter1 = " << *Iter1 << endl; //Iter1 = 4
    cout << "Iter2 = " << *Iter2 << endl; //Iter2 = 7
    cout << "Iter3 = " << *Iter3 << endl; //Iter3 = 7
    cout << "Iter4 = " << *Iter4 << endl; //Iter4 = 19
    cout << "Iter5 = " << *Iter5 << endl; //Iter5 = 1326115891

    //先頭からの距離
    cout << "----------from----------" << endl;
    cout << "Iter1 = " << Iter1 - a.begin() << endl; //Iter1 = 1
    cout << "Iter2 = " << Iter2 - a.begin() << endl; //Iter2 = 3
    cout << "Iter3 = " << Iter3 - a.begin() << endl; //Iter3 = 3
    cout << "Iter4 = " << Iter4 - a.begin() << endl; //Iter4 = 9
    cout << "Iter5 = " << Iter5 - a.begin() << endl; //Iter5 = 10

    //末尾までの距離
    cout << "----------to----------" << endl;
    cout << "Iter1 = " << a.end() - Iter1 << endl; //Iter1 = 9
    cout << "Iter2 = " << a.end() - Iter2 << endl; //Iter2 = 7
    cout << "Iter3 = " << a.end() - Iter3 << endl; //Iter3 = 7
    cout << "Iter4 = " << a.end() - Iter4 << endl; //Iter4 = 1
    cout << "Iter5 = " << a.end() - Iter5 << endl; //Iter5 = 0

    return 0;

*/
int n;
int a[30];
int lis_dp[30];
vector<int> edge[30];
int ans[30];

void dfs(int s, int p) {
  int nowbasyo = lower_bound(lis_dp, lis_dp + n, a[s]) - lis_dp;
  int nowatai = *lower_bound(lis_dp, lis_dp + n, a[s]);
  *lower_bound(lis_dp, lis_dp + n, a[s]) = a[s];
  ans[s] = lower_bound(lis_dp, lis_dp + n, INF) - lis_dp;
  REP(i, edge[s].size()) {
    if (edge[s][i] == p)
      continue;
    dfs(edge[s][i], s);
  }
  lis_dp[nowbasyo] = nowatai;
}

signed main() {
  cin >> n;
  REP(i, n) { cin >> a[i]; }
  fill(lis_dp, lis_dp + n, INF);
  REP(i, n - 1) {
    int u1, v1;
    cin >> u1 >> v1;
    u1--;
    v1--;
    edge[u1].pb(v1);
    edge[v1].pb(u1);
  }
  dfs(0, 0);
  REP(i, n) {
    cout << ans[i] << endl;
    // cout << lis_dp[i] << endl;
  }
  return 0;
}