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

#include <iostream>
using namespace std;
const int MAXN = 500000 + 5;
int n, nu[MAXN], an[MAXN], tt(0);
void init() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> nu[i];
}
void sor(int l, int r) {
  if (r - l > 1) {
    int m(l + (r - l) / 2), p(l), q(m), i(l); // l - m-1,m - r
    sor(l, m);
    sor(m, r);
    // cout<<"lmr  p="<<p<<"   m="<<m<<"  r="<<r<<endl;
    while (p < m || q < r) {
      if (q >= r || (p < m && nu[p] <= nu[q]))
        an[i++] = nu[p++];
      else
        an[i++] = nu[q++];
      tt++;
      // cout<<"p="<<p<<"   q="<<q<<endl;
      // cout<<an[i-1]<<' ';
      // system("pause");
    }
    // cout<<endl;
    for (i = l; i < r; i++)
      nu[i] = an[i];
    // cout<<"l="<<l<<"   r="<<r<<endl;for(i=l;i<r;i++) cout<<nu[i]<<'
    // ';cout<<endl;
  }
}
int main() {
  init();
  sor(0, n);
  for (int i = 0; i < n - 1; i++)
    cout << nu[i] << ' ';
  cout << nu[n - 1] << endl << tt << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <fstream>
using namespace std;
ifstream cin("w.in");
ofstream cout("w.out");
int n, num[600001];
int s[600001];
int tot = 0;
void hb(int s1, int e1, int s2, int e2) {
  int w = s1, t = s1;
  while (s1 <= e1 && s2 <= e2) {
    if (num[s1] < num[s2])
      s[w++] = num[s1++];
    else
      s[w++] = num[s2++];
    tot++;
  }
  if (s1 <= e1) {
    for (s1; s1 <= e1; s1++) {
      s[w++] = num[s1];
      tot++;
    }
  } else {
    for (s2; s2 <= e2; s2++) {
      s[w++] = num[s2];
      tot++;
    }
  }
  for (int i = t; i <= e2; i++) {
    num[i] = s[i];
    s[i] = 0;
  }
}
void gb(int l, int r) {
  if (l == r)
    return;
  int mid = (l + r) / 2;
  gb(l, mid);
  gb(mid + 1, r);
  hb(l, mid, mid + 1, r);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> num[i];
  gb(0, n - 1);
  for (int i = 0; i < n - 1; i++)
    cout << num[i] << " ";
  cout << num[n - 1] << "\\n";
  cout << tot << "\\n";
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN 500000 + 2
int AA[MAXN], TT[MAXN];
int n, g = 0;
void input() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> AA[i];
  }
}
void syf(int *A, int x, int y, int *T) {
  if (y - x > 1) {
    int m = (x + y) / 2;
    int i = x, p = x, q = m;
    syf(A, x, m, T);
    syf(A, m, y, T);
    while (p < m || q < y) {
      if (q >= y || (p < m && A[p] <= A[q]))
        T[i++] = A[p++];
      else
        T[i++] = A[q++];
      g++;
    }
    for (int i = x; i < y; ++i) {
      A[i] = T[i];
      AA[i] = T[i];
    }
  }
}
int main() {
  input();
  syf(AA, 0, n, TT);
  for (int i = 0; i < n - 1; ++i) {
    cout << AA[i] << " ";
  }
  cout << AA[n - 1] << endl;
  cout << g << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN 100000 + 2
int AA[MAXN], TT[MAXN];
int n, g = 0;
void input() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> AA[i];
  }
}
void syf(int *A, int x, int y, int *T) {
  if (y - x > 1) {
    int m = (x + y) / 2;
    int i = x, p = x, q = m;
    syf(A, x, m, T);
    syf(A, m, y, T);
    while (p < m || q < y) {
      if (q >= y || (p < m && A[p] <= A[q]))
        T[i++] = A[p++];
      else
        T[i++] = A[q++];
      g++;
    }
    for (int i = x; i < y; ++i) {
      A[i] = T[i];
      AA[i] = T[i];
    }
  }
}
int main() {
  input();
  syf(AA, 0, n, TT);
  for (int i = 0; i < n - 1; ++i) {
    cout << AA[i] << " ";
  }
  cout << AA[n - 1] << endl;
  cout << g << endl;
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

//
//  test.cpp
//  AtCoder
//
//  Created by A on 2018/06/01.
//  Copyright © 2018年 A. All rights reserved.
//

#include <iostream>
using namespace std;
#define MAX 500000
#define SENTINEL 2000000000

int L[MAX / 2 + 2], R[MAX / 2 + 2];
int cnt;

void merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = R[n2] = SENTINEL;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    cnt++;
    if (L[i] <= R[j]) {
      A[k] = L[i++];
    } else {
      A[k] = R[j++];
    }
  }
}

