alexjercan/bugnet huggingface dataset:
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, N) for (int i = 0; i < (int)N; i++)

#define N 100000
#define INF 1e9
int a[N];
int dp[N];

int main() {
  int n;
  cin >> n;
  REP(i, n) cin >> a[i];

  fill(dp, dp + n, INF);
  for (int i = 0; i < n; i++) {
    *lower_bound(dp, dp + n, a[i]) = a[i];
  }

  cout << (lower_bound(dp, dp + n, INF) - dp) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, N) for (int i = 0; i < (int)N; i++)

#define N 100000
int a[N];
int DP[2][N];

int main() {
  int n, res;
  cin >> n;
  REP(i, n) cin >> a[i];

  REP(i, n) DP[0][i] = 0;
  DP[0][0] = 1;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= i; j++)
      DP[i & 1][j] = 0;
    for (int j = 0; j <= i; j++) {
      DP[i & 1][j] = max(DP[i & 1][j], DP[(i - 1) & 1][j]);
      if (a[j] < a[i]) {
        DP[i & 1][i] = max(DP[i & 1][i], DP[(i - 1) & 1][j] + 1);
      } else {
        DP[i & 1][i] = max(DP[i & 1][i], 1);
      }
    }
  }

  res = 0;
  for (int i = 0; i < n; i++) {
    res = max(res, DP[(n - 1) & 1][i]);
  }
  cout << res << endl;

  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

// ------------------------------------
// Date:2018/ 3/ 2
// Problem:AOJ DPL Longest Increasing Subsequence 1_D.cpp
//
// ------------------------------------

#include <bits/stdc++.h>

using namespace std;

#define EACH(i, a) for (auto &&i : a)
#define FOR(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RFOR(i, a, b) for (int i = (int)b - 1; i >= (int)a; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define debug(x) cerr << #x << ":" << x << endl

typedef long long ll;

// static const int MOD = 1000000007;

int main() {
  int n;
  int a[101010];
  // dp[num]:num番目までのLISの長さ
  int dp[101010];

  memset(a, 0, sizeof(a));
  scanf("%d", &n);
  fill_n(dp, n, 1e9 + 1);
  REP(i, n) { scanf("%d", &a[i]); }

  REP(i, n) { *lower_bound(dp, dp + n, a[i]) = a[i]; }
  printf("%d\\n", distance(dp, lower_bound(dp, dp + n, 1e9 + 1)));
  /*
  REP(i, n) {
    printf("dp[%d] = %d\\n", i, dp[i]);
  }
   */
  return 0;
}


Error: TLE

Fixed submission:

// ------------------------------------
// Date:2018/ 3/ 2
// Problem:AOJ DPL Longest Increasing Subsequence 1_D.cpp
//
// ------------------------------------

#include <bits/stdc++.h>

using namespace std;

#define EACH(i, a) for (auto &&i : a)
#define FOR(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RFOR(i, a, b) for (int i = (int)b - 1; i >= (int)a; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define debug(x) cerr << #x << ":" << x << endl

typedef long long ll;

static const int MOD = 1000000007;

int n;
int a[101010];
// dp[num]:num番目までのLISの長さ
int dp[101010];

