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

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

int cnt = 0;
void merge(int a[], int left, int mid, int right);
void mergeSort(int a[], int left, int right);

int main() {
  int i, n, a[500001];
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  mergeSort(a, 0, n);
  for (i = 0; i < n; i++) {
    cout << a[i];
    if (i != n - 1)
      cout << " ";
  }
  cout << endl << cnt << endl;
  return 0;
}

void merge(int a[], int left, int mid, int right) {
  int i, j, k, n1, n2, l[250001], r[250001];
  n1 = mid - left;
  n2 = right - mid;
  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 (k = left; k < right; k++) {
    if (l[i] <= r[j]) {
      a[k] = l[i];
      i++;
      cnt++;
    } else {
      a[k] = r[j];
      j++;
      cnt++;
    }
  }
}

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

Error: 0

Fixed submission:

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

int cnt = 0;
void merge(int a[], int left, int mid, int right);
void mergeSort(int a[], int left, int right);

int main() {
  int i, n, a[50000];
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  mergeSort(a, 0, n);
  for (i = 0; i < n; i++) {
    cout << a[i];
    if (i != n - 1)
      cout << " ";
  }
  cout << endl << cnt << endl;
  return 0;
}

void merge(int a[], int left, int mid, int right) {
  int i, j, k, n1, n2, l[25000], r[25000];
  n1 = mid - left;
  n2 = right - mid;
  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 (k = left; k < right; k++) {
    if (l[i] <= r[j]) {
      a[k] = l[i];
      i++;
      cnt++;
    } else {
      a[k] = r[j];
      j++;
      cnt++;
    }
  }
}

void mergeSort(int a[], int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(a, left, mid);
    mergeSort(a, mid, right);
    merge(a, left, mid, right);
  }
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int a[500000], num = 0;
int l[250005], r[250005];

void merg(int s, int e) {
  int mid = (s + e) / 2;
  int n1 = mid - s + 1, n2 = e - mid;
  int i;
  for (i = 0; i < n1; i++) {
    l[i] = a[s + i];
  }
  for (i = 0; i < n2; i++) {
    r[i] = a[mid + i + 1];
  }
  int m1 = 0, m2 = 0;
  for (i = 0; i < e - s + 1; i++) {
    l[n1] = r[n2] = 1000000005;
    if (l[m1] <= r[m2])
      a[s + i] = l[m1++];
    else
      a[s + i] = r[m2++];
  }
  num += (e - s + 1);
}

void mergesort(int s, int e) {
  if (s == e)
    return;
  int mid = (s + e) / 2;
  mergesort(s, mid);
  mergesort(mid + 1, e);
  merg(s, e);
}

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

Error: -6
malloc(): corrupted top size


Fixed submission:

#include <iostream>

using namespace std;

int a[500000], num = 0;

void merg(int s, int e) {
  int mid = (s + e) / 2;
  int s1 = s;
  int e1 = mid + 1;
  int *ar = new int(e - s + 1);
  int cnt = 0;
  while (s1 <= mid && e1 <= e) {
    if (a[s1] <= a[e1]) {
      ar[cnt++] = a[s1++];
    } else {
      ar[cnt++] = a[e1++];
    }
    num++;
  }
  while (s1 <= mid) {
    ar[cnt++] = a[s1++];
    num++;
  }
  while (e1 <= e) {
    ar[cnt++] = a[e1++];
    num++;
  }
  int i;
  for (i = 0; i < e - s + 1; i++) {
    a[s + i] = ar[i];
  }
}

void mergesort(int s, int e) {
  if (s == e)
    return;
  int mid = (s + e) / 2;
  mergesort(s, mid);
  mergesort(mid + 1, e);
  merg(s, e);
}

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

