alexjercan/bugnet huggingface dataset:
Problem p01540 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int sum[6000][6000];
int X[6000], Y[6000];
int X1[600000], X2[600000], Y1[600000], Y2[600000];
int n, m;

int compress(int *x, int *X1, int *X2) {
  vector<int> v;
  rep(i, n) v.push_back(x[i]);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  rep(i, m) {
    X1[i] = lower_bound(v.begin(), v.end(), X1[i]) - v.begin();
    X2[i] = upper_bound(v.begin(), v.end(), X2[i]) - v.begin() - 1;
  }
  rep(i, n) { x[i] = lower_bound(v.begin(), v.end(), x[i]) - v.begin(); }
  return (int)v.size();
}
signed main() {
  scanf("%d%d", &n, &m);
  rep(i, n) scanf("%d%d", &X[i], &Y[i]);
  rep(i, m) scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);
  int h = compress(X, X1, X2), w = compress(Y, Y1, Y2);
  rep(i, n) sum[X[i]][Y[i]]++;
  rep(i, h + 1) for (int j = 1; j <= w; j++) { sum[i][j] += sum[i][j - 1]; }
  for (int i = 1; i <= h; i++)
    rep(j, w + 1) { sum[i][j] += sum[i - 1][j]; }
  rep(i, m) {
    if (X2[i] < 0 || Y2[i] < 0) {
      puts("0");
      continue;
    }
    int ans = sum[X2[i]][Y2[i]];
    if (X1[i] > 0)
      ans -= sum[X1[i] - 1][Y2[i]];
    if (Y1[i] > 0)
      ans -= sum[X2[i]][Y1[i] - 1];
    if (X1[i] > 0 && Y1[i] > 0)
      ans += sum[X1[i] - 1][Y1[i] - 1];
    printf("%d\\n", ans);
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int sum[5000][5000];
int X[5000], Y[5000];
int X1[500000], X2[500000], Y1[500000], Y2[500000];
int n, m;

int compress(int *x, int *X1, int *X2) {
  vector<int> v;
  rep(i, n) v.push_back(x[i]);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  rep(i, m) {
    X1[i] = lower_bound(v.begin(), v.end(), X1[i]) - v.begin();
    X2[i] = upper_bound(v.begin(), v.end(), X2[i]) - v.begin() - 1;
  }
  rep(i, n) { x[i] = lower_bound(v.begin(), v.end(), x[i]) - v.begin(); }
  return (int)v.size();
}
signed main() {
  scanf("%d%d", &n, &m);
  rep(i, n) scanf("%d%d", &X[i], &Y[i]);
  rep(i, m) scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);
  int h = compress(X, X1, X2), w = compress(Y, Y1, Y2);
  rep(i, n) sum[X[i]][Y[i]]++;
  rep(i, h + 1) for (int j = 1; j <= w; j++) { sum[i][j] += sum[i][j - 1]; }
  for (int i = 1; i <= h; i++)
    rep(j, w + 1) { sum[i][j] += sum[i - 1][j]; }
  rep(i, m) {
    if (X2[i] < 0 || Y2[i] < 0) {
      puts("0");
      continue;
    }
    int ans = sum[X2[i]][Y2[i]];
    if (X1[i] > 0)
      ans -= sum[X1[i] - 1][Y2[i]];
    if (Y1[i] > 0)
      ans -= sum[X2[i]][Y1[i] - 1];
    if (X1[i] > 0 && Y1[i] > 0)
      ans += sum[X1[i] - 1][Y1[i] - 1];
    printf("%d\\n", ans);
  }
}
Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int main() {
  int n, m;
  cin >> n >> m;
  vp in(n);
  vi x, y;
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    x.pb(a);
    y.pb(b);
    in[i] = pii(a, b);
  }
  sort(all(x));
  sort(all(y));
  x.erase(unique(all(x)), x.end());
  y.erase(unique(all(y)), y.end());
  rep(i, n) in[i] = pii(lower_bound(all(x), in[i].first) - x.begin(),
                        lower_bound(all(y), in[i].second) - y.begin());
  vvi fi(x.size() + 1, vi(y.size() + 1));
  rep(i, n) fi[in[i].first + 1][in[i].second + 1]++;
  rep(i, x.size()) rep(j, y.size()) fi[i + 1][j + 1] +=
      fi[i][j + 1] + fi[i + 1][j] - fi[i][j];
  while (m--) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a = lower_bound(all(x), a) - x.begin();
    b = lower_bound(all(y), b) - y.begin();
    c = upper_bound(all(x), c) - x.begin();
    d = upper_bound(all(y), d) - y.begin();
    cout << fi[c][d] - fi[a][d] - fi[c][b] + fi[a][b] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