int main() {
  memset(a, 0, sizeof(a));
  memset(dp, 0, sizeof(dp));
  scanf("%d", &n);
  REP(i, n) { scanf("%d", &a[i]); }

  int ans = 1;
  REP(i, n) {
    // 最低でもその数字自身で長さ1は保障されている;
    dp[i] = 1;
    REP(j, i) {
      if (a[j] < a[i]) {
        // a[j]にa[i]を付け足せるときに+1;
        dp[i] = max(dp[j] + 1, dp[i]);
      }
    }
    ans = max(dp[i], ans);
  }
  printf("%d\\n", ans);
  /*
  REP(i, n) {
    printf("dp[%d] -> %d\\n", i, dp[i]);
  }
   */
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define mp make_pair
#define pb push_back
#define ll __int64
#define __int64 long long
#define P pair<int, int>
#define PP pair<P, int>

int n;
int t[111111];
int dp[111111];
const int INF = 1010000001;

int main() {
  cin >> n;
  rep(i, n) cin >> t[i];
  int ans = 0;
  int r = 0;
  rep(i, n) dp[i] = INF;

  for (int i = 0; i < n; i++) {
    int index = lower_bound(dp, dp + n, t[i]) - dp;
    dp[index] = t[i];
  }

  cout << lower_bound(dp, dp + n, INF) - dp << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int n;
int array[100001];
int dp[100001];
int main() {
  cin >> n;
  array[0] = -1;
  for (int i = 1; i <= n; i++)
    cin >> array[i];

  for (int i = 1; i <= n; i++) {
    int ma = 0, index = 0;
    for (int j = 0; j < i; j++) {
      if (array[i] > array[j] && ma < dp[j])
        ma = dp[j];
    }
    dp[i] = ma + 1;
  }

  int ans = 0;
  for (int i = 0; i <= n; i++)
    ans = max(ans, dp[i]);
  cout << ans << endl;
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

// longest increasing subsequence (LIS)
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> sequence;
  vector<int> LIS;
  int initial;
  cin >> initial;
  LIS.emplace_back(initial);
  for (int i = 1; i < n; ++i) {
    int a;
    cin >> a;
    auto it = lower_bound(begin(LIS), end(LIS), a);
    if (it == end(LIS)) {
      LIS.emplace_back(a);
    } else {
      *it = a;
    }
  }
  cout << LIS.size() << endl;
}


Error: TLE

Fixed submission:

// longest increasing subsequence (LIS)
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> sequence;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    if (sequence.size() == 0 || a != sequence.back())
      sequence.emplace_back(a);
  }
  vector<int> LIS(sequence.size(), 1);
  for (size_t i = 1; i < sequence.size(); ++i) {
    for (size_t j = 0; j < i; ++j) {
      if (sequence[i] > sequence[j]) {
        if (1 + LIS[j] > LIS[i]) {
          LIS[i] = 1 + LIS[j];
        }
      }
    }
  }
  cout << *max_element(begin(LIS), end(LIS)) << endl;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
const int num = 100001, INF = 2000000000;
int a[num];
int dp[num];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  fill(dp, dp + n, INF);
  for (int i = 0; i < n; i++) {
    *lower_bound(dp, dp + n, a[i]) = a[i];
  }
  cout << lower_bound(dp, dp + n, INF) - dp << endl;
  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <iostream>
#include <set>
#include <string.h>
using namespace std;
const int num = 100001;
int a[num];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  set<int> s;
  s.insert(2000000000);
  for (int i = 0; i < n; i++) {
    set<int>::iterator it = lower_bound(s.begin(), s.end(), a[i]);
    if (*it != 2000000000) {
      s.erase(*it);
    }
    s.insert(a[i]);
  }
  cout << s.size() - 1 << endl;
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

const int INF = 1e9 + 1;
int n, a[100000], dp[100000];

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];

  for (int i = 0; i < n; ++i)
    dp[i] = INF;

  int res = 1;
  for (int i = 0; i < n; ++i) {
    int k = lower_bound(dp, dp + n, a[i]) - dp;
    dp[k] = a[i];
    res = max(res, k + 1);
  }

  cout << res << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int n, a[100000], dp[100000];

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];

  for (int i = 0; i < n; ++i)
    dp[i] = 1;

  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < i; ++j) {
      if (a[i] > a[j]) {
        dp[i] = max(dp[i], dp[j] + 1);
      }
    }
  }

  int res = 1;
  for (int i = 0; i < n; ++i)
    res = max(res, dp[i]);

  cout << res << endl;

  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