#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() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int A[MAX], n, i;
  cnt = 0;

  cin >> n;
  for (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: -11

Fixed submission:

#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);
    mergeSort(A, n, left, mid);
    mergeSort(A, n, mid, right);
    merge(A, n, left, mid, right);
  }
}

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

  int A[MAX], n, i;
  cnt = 0;

  cin >> n;
  for (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;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <climits>
#include <cstdio>
#include <iostream>

using namespace std;

int cnt = 0;
int Arr[500010];
int L[500010];
int R[500010];

void merge(int A[], int l, int m, int r) {
  int n1 = m - l;
  int n2 = r - m;
  for (int i = 0; i < n1; i++)
    L[i] = A[l + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[m + i];
  L[n1] = INT_MAX;
  R[n2] = INT_MAX;

  int a = 0;
  int b = 0;
  for (int i = l; i < r; i++) {
    cnt++;
    if (L[a] <= R[b]) {
      A[i] = L[a];
      a++;
    } else {
      A[i] = R[b];
      b++;
    }
  }
}

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

void printArr(int A[], int n) {
  for (int i = 0; i < n - 1; i++)
    cout << A[i] << " ";
  cout << A[n - 1] << endl;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> Arr[i];
  mergeSort(Arr, 0, n);
  printArr(Arr, n);
  cout << cnt << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <climits>
#include <cstdio>
#include <iostream>

using namespace std;

int cnt = 0;

void merge(int A[], int l, int m, int r) {
  int n1 = m - l;
  int n2 = r - m;
  int *L = new int[n1];
  int *R = new int[n2];
  for (int i = 0; i < n1; i++)
    L[i] = A[l + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[m + i];
  L[n1] = INT_MAX;
  R[n2] = INT_MAX;

  int a = 0;
  int b = 0;
  for (int i = l; i < r; i++) {
    cnt++;
    if (L[a] <= R[b]) {
      A[i] = L[a];
      a++;
    } else {
      A[i] = R[b];
      b++;
    }
  }
  delete L;
  delete R;
}

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

void printArr(int A[], int n) {
  for (int i = 0; i < n - 1; i++)
    cout << A[i] << " ";
  cout << A[n - 1] << endl;
}

int main() {
  int n;
  cin >> n;
  int Arr[500010];
  for (int i = 0; i < n; i++)
    cin >> Arr[i];
  mergeSort(Arr, 0, n);
  printArr(Arr, n);
  cout << cnt << endl;
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <limits>
#define INFINITY 0x7FFFFFFF
void mergeSort(int *A, int left, int right);
using namespace std;
int ct = 0;
int main() {
  int *S;
  int n;
  cin >> n;
  S = new int[n];
  for (int i = 0; i < n; ++i) {
    cin >> S[i];
  }
  /*for (int i = 0; i < n; ++i) {
  cout << S[i];
  cout << " ";
  }
  cout << endl;
  */
  mergeSort(S, 0, n);

  for (int i = 0; i < n - 1; ++i) {
    cout << S[i];
    cout << " ";
  }
  cout << S[n - 1] << endl;
  cout << ct << endl;

  return 0;
}

void merge(int *A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int *L;
  int *R;
  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] = (int)INFINITY;
  R[n2] = (int)INFINITY;
  int i = 0;
  int j = 0;

  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i = i + 1;
      ct++;
    } else {
      A[k] = R[j];
      j = j + 1;
      ct++;
    }
  }
  delete[] L;
  delete[] R;
}

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


Error: 0

Fixed submission:

#include <iostream>
#include <limits>
#define INFINITY 0xFFFFFF
void mergeSort(int *A, int left, int right);
using namespace std;
int ct = 0;
int main() {
  int *S;
  int n;
  cin >> n;
  S = new int[n];
  for (int i = 0; i < n; ++i) {
    cin >> S[i];
  }
  /*for (int i = 0; i < n; ++i) {
  cout << S[i];
  cout << " ";
  }
  cout << endl;
  */
  mergeSort(S, 0, n);

  for (int i = 0; i < n - 1; ++i) {
    cout << S[i];
    cout << " ";
  }
  cout << S[n - 1] << endl;
  cout << ct << endl;

  return 0;
}

void merge(int *A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int *L;
  int *R;
  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] = (int)INFINITY;
  R[n2] = (int)INFINITY;
  int i = 0;
  int j = 0;

  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i = i + 1;
      ct++;
    } else {
      A[k] = R[j];
      j = j + 1;
      ct++;
    }
  }
  delete[] L;
  delete[] R;
}

void mergeSort(int *A, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(A, left, mid);
    mergeSort(A, mid, right);
    merge(A, left, mid, right);
  }
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
#define SENTINEL 2000000000
#define MAX 500000
int cnt;
void out(int a[], int n) {
  int i;
  cout << a[0];
  for (i = 1; i < n; i++) {
    cout << " " << a[i];
  }
  cout << endl;
}

int merge(int a[], int left, int mid, int right) {
  int i, j, n1, n2, k;
  n1 = mid - left;
  n2 = right - mid;
  int L[MAX / 2 + 2], R[MAX / 2 + 2];
  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] = SENTINEL;
  i = 0;
  j = 0;
  for (k = left; k < right; k++) {
    cnt++;
    if (L[i] <= R[j]) {
      a[k] = L[i++];

    } else {
      a[k] = R[j++];
    }
  }
  // out(a, sizeof(a));
  return 0;
}
int 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);
  }
  return 0;
}

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

  mergeSort(a, 0, n);
  out(a, n);
  cout << cnt << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
#define SENTINEL 20000000;
int cnt;
void out(int a[], int n) {
  int i;
  cout << a[0];
  for (i = 1; i < n; i++) {
    cout << " " << a[i];
  }
  cout << endl;
}