typedef long long ll;
// typedef vector<int> vi;
// typedef vector<vi> vvi;
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(n);
  vector<int> y(n);
  rep(i, n) cin >> x[i] >> y[i];
  while (m--) {
    int co = 0;
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    rep(i, n) if (a <= x[i] && x[i] <= c && b <= y[i] && y[i] <= d) co++;
    cout << co << endl;
  }
}
Problem p01544 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;
int n, c, e, ans[4009], pre[4009], dp[4009], ps[4009][4009], l[8002009],
    r[8002009], p[8002009];
long long s[4009];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &s[i]), s[i] += s[i - 1];
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      l[c] = i, r[c] = j, p[c] = c, c++;
    }
  }
  sort(p, p + c, [](int i, int j) {
    return s[r[i]] - s[l[i]] != s[r[j]] - s[l[j]]
               ? s[r[i]] - s[l[i]] < s[r[j]] - s[l[j]]
               : i > j;
  });
  fill(dp + 1, dp + n + 1, -(1 << 30));
  for (int i = 0; i < c; i++) {
    if (dp[r[p[i]]] < dp[l[p[i]]] + 1) {
      dp[r[p[i]]] = dp[l[p[i]]] + 1;
      ps[r[p[i]]][l[p[i]]] = pre[l[p[i]]];
      pre[r[p[i]]] = l[p[i]];
    }
  }
  printf("%d\\n", dp[n]);
  int p1 = n, p2 = pre[n];
  while (p2 != 0) {
    ans[e++] = p2;
    swap(p1, p2);
    p2 = ps[p2][p1];
  }
  for (int i = e - 1; i >= 0; i--)
    printf(i == 0 ? "%d\\n" : "%d ", ans[i]);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
int n, c, e, ans[4009], pre[4009], dp[4009], ps[4009][4009], l[8000009],
    r[8000009], p[8000009];
long long s[4009];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &s[i]), s[i] += s[i - 1];
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      l[c] = i, r[c] = j, p[c] = c, c++;
    }
  }
  sort(p, p + c, [](int i, int j) {
    return s[r[i]] - s[l[i]] != s[r[j]] - s[l[j]]
               ? s[r[i]] - s[l[i]] < s[r[j]] - s[l[j]]
               : i > j;
  });
  fill(dp + 1, dp + n + 1, -(1 << 30));
  for (int i = 0; i < c; i++) {
    if (dp[r[p[i]]] < dp[l[p[i]]] + 1) {
      dp[r[p[i]]] = dp[l[p[i]]] + 1;
      ps[r[p[i]]][l[p[i]]] = pre[l[p[i]]];
      pre[r[p[i]]] = l[p[i]];
    }
  }
  printf("%d\\n", dp[n]);
  int p1 = n, p2 = pre[n];
  while (p2 != 0) {
    ans[e++] = p2;
    swap(p1, p2);
    p2 = ps[p2][p1];
  }
  for (int i = e - 1; i >= 0; i--)
    printf(i == 0 ? "%d\\n" : "%d ", ans[i]);
  return 0;
}
Problem p01544 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <map>
#include <stdio.h>
#include <vector>
#define INF 1000000
#define LINF 1000000000000LL
using namespace std;
typedef pair<long long, int> P;
long long a[4000];
vector<P> dp[4001];
int r[4000];
vector<int> pos;
int max(int a, int b) { return a > b ? a : b; }