void mergeSort(int A[], int n, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;

    mergeSort(A, n, left, mid);
    mergeSort(A, n, mid, right);
    merge(A, n, left, mid, right);
  }
}

int main() {
  int A[MAX], n, i;
  cnt = 0;

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

  mergeSort(A, n, 0, n);

  for (i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;

  cout << cnt << endl;

  return 0;
}


Error: 0

Fixed submission:

//
//  test.cpp
//  AtCoder
//
//  Created by A on 2018/06/01.
//  Copyright © 2018年 A. All rights reserved.
//

#include <iostream>
using namespace std;
#define MAX 500000
#define SENTINEL 200000000

int L[MAX / 2 + 2], R[MAX / 2 + 2];
int cnt;

void merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = R[n2] = SENTINEL;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    cnt++;
    if (L[i] <= R[j]) {
      A[k] = L[i++];
    } else {
      A[k] = R[j++];
    }
  }
}

void mergeSort(int A[], int n, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;

    mergeSort(A, n, left, mid);
    mergeSort(A, n, mid, right);
    merge(A, n, left, mid, right);
  }
}

int main() {
  int A[MAX], n, i;
  cnt = 0;

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

  mergeSort(A, n, 0, n);

  for (i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;

  cout << cnt << endl;

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

//??????????????????
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep3(i, k, n) for (int i = k; i < n; i++)
#define INFTY 2000000000
using namespace std;

int L[250002], R[250002], cnt;

//???????????????????£????????????§????????????
void merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  rep(i, n1) { L[i] = A[left + i]; }
  rep(i, n2) { R[i] = A[mid + i]; }
  L[n1] = INFTY;
  R[n2] = INFTY;
  int i = 0, j = 0, k;
  rep3(k, left, right) {
    cnt++;
    if (L[i] <= R[j]) {
      A[k] = L[i++];
    } else {
      A[k] = R[j++];
    }
  }
}

void MergeSort(int A[], int n, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    MergeSort(A, n, left, mid);
    MergeSort(A, n, mid, right);
    merge(A, n, left, mid, right);
  }
}

int main(void) {
  int A[500000], n, i;
  cnt = 0;

  cin >> n;
  rep(i, n) cin >> A[i];
  MergeSort(A, n, 0, n);

  rep(i, n) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;

  cout << cnt << endl;

  return 0;
}

Error: 0

Fixed submission:

//??????????????????
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep3(i, k, n) for (int i = k; i < n; i++)
#define INFTY 200000000
using namespace std;

int L[250002], R[250002], cnt;

//???????????????????£????????????§????????????
void merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  rep(i, n1) { L[i] = A[left + i]; }
  rep(i, n2) { R[i] = A[mid + i]; }
  L[n1] = INFTY;
  R[n2] = INFTY;
  int i = 0, j = 0, k;
  rep3(k, left, right) {
    cnt++;
    if (L[i] <= R[j]) {
      A[k] = L[i++];
    } else {
      A[k] = R[j++];
    }
  }
}

void MergeSort(int A[], int n, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    MergeSort(A, n, left, mid);
    MergeSort(A, n, mid, right);
    merge(A, n, left, mid, right);
  }
}