int merge(int a[], int left, int mid, int right) {
  int i, j, n1, n2, k;
  n1 = mid - left;
  n2 = right - mid;
  int L[5000], R[5000];
  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] = SENTINEL;
  i = 0;
  j = 0;
  for (k = left; k < right; k++) {
    cnt++;
    if (L[i] <= R[j]) {
      a[k] = L[i++];

    } else {
      a[k] = R[j++];
    }
  }
  // out(a, sizeof(a));
  return 0;
}
int 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);
  }
  return 0;
}

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

  mergeSort(a, 0, n);
  out(a, n);
  cout << cnt << endl;
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

const int INF = 1 << 30;

template <class RandomIt> size_t msort(RandomIt first, RandomIt last) {
  if (first + 1 >= last)
    return 0;

  using T = typename RandomIt::value_type;
  ptrdiff_t lhalf = (last - first) >> 1;
  RandomIt mid = first + lhalf;

  size_t count_ = 0;
  count_ += msort(first, mid);
  count_ += msort(mid, last);

  ptrdiff_t rhalf = last - mid;
  vector<T> L(lhalf + 1), R(rhalf + 1);

  for (RandomIt in = first, out = L.begin(); in < mid; (++in, ++out))
    *out = *in;

  for (RandomIt in = mid, out = R.begin(); in < last; (++in, ++out))
    *out = *in;

  L[lhalf] = R[rhalf] = INF;

  size_t i = 0, j = 0;
  for (RandomIt it = first; it < last; ++it) {
    if (L[i] <= R[j]) {
      *it = L[i];
      ++i;
    } else {
      *it = R[j];
      ++j;
    }
    ++count_;
  }

  return count_;
}

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

  vector<int> S(n);
  for (size_t i = 0; i < n; ++i)
    scanf("%d", &S[i]);

  size_t t = msort(S.begin(), S.end());
  for (size_t i = 0; i < n; ++i)
    printf("%d%c", S[i], i < n - 1 ? ' ' : '\\n');

  printf("%zu\\n", t);
  return 0;
}

Error: -6
malloc(): corrupted top size


Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

const int INF = 1 << 30;

template <class RandomIt> size_t msort(RandomIt first, RandomIt last) {
  if (first + 1 >= last)
    return 0;

  using T = typename RandomIt::value_type;
  ptrdiff_t lhalf = (last - first) >> 1;
  RandomIt mid = first + lhalf;

  size_t count_ = 0;
  count_ += msort(first, mid);
  count_ += msort(mid, last);

  ptrdiff_t rhalf = last - mid;
  vector<T> L(lhalf + 1), R(rhalf + 1);

  for (RandomIt in = first, out = L.begin(); in < mid; (++in, ++out))
    *out = *in;

  for (RandomIt in = first, out = R.begin(); in < last; (++in, ++out))
    *out = *in;

  L[lhalf] = R[rhalf] = INF;

  size_t i = 0, j = 0;
  for (RandomIt it = first; it < last; ++it) {
    if (L[i] <= R[j]) {
      *it = L[i];
      ++i;
    } else {
      *it = R[j];
      ++j;
    }
    ++count_;
  }

  return count_;
}

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

  vector<int> S(n);
  for (size_t i = 0; i < n; ++i)
    scanf("%d", &S[i]);

  size_t t = msort(S.begin(), S.end());
  for (size_t i = 0; i < n; ++i)
    printf("%d%c", S[i], i < n - 1 ? ' ' : '\\n');

  printf("%zu\\n", t);
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

#define SENTINEL 1000000001

int count = 0;

void trace(int a[], int size) {
  for (int i = 0; i < size; ++i) {
    if (i > 0) {
      printf(" ");
    }
    printf("%d", a[i]);
  }
  printf("\\n");
}

void Merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int L[n1], R[n2];
  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] = SENTINEL;
  R[n2] = SENTINEL;
  int m = 0;
  int n = 0;
  for (int i = left; i < right; ++i) {
    if (L[m] <= R[n]) {
      A[i] = L[m];
      ++m;
    } else {
      A[i] = R[n];
      ++n;
    }
    count++;
  }
}

void MergeSort(int A[], int left, int right) {
  if (left >= right - 1)
    return;
  int mid = (left + right) / 2;
  MergeSort(A, left, mid);
  MergeSort(A, mid, right);
  Merge(A, left, mid, right);
}