long int table[100005];
long int num[100005];
int n;

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num[i];
  }
  for (int i = 0; i < n; i++) {
    table[i] = 9999999999;
  }
  for (int i = 0; i < n; i++) {
    *lower_bound(table, table + n, num[i]) = num[i];
  }

  cout << lower_bound(table, table + n, 9999999999) - table << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int table[100000];
  int num[100000];
  int n;
  int ans = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> num[i];
  }
  for (int i = 0; i < n; i++) {
    table[i] = 1;
    for (int j = 0; j < i; j++) {
      if (num[i] > num[j])
        table[i] = max(table[i], table[j] + 1);
    }
    ans = max(ans, table[i]);
  }

  cout << ans << endl;

  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

long n, m = 1;
long a[100001], b[100001];
const long INF = 1000000001;

int main() {
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n + 1; i++)
    b[i] = INF;

  for (int i = 0; i < n; i++)
    *lower_bound(b, b + n, a[i]) = a[i];

  cout << lower_bound(b, b + n, INF) - b << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

long n, m = 0;
long a[100001], b[100001];
const long INF = 1000000001;

int main() {
  cin >> n;

  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n + 1; i++)
    b[i] = INF;

  b[0] = a[0];

  for (int i = 1; i < n; i++) {
    if (a[i] < b[0])
      b[0] = a[i];
    for (int j = 1; j <= i; j++) {
      if (a[i] < b[j] && a[i] > b[j - 1]) {
        b[j] = a[i];
        break;
      }
    }
  }

  while (b[m] < INF) {
    m++;
  }

  cout << m << endl;

  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <math.h>
#include <stack>
#include <stdio.h>

using namespace std;

int main() {

  int n, tmp;
  scanf("%d", &n);

  int L[n], length = 0;

  for (int i = 0; i < n; i++) {
    scanf("%d", &tmp);

    if (i == 0) {
      L[0] = tmp;
    } else {
      if (L[length] < tmp) {
        length++;
        L[length] = tmp;
      } else {
        for (int k = 0; k <= length; k++) {
          if (L[k] >= tmp) {
            L[k] = tmp;
            break;
          }
        }
      }
    }
  }

  printf("%d\\n", length + 1);

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  int a[1000000];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  int dp[1000000] = {};
  dp[0] = 1;
  for (int t = 1; t < 2000000000; t++) {
    for (int i = 1; i < n; i++) {
      if (a[i] > t) {
        continue;
      }
      dp[i] = max(dp[i], dp[i - 1]);
      if (a[i] > a[i - 1]) {
        dp[i] = max(dp[i], dp[i - 1] + 1);
      }
    }
  }

  cout << dp[n - 1] << endl;

  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; ++i)

const int N_MAX = 100000 + 1;
int n;
int a[N_MAX];

bool input() {
  if (cin >> n) {
    rep(i, n) cin >> a[i];
    return true;
  }
  return false;
}

int solve() {
  int l[n];
  int lc = 1;
  l[0] = a[0];
  rep(i, n) {
    if (l[lc - 1] < a[i])
      l[lc++] = a[i];
    auto it = lower_bound(l, l + lc, a[i]);
    if (it != l + lc)
      *it = a[i];
  }
  return lc;
}