int main(void) {
  int A[500000], n, i;
  cnt = 0;

  cin >> n;
  rep(i, n) cin >> A[i];
  MergeSort(A, n, 0, n);

  rep(i, n) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;

  cout << cnt << endl;

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

#include <iostream>
using namespace std;
const int INFTY = 1000000000;
int cnt = 0;

void merge(int A[], int left, int mid, int right) {
  int n1, n2;
  int *L, *R;
  n1 = mid - left;
  n2 = right - mid;
  L = new int[n1 + 1];
  R = new int[n2 + 1];
  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = INFTY;
  R[n2] = INFTY;
  int i = 0;
  int j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i++];
      cnt++;
    } else {
      A[k] = R[j++];
      cnt++;
    }
  }
  delete[] L;
  delete[] R;
}

void mergeSort(int A[], int left, int right) {
  int mid;
  if (left + 1 < right) {
    mid = (left + right) / 2;
    mergeSort(A, left, mid);
    mergeSort(A, mid, right);
    merge(A, left, mid, right);
  }
}
int main() {
  int n;
  int *A;
  cin >> n;
  A = new int[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];
  mergeSort(A, 0, n);
  for (int i = 0; i < n - 1; i++)
    cout << A[i] << ' ';
  cout << A[n - 1] << endl << cnt << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
const int INFTY = 1000000;
int cnt = 0;

void merge(int A[], int left, int mid, int right) {
  int n1, n2;
  int *L, *R;
  n1 = mid - left;
  n2 = right - mid;
  L = new int[n1 + 1];
  R = new int[n2 + 1];
  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = INFTY;
  R[n2] = INFTY;
  int i = 0;
  int j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i++];
      cnt++;
    } else {
      A[k] = R[j++];
      cnt++;
    }
  }
  delete[] L;
  delete[] R;
}

void mergeSort(int A[], int left, int right) {
  int mid;
  if (left + 1 < right) {
    mid = (left + right) / 2;
    mergeSort(A, left, mid);
    mergeSort(A, mid, right);
    merge(A, left, mid, right);
  }
}
int main() {
  int n;
  int *A;
  cin >> n;
  A = new int[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];
  mergeSort(A, 0, n);
  for (int i = 0; i < n - 1; i++)
    cout << A[i] << ' ';
  cout << A[n - 1] << endl << cnt << endl;
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

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

int MargeElem(int elem[], int start, int mid, int end) {
  int i, j, k, l;
  int num = 0;
  int A[250001], B[250001];
  i = mid - start;
  j = end - mid;
  l = (i > j) ? i : j;
  for (k = 0; k < l; k++) {
    A[k] = elem[start + k];
    B[k] = elem[mid + k];
  }
  A[i] = 0x7FFFFFFF;
  B[j] = 0x7FFFFFFF;

  i = 0;
  j = 0;
  for (k = start; k < end; k++) {
    ++num;
    if (A[i] <= B[j]) {
      elem[k] = A[i];
      ++i;
    } else {
      elem[k] = B[j];
      ++j;
    }
  }

  return num;
}

int MargeSort(int elem[], int start, int end) {
  int mid;
  int cnt = 0;

  if ((start + 1) >= end) {
    return 0;
  }

  mid = (start + end) / 2;
  cnt += MargeSort(elem, start, mid);
  cnt += MargeSort(elem, mid, end);
  cnt += MargeElem(elem, start, mid, end);

  return cnt;
}

int main(void) {
  int i;
  int num;
  int cnt;
  int elem[500002];

  scanf("%d", &num);
  for (i = 0; i < num; i++) {
    scanf("%d", &elem[i]);
  }

  cnt = MargeSort(elem, 0, num);

  printf("%d", elem[0]);
  for (i = 1; i < num; i++) {
    printf(" %d", elem[i]);
  }
  printf("\\n%d\\n", cnt);

  return 0;
}

Error: 0

Fixed submission:

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

int MargeElem(int elem[], int start, int mid, int end) {
  int i, j, k, l;
  int num = 0;
  int A[2500], B[2500];
  i = mid - start;
  j = end - mid;
  l = (i > j) ? i : j;
  for (k = 0; k < l; k++) {
    A[k] = elem[start + k];
    B[k] = elem[mid + k];
  }
  A[i] = 0x7FFFFFFF;
  B[j] = 0x7FFFFFFF;

  i = 0;
  j = 0;
  for (k = start; k < end; k++) {
    ++num;
    if (A[i] <= B[j]) {
      elem[k] = A[i];
      ++i;
    } else {
      elem[k] = B[j];
      ++j;
    }
  }

  return num;
}

int MargeSort(int elem[], int start, int end) {
  int mid;
  int cnt = 0;

  if ((start + 1) >= end) {
    return 0;
  }

  mid = (start + end) / 2;
  cnt += MargeSort(elem, start, mid);
  cnt += MargeSort(elem, mid, end);
  cnt += MargeElem(elem, start, mid, end);

  return cnt;
}

int main(void) {
  int i;
  int num;
  int cnt;
  int elem[500002];

  scanf("%d", &num);
  for (i = 0; i < num; i++) {
    scanf("%d", &elem[i]);
  }

  cnt = MargeSort(elem, 0, num);

  printf("%d", elem[0]);
  for (i = 1; i < num; i++) {
    printf(" %d", elem[i]);
  }
  printf("\\n%d\\n", cnt);

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

#include <iostream>
#include <vector>
#define INF 1000000000
#define nmax 500005
using namespace std;

int n;
int cnt = 0;
int a[nmax];

void merge(int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int l[nmax];
  int r[nmax];

  int i, j;

  for (i = 0; i < n1; i++)
    l[i] = a[left + i];
  for (i = 0; i < n2; i++)
    r[i] = a[mid + i];

  l[n1] = INF;
  r[n2] = INF;

  i = 0;
  j = 0;

  for (int k = left; k < right; k++) {
    if (l[i] <= r[j])
      a[k] = l[i++];

    else
      a[k] = r[j++];

    cnt++;
  }
}

void mergeSort(int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;

    mergeSort(left, mid);
    mergeSort(mid, right);
    merge(left, mid, right);
  }
}

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

  mergeSort(0, n);

  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";

    cout << a[i];
  }

  cout << endl;
  cout << cnt << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