int main() {
  int N;
  int A[500000];

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

  MergeSort(A, 0, N);
  trace(A, N);
  printf("%d\\n", count);
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

#define SENTINEL 1000000001

int count = 0;

void trace(int a[], int size) {
  for (int i = 0; i < size; ++i) {
    if (i > 0) {
      printf(" ");
    }
    printf("%d", a[i]);
  }
  printf("\\n");
}

void Merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int L[n1], R[n2];
  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] = SENTINEL;
  R[n2] = SENTINEL;
  int m = 0;
  int n = 0;
  for (int i = left; i < right; ++i) {
    if (L[m] <= R[n]) {
      A[i] = L[m];
      ++m;
    } else {
      A[i] = R[n];
      ++n;
    }
    count++;
  }
}

void MergeSort(int A[], int left, int right) {
  if (left >= right - 1)
    return;
  int mid = (left + right) / 2;
  MergeSort(A, left, mid);
  MergeSort(A, mid, right);
  Merge(A, left, mid, right);
}

int main() {
  int N;
  int A[100];

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

  MergeSort(A, 0, N);
  trace(A, N);
  printf("%d\\n", count);
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

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

const long NUM = 500000;
long n, S[NUM], c = 0;

void merge(long A[], long left, long mid, long right) {
  long L[NUM / 2 + 2], R[NUM / 2 + 2], n1 = mid - left, 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] = numeric_limits<long>::max();

  long i = 0, j = 0;

  for (long k = left; k < right; k++) {
    if (L[i] <= R[j])
      A[k] = L[i++];
    else
      A[k] = R[j++];
    c++;
  }
}

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

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

  mergeSort(S, 0, n);

  for (long i = 0; i < n; i++) {
    if (i)
      cout << ' ';
    cout << S[i];
  }
  cout << endl;
  cout << c << endl;
  return 0;
}

Error: 0

Fixed submission:

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

const long NUM = 500000;
long n, S[NUM], c = 0;

void merge(long A[], long left, long mid, long right) {
  long L[NUM / 2], R[NUM / 2], n1 = mid - left, 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] = numeric_limits<long>::max();

  long i = 0, j = 0;

  for (long k = left; k < right; k++) {
    if (L[i] <= R[j])
      A[k] = L[i++];
    else
      A[k] = R[j++];
    c++;
  }
}

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

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

  mergeSort(S, 0, n);

  for (long i = 0; i < n; i++) {
    if (i)
      cout << ' ';
    cout << S[i];
  }
  cout << endl;
  cout << c << endl;

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

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>

using namespace std;
#define MAX 500000
#define SENTI 2000000000
int cnt;

void merge(vector<int> &S, int left, int mid, int right) {

  int i = 0;
  int j = 0;
  int n1 = mid - left;
  int n2 = right - mid;
  vector<int> R;
  vector<int> L;

  for (int i = 0; i < n1; i++) {
    L.push_back(S[left + i]);
  }
  for (int i = 0; i < n2; i++) {
    R.push_back(S[mid + i]);
  }

  L.push_back(SENTI);
  R.push_back(SENTI);

  for (int k = left; k < right; k++) {
    cnt++;
    if (L[i] <= R[j]) {
      S[k] = L[i];
      i++;
    } else {
      S[k] = R[j];
      j++;
    }
  }
  // for (int i = 0; i<S.size(); i++) {
  // cout << S[i]<<" ";
  //}
  // cout << endl;
}

void mergesort(vector<int> &S, int left, int right) {
  int mid = 0;
  if (left + 1 < right) {
    mid = (right + left) / 2;
    mergesort(S, left, mid);
    mergesort(S, mid, right);
    merge(S, left, mid, right);
  }
}

int main() {

  int n = 0;
  int num = 0;
  cin >> n;
  vector<int> S;

  for (int i = 0; i < n; i++) {
    cin >> num;
    S.push_back(num);
  }

  mergesort(S, 0, n);

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

  cout << cnt;

  cout << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>

using namespace std;
#define MAX 40000
#define SENTI 2000000
int cnt;

void merge(vector<int> S, int left, int mid, int right) {

  int i = 0;
  int j = 0;
  int n1 = mid - left;
  int n2 = right - mid;
  vector<int> R;
  vector<int> L;

  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] = SENTI;
  R[n2] = SENTI;

  for (int k = left; k < right; k++) {
    cnt++;
    if (L[i] <= R[j]) {
      S[k] = L[i];
      i++;
    } else {
      S[k] = R[j];
      j++;
    }
  }
}

void mergesort(vector<int> S, int left, int right) {
  int mid = 0;
  if (left + 1 < right) {
    mid = (right + left) / 2;
    mergesort(S, left, mid);
    mergesort(S, mid, right);
    merge(S, left, mid, right);
  }
}

int main() {

  int n = 0;
  int num = 0;
  cin >> n;
  vector<int> S;

  for (int i = 0; i < n; i++) {
    cin >> num;
    S.push_back(num);
  }

  mergesort(S, 0, n);

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

  cout << cnt;
  return 0;
}