int main() {
  while (input()) {
    cout << solve() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; ++i)

const int N_MAX = 100000 + 1;
int n;
int a[N_MAX];

bool input() {
  if (cin >> n) {
    rep(i, n) cin >> a[i];
    return true;
  }
  return false;
}

int solve() {
  int l[n];
  fill(l, l + n, 0);
  rep(i, n) {
    int k = -1;
    rep(j, i) {
      if (a[j] < a[i]) {
        if (k == -1)
          k = j;
        if (l[j] > l[k])
          k = j;
      }
    }
    l[i] = l[k] + 1;
  }
  return *max_element(l, l + n);
}

int main() {
  while (input()) {
    cout << solve() << endl;
  }
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

using namespace std;

int main() {
  int n;
  scanf("%d", &n);

  int A[n];
  for (int i = 0; i < n; ++i) {
    int v;
    scanf("%d", &v);
    A[i] = v;
  }

  int L[n];
  L[0] = A[0];
  int length = 1;
  for (int i = 1; i < n; ++i) {
    if (L[length - 1] < A[i]) {
      L[length++] = A[i];
    } else {
      // L[j] (j ??? 0 ?????? lengh-1) ????????§ A[i] ??\\?????¨??????????????? j
      // ????????? = A[i]
      int left = 0;
      int right = length - 1;
      int mid = (left + right) / 2;

      while (left < right) {
        if (L[mid] >= A[i]) {
          right = mid;
        } else {
          left = mid + 1;
        }
        mid = (left + right) / 2;
      }
      L[mid] = A[i];
    }
  }

  cout << length << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

int main() {

  int n;
  scanf("%d", &n);

  int A[n + 1];
  for (int i = 1; i <= n; ++i) {
    int v;
    scanf("%d", &v);
    A[i] = v;
  }
  int L[n + 1];
  L[0] = 0;
  A[0] = 0;
  for (int i = 1; i <= n; ++i) {
    int k = 0;
    for (int j = 1; j < i; ++j) {
      if (A[i] > A[j] && L[j] > L[k]) {
        k = j;
      }
    }
    L[i] = L[k] + 1;
  }

  int maxv = 0;
  for (int i = 1; i <= n; ++i) {
    maxv = max(maxv, L[i]);
  }

  cout << maxv << endl;

  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> int_pair;
typedef pair<ll, ll> ll_pair;
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
const int N = 100001;
ll n, a[N], ans = 1;
ll dp[N];

int main() {
  cin >> n;
  FOR(i, 0, n) { cin >> a[i]; }
  fill(dp, dp + n, 1e9 + 1);
  FOR(i, 0, n) { *lower_bound(dp, dp + n, a[i]) = a[i]; }
  cout << lower_bound(dp, dp + n, 1e9 + 1) - dp << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> int_pair;
typedef pair<ll, ll> ll_pair;
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
const int N = 100001;
ll n, a[N], ans = 1;
ll dp[N] = {0};
ll mi;

int main() {
  mi = 1e9 + 1;
  cin >> n;
  FOR(i, 0, n) { cin >> a[i]; }
  FOR(i, 0, n) {
    dp[i] = 1;
    if (mi >= a[i]) {
      mi = a[i];
      continue;
    }
    FOR(j, 0, i) {
      if (a[j] < a[i])
        dp[i] = max(dp[i], dp[j] + 1);
    }
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> v(n + 1, -1);
  int l = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (v[l] < a) {
      v[l + 1] = a;
      l++;
    } else {
      *lower_bound(v.begin(), v.begin() + l, a) = a;
    }
  }
  cout << l << endl;

  return 0;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> v(n + 1, -1);
  int l = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (v[l] < a) {
      v[l + 1] = a;
      l++;
    } else {
      *lower_bound(v.begin(), v.end(), a) = a;
    }
  }
  cout << l << endl;

  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define foreach(c, itr)                                                        \\
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
typedef long long ll;
typedef pair<int, int> P;

int dp[100010], a[100010];

int main(void) {

  int n;
  cin >> n;
  REP(i, n) cin >> a[i];

  const int INF = 1e9 + 7;
  fill(dp, dp + n, INF);
  REP(i, n) { *lower_bound(dp, dp + n, a[i]) = a[i]; }

  cout << lower_bound(dp, dp + n, INF) - dp << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define foreach(c, itr)                                                        \\
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
typedef long long ll;
typedef pair<int, int> P;

int dp[100010], a[100010];

int main(void) {
  int i, j;

  int n;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];

  for (i = 0; i < n; i++) {
    dp[i] = 1;
    for (j = 0; j < i; j++)
      if (a[j] < a[i])
        dp[i] = max(dp[i], dp[j] + 1);
  }

  int ans = 0;
  for (i = 0; i < n; i++)
    ans = max(ans, dp[i]);

  cout << ans << endl;

  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REP3(i, s, n) for (int i = s; i < n; i++)
using namespace std;

int main() {
  int N;
  cin >> N;
  int a[N];
  REP(i, N) { cin >> a[i]; }
  vector<int> v;
  vector<int>::iterator it;
  v.reserve(N);

  REP(i, N) {
    if (i == 0 || a[i] > *(v.end() - 1))
      v.push_back(a[i]);
    else {
      it = lower_bound(v.begin(), v.end(), a[i]);
      *it = a[i];
    }
  }
  cout << v.size() << '\\n';
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REP3(i, s, n) for (int i = s; i < n; i++)
using namespace std;

int main() {
  int N;
  cin >> N;
  int a[N], cnt[N];
  int mx = 1;
  REP(i, N) {
    cin >> a[i];
    cnt[i] = 1;
  }
  REP(i, N) {
    REP(j, i) {
      if (a[j] < a[i])
        cnt[i] = max(cnt[i], cnt[j] + 1);
    }
    mx = max(mx, cnt[i]);
  }
  cout << mx << '\\n';
}
Problem p02317 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
using namespace std;
static const int MAX_N = 1ll << 17;
typedef pair<int, int> PII;
int dat[2 * MAX_N - 1];

class Segment {
public:
  int n;
  void inti(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; ++i)
      dat[i] = -INT_MAX;
  }

  void update(int k, int a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  int query(int a, int b, int k, int l, int r) {
    if (a <= l && r <= b)
      return dat[k];
    if (r <= a || b <= l)
      return -INT_MAX;
    else {
      int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return max(vl, vr);
    }
  }
};

int N;
vector<PII> A;

signed main() {
  cin >> N;
  Segment seg;
  seg.inti(N);
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    A.push_back(PII(a, -i));
  }
  sort(A.begin(), A.end());
  for (int i = 0; i < N; ++i) {
    int j = -A[i].second;
    seg.update(j, 1);
    seg.update(j, max(dat[j + seg.n - 1], seg.query(0, j, 0, 0, seg.n) + 1));
  }
  cout << dat[0] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
static const int MAX_N = 1ll << 17;

int dat[2 * MAX_N - 1];

class Segment {
public:
  int n;
  void inti(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; ++i)
      dat[i] = -INT_MAX;
  }

  void update(int k, int a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  int query(int a, int b, int k, int l, int r) {
    if (a <= l && r <= b)
      return dat[k];
    if (r <= a || b <= l)
      return -INT_MAX;
    else {
      int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return max(vl, vr);
    }
  }
};

int N;
int A[MAX_N];

signed main() {
  cin >> N;
  Segment seg;
  seg.inti(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }
  for (int i = 0; i < N; ++i) {
    int j = A[i];
    seg.update(j, 1);
    seg.update(j, max(dat[j + seg.n - 1], seg.query(0, j, 0, 0, seg.n) + 1));
  }
  cout << dat[0] << endl;
  return 0;
}
Problem p02317 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MAX 100000
#define INF 1000000001

int a[MAX];
int l[MAX];
int main() {
  int n;
  cin >> n;
  REP(i, n) {
    cin >> a[i];
    l[i] = INF;
  }
  int length = 0;
  l[length] = a[0];
  length++;
  FOR(i, 1, n) {
    if (l[length - 1] < a[i]) {
      l[length] = a[i];
      length++;
    } else {
      REP(j, length) {
        if (j == 0 && l[0] > a[i]) {
          l[0] = a[i];
          break;
        } else if (l[j] < a[i] && l[j + 1] > a[i]) {
          l[j + 1] = a[i];
          break;
        }
      }
    }
  }
  cout << length << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MAX 100000
#define INF 1000000001

int a[MAX];
int l[MAX];
int main() {
  int n;
  cin >> n;
  REP(i, n) {
    cin >> a[i];
    l[i] = INF;
  }
  int length = 0;
  l[length] = a[0];
  length++;
  FOR(i, 1, n) {
    if (l[length - 1] < a[i]) {
      l[length] = a[i];
      length++;
    } else {
      int j = length;
      while (1) {
        j--;
        if (l[j] > a[i]) {
          l[j] = a[i];
          break;
        }
      }
    }
  }
  cout << length << endl;

  return 0;
}