#define INF 1000000000
#define nmax 500005
using namespace std;

int n;
int cnt = 0;
vector<int> a(nmax);

void merge(int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  vector<int> l(nmax / 2 + 5);
  vector<int> r(nmax / 2 + 5);

  int i, j;

  for (i = 0; i < n1; i++)
    l[i] = a[left + i];
  for (i = 0; i < n2; i++)
    r[i] = a[mid + i];

  l[n1] = INF;
  r[n2] = INF;

  i = 0;
  j = 0;

  for (int k = left; k < right; k++) {
    if (l[i] <= r[j])
      a[k] = l[i++];

    else
      a[k] = r[j++];

    cnt++;
  }
}

void mergeSort(int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;

    mergeSort(left, mid);
    mergeSort(mid, right);
    merge(left, mid, right);
  }
}

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

  mergeSort(0, n);

  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";

    cout << a[i];
  }

  cout << endl;
  cout << cnt << endl;

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

#include <iostream>
#include <stdio.h>
using namespace std;
#define MAX 500000
#define S 2000000000

int L[MAX / 2 + 2], R[MAX / 2 + 2];
int cnt;

void merg(int a[], int n, int le, int mid, int ri) {
  int n1 = mid - le;
  int n2 = ri - mid;
  for (int i = 0; i < n1; i++)
    L[i] = a[le + i];
  for (int i = 0; i < n2; i++)
    R[i] = a[mid + i];
  L[n1] = R[n2] = S;
  int i = 0, j = 0;
  for (int k = le; k < ri; k++) {
    cnt++;
    if (L[i] <= R[j]) {
      a[k] = L[i++];
    } else {
      a[k] = R[j++];
    }
  }
}

void mergesort(int a[], int n, int le, int ri) {
  if (le + 1 < ri) {
    int mid = (le + ri) / 2;
    mergesort(a, n, le, mid);
    mergesort(a, n, mid, ri);
    merg(a, n, le, mid, ri);
  }
}