int main() {
  int n, i, j;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  dp[0].push_back(P(-LINF, 0));
  for (i = 0; i < n; i++) {
    int m = dp[i].size();
    sort(dp[i].begin(), dp[i].end());
    r[0] = -INF;
    for (j = 1; j <= m; j++)
      r[j] = max(r[j - 1], dp[i][j - 1].second);
    long long s = 0;
    for (j = i + 1; j <= n; j++) {
      s += a[j - 1];
      int p = lower_bound(dp[i].begin(), dp[i].end(), P(s, 0)) - dp[i].begin();
      dp[j].push_back(P(s, r[p] + 1));
    }
  }
  P ans = P(0, -INF);
  for (i = 0; i < dp[n].size(); i++)
    if (ans.second < dp[n][i].second)
      ans = dp[n][i];
  printf("%d\\n", ans.second);
  int p = n;
  int t = ans.second;
  while (t > 0) {
    long long s = 0;
    while (1) {
      p--;
      s += a[p];
      for (i = 0; i < dp[p].size(); i++)
        if (dp[p][i].first < s && dp[p][i].second == t - 1)
          break;
      if (i < dp[p].size())
        break;
    }
    if (p > 0)
      pos.push_back(p);
    t--;
  }
  reverse(pos.begin(), pos.end());
  for (i = 0; i < pos.size(); i++)
    printf("%d ", pos[i]);
  puts("");
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <map>
#include <stdio.h>
#include <vector>
#define INF 1000000
#define LINF 1000000000000LL
using namespace std;
typedef pair<long long, int> P;
long long a[4000];
vector<P> dp[4001];
int r[4000][4000];
vector<int> pos;
int max(int a, int b) { return a > b ? a : b; }

int main() {
  int n, i, j;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  dp[0].push_back(P(-LINF, 0));
  for (i = 0; i < n; i++) {
    int m = dp[i].size();
    sort(dp[i].begin(), dp[i].end());
    r[i][0] = -INF;
    for (j = 1; j <= m; j++)
      r[i][j] = max(r[i][j - 1], dp[i][j - 1].second);
    long long s = 0;
    for (j = i + 1; j <= n; j++) {
      s += a[j - 1];
      int p = lower_bound(dp[i].begin(), dp[i].end(), P(s, 0)) - dp[i].begin();
      dp[j].push_back(P(s, r[i][p] + 1));
    }
  }
  P ans = P(0, -INF);
  for (i = 0; i < dp[n].size(); i++)
    if (ans.second < dp[n][i].second)
      ans = dp[n][i];
  printf("%d\\n", ans.second);
  int p = n;
  int t = ans.second;
  while (t > 0) {
    long long s = 0;
    while (1) {
      p--;
      s += a[p];
      for (i = 0; i < dp[p].size(); i++)
        if (dp[p][i].first < s && dp[p][i].second == t - 1)
          break;
      if (i < dp[p].size())
        break;
    }
    if (p > 0)
      pos.push_back(p);
    t--;
  }
  reverse(pos.begin(), pos.end());
  for (i = 0; i < pos.size(); i++)
    printf("%d ", pos[i]);
  puts("");
  return 0;
}
Problem p01545 (C++) — buggy submission, verdict: Runtime Error

#include <cassert>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

template <class T> inline void chmax(T &a, const T &b) {
  if (b > a)
    a = b;
}

typedef long long value; // 格納するデータの型
const value INIT = 0ll; // 初期値．目的に応じて，適切な値に変更すること．

class segment_tree {
private:
  int n;
  vector<value> dat;

  value func(value a, value b) const { // ノードの値を決める為の関数
    return max(a, b);
  }

  value query(int a, int b, int k, int l, int r) const {
    if (r <= a || b <= l) // 求める区間が，ノードの区間と重なっていない．
      return INIT;

    if (a <= l && r <= b) { // 求める区間が，ノードの区間を完全に含む．
      return dat[k];
    } else {
      const int mid = (l + r) / 2;
      const value vl = query(a, b, k * 2 + 1, l, mid);
      const value vr = query(a, b, k * 2 + 2, mid, r);
      return func(vl, vr);
    }
  }

public:
  segment_tree(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;

    dat.assign(4 * n - 1, INIT);
  }

  void update(int k, value a) { // k番目の要素をaに更新する．
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = func(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  value query(int a, int b) { // [a, b)に対するクエリ
    return query(a, b, 0, 0, n);
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int m;
  cin >> m;

  segment_tree segt(m);

  long long mx = 0;
  for (int i = 0; i < m; ++i) {
    int w;
    cin >> w;

    long long tmp = segt.query(1, w) + w;
    segt.update(w, tmp);
    chmax(mx, tmp);
  }

  cout << ((m + 1ll) * m / 2ll - mx) << endl;
  return EXIT_SUCCESS;
}

Error: 0

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

template <class T> inline void chmax(T &a, const T &b) {
  if (b > a)
    a = b;
}

typedef long long value; // 格納するデータの型
const value INIT = 0ll; // 初期値．目的に応じて，適切な値に変更すること．

class segment_tree {
private:
  int n;
  vector<value> dat;

  value func(value a, value b) const { // ノードの値を決める為の関数
    return max(a, b);
  }

  value query(int a, int b, int k, int l, int r) const {
    if (r <= a || b <= l) // 求める区間が，ノードの区間と重なっていない．
      return INIT;

    if (a <= l && r <= b) { // 求める区間が，ノードの区間を完全に含む．
      return dat[k];
    } else {
      const int mid = (l + r) / 2;
      const value vl = query(a, b, k * 2 + 1, l, mid);
      const value vr = query(a, b, k * 2 + 2, mid, r);
      return func(vl, vr);
    }
  }

public:
  segment_tree(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;

    dat.assign(2 * n - 1, INIT);
  }

  void update(int k, value a) { // k番目の要素をaに更新する．
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = func(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

  value query(int a, int b) { // [a, b)に対するクエリ
    return query(a, b, 0, 0, n);
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int m;
  cin >> m;

  segment_tree segt(m);

  long long mx = 0;
  for (int i = 0; i < m; ++i) {
    int w;
    cin >> w;

    long long tmp = segt.query(1, w) + w;
    segt.update(w, tmp);
    chmax(mx, tmp);
  }

  cout << ((m + 1ll) * m / 2ll - mx) << endl;
  return EXIT_SUCCESS;
}
Problem p01545 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define INF (1 << 30)
struct SegmentTree {
private:
  ll dat[1 << 18];
  int n;

public:
  SegmentTree() {}
  SegmentTree(int size) {
    n = 1;
    while (n < size)
      n *= 2;
  }
  ll getSize() { return n; }
  ll getSeg(int s) { return dat[s]; }
  void update(int i, ll x) {
    i += n - 1;
    dat[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      dat[i] = max(dat[i * 2 + 1], dat[i * 2 + 2]);
    }
  }
  ll query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return dat[k];
    ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
};
int main() {
  int n;
  ll ans = 0;
  cin >> n;
  SegmentTree BIT(n);
  for (int i = 0, x; i < n; i++) {
    cin >> x;
    BIT.update(x, BIT.query(1, x + 1, 0, 0, BIT.getSize()) + x);
    ans = max(ans, BIT.getSeg(x + BIT.getSize() - 1));
  }
  cout << ((ll)n * (ll)(n + 1) / 2 - ans) << endl;
}

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 <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
#define INF (1 << 30)
struct SegmentTree {
private:
  vector<ll> dat;
  int n;

public:
  SegmentTree() {}
  SegmentTree(int size) {
    n = 1;
    while (n < size)
      n *= 2;
    dat = vector<ll>(2 * n - 1, 0);
  }
  int getSize() { return n; }
  ll getSeg(int s) { return dat[s]; }
  void update(int i, ll x) {
    i += n - 1;
    dat[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      dat[i] = max(dat[i * 2 + 1], dat[i * 2 + 2]);
    }
  }
  ll query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return dat[k];
    ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
};
int main() {
  int n;
  ll ans = 0;
  cin >> n;
  SegmentTree BIT(n);
  for (int i = 0, x; i < n; i++) {
    cin >> x;
    BIT.update(x, BIT.query(1, x + 1, 0, 0, BIT.getSize()) + x);
    ans = max(ans, BIT.getSeg(x + BIT.getSize() - 1));
  }
  cout << (n * (n + 1) / 2 - ans) << endl;
}
Problem p01545 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <functional> //for "greater<type>"
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define pb push_back
#define fi first
#define sc second
#define mp make_pair
#define is insert //?????????

#define BIT_MAX_N 100000

int n;
long long bit[BIT_MAX_N + 1];

void update(int k, long long a) {
  while (k <= n) {
    bit[k] = max(a, bit[k]);
    k = (k | (k - 1)) + 1;
  }
}

long long s(int k) {
  long long z = 0;
  while (k > 0) {
    z = max(z, bit[k]);
    k = k & (k - 1);
  }
  return z;
}

int main() {
  cin >> n;
  fill(bit, bit + n + 1, 0);
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    update(x, s(x - 1) + x);
  }
  cout << ((long long)n * (n + 1)) / 2 - s(n) << endl;
  ;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define pb push_back
#define fi first
#define sc second
#define mp make_pair
#define is insert
const int inf = 100001;

struct plli1 {
  long long fi;
  int sc;
};
struct plli2 {
  long long fi;
  int sc;
};
typedef set<plli1> slli1;
typedef set<plli2> slli2;

bool operator<(plli1 p, plli1 q) { return p.fi < q.fi; }

bool operator>(plli1 p, plli1 q) { return p.fi > q.fi; }

bool operator<(plli2 p, plli2 q) { return p.sc < q.sc; }

bool operator>(plli2 p, plli2 q) { return p.sc > q.sc; }

int main() {
  int n;
  slli1 s;
  slli2 t;
  s.is((plli1){0, 0});
  s.is((plli1){1, inf});
  t.is((plli2){0, 0});
  t.is((plli2){1, inf});
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    slli2::iterator b = upper_bound(t.begin(), t.end(), ((plli2){0, x}));
    long long y = ((*b).fi);
    slli1::iterator e = lower_bound(s.begin(), s.end(), ((plli1){y + x, 0}));
    int z = (*(--e)).sc;
    ++e;
    plli1 c1 = ((plli1){(*b).fi, (*b).sc});
    plli2 f1 = ((plli2){(*e).fi, (*e).sc});
    slli1::iterator b1 = s.find(c1);
    slli2::iterator e1 = t.find(f1);
    s.erase(b1, e);
    t.erase(b, e1);
    s.is((plli1){y, x});
    s.is((plli1){y + x, z});
    t.is((plli2){y, x});
    t.is((plli2){y + x, z});
  }
  slli1::iterator e = s.end();
  --e;
  cout << ((long long)n * (n + 1) / 2) - (*e).fi + 1 << endl;
}
Problem p01545 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

const int MAX_N = 1000000;

typedef long long ll;

using namespace std;

ll n, dat[2 * MAX_N - 1];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
}

void update(int k, ll a) {
  k += n - 1;
  dat[k] = a;

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

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

int main(void) {

  ll in, m, ans = 0;

  cin >> m;

  init(m);

  for (int i = 0; i < m; i++) {
    cin >> in;
    update(in, in + query(1, in + 1, 0, 0, n));
    // ans=max(ans,dat[in+n-1]);
  }
  cout << m * (m + 1) / 2 - query(1, m + 1, 0, 0, n) << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

const int MAX_N = 100000;

typedef long long ll;

using namespace std;

ll n, dat[2 * MAX_N - 1];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
}

void update(int k, ll a) {
  k += n - 1;
  dat[k] = a;

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

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

int main(void) {

  ll in, m, ans = 0;

  cin >> m;

  init(m);

  for (int i = 0; i < m; i++) {
    cin >> in;
    update(in, in + query(1, in + 1, 0, 0, n));
    // ans=max(ans,dat[in+n-1]);
  }
  cout << m * (m + 1) / 2 - query(1, m + 1, 0, 0, n) << endl;

  return 0;
}
Problem p01545 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct SegmentTree {
  ll data[500010];
  int sz;
  void init(int n) {
    sz = 1;
    while (sz < n)
      sz *= 2;
    memset(data, 0LL, sizeof(data));
  }
  void update(int k, ll x) {
    k += sz - 1;
    data[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = max(data[2 * k + 1], data[2 * k + 2]);
    }
  }
  ll at(int k) { return data[k + sz - 1]; }
  ll query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return data[k];
    ll left = query(a, b, 2 * k + 1, l, (l + r) / 2);
    ll right = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return max(left, right);
  }
  ll query(int a, int b) { return query(a, b, 0, 0, sz); }
  int size() { return sz; }
};

int main() {
  SegmentTree tree;
  int n;
  cin >> n;
  tree.init(n);
  ll maxcost = 0;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    ll cost = tree.query(1, x + 1);
    tree.update(x, cost + x);
    maxcost = max(maxcost, tree.at(x));
  }
  cout << ((ll)n + 1LL) * n / 2LL - maxcost << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct SegmentTree {
  ll data[200010];
  int sz;
  void init(int n) {
    sz = 1;
    while (sz < n)
      sz *= 2;
    memset(data, 0LL, sizeof(data));
  }
  void update(int k, ll x) {
    k += sz - 1;
    data[k] = x;
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = max(data[2 * k + 1], data[2 * k + 2]);
    }
  }
  ll at(int k) { return data[k + sz - 1]; }
  ll query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return -1;
    if (a <= l && r <= b)
      return data[k];
    ll left = query(a, b, 2 * k + 1, l, (l + r) / 2);
    ll right = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return max(left, right);
  }
  ll query(int a, int b) { return query(a, b, 0, 0, sz); }
  int size() { return sz; }
};

int main() {
  SegmentTree tree;
  int n;
  cin >> n;
  tree.init(n);
  ll maxcost = 0;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    ll cost = tree.query(1, x + 1);
    tree.update(x, cost + x);
    maxcost = max(maxcost, tree.at(x));
  }
  cout << ((ll)n + 1LL) * n / 2LL - maxcost << endl;
  return 0;
}