int main() {
  int a[MAX], n, i;
  cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  mergesort(a, n, 0, n);
  for (int i = 0; i < n; i++) {
    if (i) {
      cout << " ";
    }
    cout << a[i];
  }
  cout << endl;
  cout << cnt << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
using namespace std;
const int mx = 500000, mm = 200000000;
int L[mx / 2 + 2], R[mx / 2 + 2];
int cnt;

void merg(int a[], int n, int le, int mi, int ri) {
  int n1 = mi - le;
  int n2 = ri - mi;
  for (int i = 0; i < n1; i++)
    L[i] = a[le + i];
  for (int i = 0; i < n2; i++)
    R[i] = a[mi + i];
  L[n1] = R[n2] = mm;
  int i = 0, j = 0;
  for (int k = le; k < ri; k++) {
    cnt++;
    if (L[i] <= R[j]) {
      a[k] = L[i++];
    } else {
      a[k] = R[j++];
    }
  }
}

void mergesort(int a[], int n, int le, int ri) {
  if (le + 1 < ri) {
    int mi = (le + ri) / 2;
    mergesort(a, n, le, mi);
    mergesort(a, n, mi, ri);
    merg(a, n, le, mi, ri);
  }
}

int main() {
  int a[mx], n, i;
  cnt = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  mergesort(a, n, 0, n);
  for (int i = 0; i < n; i++) {
    if (i)
      printf(" ");
    printf("%d", a[i]);
  }
  printf("\\n%d\\n", cnt);
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

// #define LOCAL

#include <cstdio>

#define M 500010
#define SENTINEL 2000000000
int s = 0, A[M], L[M], R[M];

void merge(int l, int m, int r) {
  int i, j, k, n1 = m - l, n2 = r - m;
  L[n1] = R[n2] = SENTINEL;
  for (i = 0; i < n1; ++i)
    L[i] = A[l + i];
  for (i = 0; i < n2; ++i)
    R[i] = A[m + i];
  i = j = 0;
  for (k = l; k < r; ++k) {
    if (L[i] <= R[j])
      A[k] = L[i++];
    else
      A[k] = R[j++];
    ++s;
  }
}

void mergeSort(int l, int r) {
  if (l + 1 < r) {
    int m = (l + r) / 2;
    mergeSort(l, m);
    mergeSort(m, r);
    merge(l, m, r);
  }
}

int main() {
#ifdef LOCAL
  freopen("E:\\\\Temp\\\\input.txt", "r", stdin);
  freopen("E:\\\\Temp\\\\output.txt", "w", stdout);
#endif

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

  mergeSort(0, n);
  for (i = 0; i < n; ++i) {
    if (i)
      printf(" ");
    printf("%d", A[i]);
  }
  printf("\\n%d\\n", s);

  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
const int mx = 50000;
const int P = 2000000000;
using namespace std;
int cnt = 0;
int L[mx / 2 + 2], R[mx / 2 + 2];
void merg(int a[], int n, int left, int mid, int right) {

  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++)
    L[i] = a[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = a[mid + i];
  L[n1] = R[n2] = P;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    cnt++;
    if (L[i] <= R[j])
      a[k] = L[i++];
    else
      a[k] = R[j++];
  }
}
void mergesort(int a[], int n, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergesort(a, n, left, mid);
    mergesort(a, n, mid, right);
    merg(a, n, left, mid, right);
  }
}
int main() {
  int n, a[mx];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  mergesort(a, n, 0, n);
  for (int i = 0; i < n; i++) {
    if (i)
      printf(" ");
    printf("%d", a[i]);
  }
  printf("\\n");
  printf("%d\\n", cnt);
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define maxn 500010
#define INF 1000000100
using namespace std;
int flag, n;
int a[maxn];

void Merge(int l, int m, int r) {
  int n1 = m - l;
  int n2 = r - m;
  int b[n1 + 1], c[n2 + 1];
  for (int i = 0; i < n1; i++)
    b[i] = a[l + i];
  for (int i = 0; i < n2; i++)
    c[i] = a[m + i];
  int i = 0, j = 0;
  b[n1] = INF;
  c[n2] = INF;
  for (int k = l; k < r; k++) {
    flag++;
    if (b[i] <= c[j])
      a[k] = b[i++];
    else
      a[k] = c[j++];
  }
}
void MergeSort(int l, int r) {
  if (l + 1 < r) {
    int m = (l + r) / 2;
    MergeSort(l, m);
    MergeSort(m, r);
    Merge(l, m, r);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  int l, r, m;
  l = 0;
  r = n;
  flag = 0;
  MergeSort(l, r);
  for (int i = 0; i < n; i++)
    printf("%d%c", a[i], i == n - 1 ? '\\n' : ' ');
  printf("%d\\n", flag);
  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;
typedef long ll;
void Merge(ll *a, int left, int mid, int right, int *sum) {
  int n1 = mid - left, n2 = right - mid, i, j;
  ll *L, *R;
  L = new ll[n1];
  R = new ll[n2];
  for (i = 0; i < n1; i++)
    L[i] = a[left + i];
  for (i = 0; i < n2; i++)
    R[i] = a[mid + i];
  L[n1] = R[n2] = 1000000000;
  i = 0;
  j = 0;
  int k;
  for (k = left; k < right; k++) {
    (*sum)++;
    if (L[i] <= R[j]) {

      a[k] = L[i];
      i++;
    } else {
      a[k] = R[j];
      j++;
    }
  }
  delete[] L;
  delete[] R;
}

void Merge_Sort(ll *a, int left, int right, int *sum) {
  int mid;
  if (left + 1 < right) {
    mid = (left + right) / 2;
    Merge_Sort(a, left, mid, sum);
    Merge_Sort(a, mid, right, sum);
    Merge(a, left, mid, right, sum);
  }
}

int main() {
  ll n, a[500000];
  int sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  Merge_Sort(a, 0, n, &sum);
  for (int i = 0; i < n; i++)
    if (i == n - 1)
      cout << a[i] << endl;
    else
      cout << a[i] << " ";
  cout << sum << endl;
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
// #include<cmath>
using namespace std;

const int INFTY = 1000000001;
int S[500010], n, cmp;

void merge(int left, int mid, int right) {
  int n1 = mid - left, n2 = right - mid;
  vector<int> L(n1 + 1), R(n2 + 1);
  for (int i = 0; i < n1; i++)
    L[i] = S[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = S[mid + i];
  L[n1] = R[n2] = INFTY;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      S[k] = L[i];
      i += 1;
      cmp++;
    } else {
      S[k] = R[j];
      j += 1;
      cmp++;
    }
  }
}

void mergeSort(int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(left, mid);
    mergeSort(mid, right);
    merge(left, mid, right);
  }
}

int main(void) {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  mergeSort(0, n);
  int i = 0;
  for (int j = 0; j < n; j++) {
    if (i++)
      cout << " ";
    cout << S[j];
  }
  cout << endl;
  cout << cmp << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
// #include<cmath>
using namespace std;

const int INFTY = 1000000001;
int S[200010], n, cmp;

void merge(int left, int mid, int right) {
  int n1 = mid - left, n2 = right - mid;
  vector<int> L(n1 + 1), R(n2 + 1);
  for (int i = 0; i < n1; i++)
    L[i] = S[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = S[mid + i];
  L[n1] = R[n2] = INFTY;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      S[k] = L[i];
      i += 1;
      cmp++;
    } else {
      S[k] = R[j];
      j += 1;
      cmp++;
    }
  }
}

void mergeSort(int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(left, mid);
    mergeSort(mid, right);
    merge(left, mid, right);
  }
}

int main(void) {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  mergeSort(0, n);
  int i = 0;
  for (int j = 0; j < n; j++) {
    if (i++)
      cout << " ";
    cout << S[j];
  }
  cout << endl;
  cout << cmp << endl;
  return 0;
}