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

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1LL << 55LL;
int cnt;
void merge(int *A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int L[n1 + 1], R[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] = INF;
  R[n2] = INF;
  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 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);
  }
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  int S[n];
  for (int i = 0; i < n; i++)
    cin >> S[i];
  cnt = 0;
  mergeSort(S, 0, n);
  for (int i = 0; i < n; i++)
    cout << S[i] << " \\n"[i == n - 1];
  cout << cnt << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define Max 50000
#define SENTINEL 200000000;

int L[Max / 2 + 2], R[Max / 2 + 2];
int cnt = 0;

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

  int i = 0;
  int j = 0;
  for (int k = left; k < right; k++) {
    cnt++;
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else {
      A[k] = R[j];
      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];
  int n, i;
  cin >> n;
  for (int 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>
#include <vector>
using namespace std;

int c = 0;

void merge(int a[], int l, int m, int r) {
  int n1 = m - l;
  int n2 = r - m;
  vector<int> L(n1 + 1), R(n2 + 1);

  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] = 1000000001;
  R[n2] = 1000000002;
  int i = 0, j = 0;

  for (int k = l; k < r; k++) {
    if (L[i] <= R[j]) {
      a[k] = L[i];
      i++;
    } else {
      a[k] = R[j];
      j++;
    }
    c++;
  }
}

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);
  }
}

int main() {
  int n = 0, a[500000] = {0};

  cin >> 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;
  cout << c << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

long n = 0, a[50000] = {0}, c = 0;

void merge(long a[50000], long l, long m, long r) {
  long n1 = m - l;
  long n2 = r - m;
  long L[25000] = {0}, R[25000] = {0};

  for (long i = 0; i < n1; i++)
    L[i] = a[l + i];

  for (long i = 0; i < n2; i++)
    R[i] = a[m + i];

  L[n1] = 1000000001;
  R[n2] = 1000000002;
  long i = 0, j = 0;

  for (long k = l; k < r; k++) {
    if (L[i] <= R[j]) {
      a[k] = L[i];
      i++;
    } else {
      a[k] = R[j];
      j++;
    }
    c++;
  }
}

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

int main() {

  cin >> n;

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

  mergeSort(a, 0, n);

  for (long i = 0; i < n - 1; i++)
    cout << a[i] << " ";

  cout << a[n - 1] << endl;
  cout << c << endl;

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

#include <iostream>
#define rep(i, o, u) for (int i = o; i < u; i++)
using namespace std;
int L[500000 / 2 + 2], R[500000 / 2 + 2];
int ans = 0;
void merge(int a[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  rep(i, 0, n1) L[i] = a[left + i];
  rep(i, 0, n2) R[i] = a[mid + i];
  L[n1] = R[n2] = 2000000000;
  int i = 0, j = 0;
  rep(k, left, right) {
    ans++;
    if (L[i] <= R[j]) {
      a[k] = L[i++];
    } else {
      a[k] = R[j++];
    }
  }
}
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);
  }
}
int main() {
  int n;
  cin >> n;
  int a[500001];
  rep(i, 0, n) cin >> a[i];
  mergeSort(a, 0, n);
  rep(i, 0, n - 1) { cout << a[i] << " "; }
  cout << a[n - 1] << endl;
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#define rep(i, o, u) for (int i = o; i < u; i++)
using namespace std;
int L[50000 / 2 + 2], R[50000 / 2 + 2];
int ans = 0;
void merge(int a[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  rep(i, 0, n1) L[i] = a[left + i];
  rep(i, 0, n2) R[i] = a[mid + i];
  L[n1] = R[n2] = 2000000000;
  int i = 0, j = 0;
  rep(k, left, right) {
    ans++;
    if (L[i] <= R[j]) {
      a[k] = L[i++];
    } else {
      a[k] = R[j++];
    }
  }
}
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);
  }
}
int main() {
  int n;
  cin >> n;
  int a[50001];
  rep(i, 0, n) cin >> a[i];
  mergeSort(a, 0, n);
  rep(i, 0, n - 1) { cout << a[i] << " "; }
  cout << a[n - 1] << endl;
  cout << ans << endl;
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

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

int A[500000], S = 0;

int merge(int l, int m, int r) {
  int n1 = m - l, n2 = r - m, i, j;
  int L[n1 + 1], R[n2 + 1];
  for (i = 0; i < n1; i++)
    L[i] = A[i + l];
  for (i = 0; i < n2; i++)
    R[i] = A[i + m];
  i = 0;
  j = 0;
  // printf("%d %d : \\n", L[0], R[0]);
  for (int k = 0; k < r - l; k++) {
    if (j == n2 || (i < n1 && L[i] <= R[j]))
      A[k + l] = L[i++];
    else
      A[k + l] = R[j++];
    S++;
  }
  // for(int i = 0;i < 4;i++)printf(" %d", A[i]);
  // puts("");
}

int 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() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  mergeSort(0, n);
  for (int i = 0; i < n; i++) {
    if (i)
      printf(" ");
    printf("%d", A[i]);
  }
  printf("\\n%d\\n", S);

  return 0;
}

Error: 0

Fixed submission:

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

int A[5000], S = 0;

int merge(int l, int m, int r) {
  int n1 = m - l, n2 = r - m, i, j;
  int L[n1 + 1], R[n2 + 1];
  for (i = 0; i < n1; i++)
    L[i] = A[i + l];
  for (i = 0; i < n2; i++)
    R[i] = A[i + m];
  i = 0;
  j = 0;
  // printf("%d %d : \\n", L[0], R[0]);
  for (int k = 0; k < r - l; k++) {
    if (j == n2 || (i < n1 && L[i] <= R[j]))
      A[k + l] = L[i++];
    else
      A[k + l] = R[j++];
    S++;
  }
  // for(int i = 0;i < 4;i++)printf(" %d", A[i]);
  // puts("");
}

int 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() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  mergeSort(0, n);
  for (int i = 0; i < n; i++) {
    if (i)
      printf(" ");
    printf("%d", A[i]);
  }
  printf("\\n%d\\n", S);

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

#include <iostream>
using namespace std;
#define INFTY 2000000000
int count = 0;
void merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int *L = new int[n1 + 1];
  int *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];
      i++;
    } else {
      A[k] = R[j];
      j++;
    }
    count++;
  }
  delete[] L;
  delete[] R;
}
void mergeSort(int A[], int left, int right) {
  if (left + 1 >= right)
    return;
  int mid = (left + right) / 2;
  mergeSort(A, left, mid);
  mergeSort(A, mid, right);
  merge(A, left, mid, right);
}

int main() {
  int n;
  cin >> n;
  int A[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;
  cout << count << endl;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define INFTY 2000000000
int count = 0;
void merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int *L = new int[n1 + 1];
  int *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];
      i++;
    } else {
      A[k] = R[j];
      j++;
    }
    count++;
  }
  delete[] L;
  delete[] R;
}
void mergeSort(int A[], int left, int right) {
  if (left + 1 >= right)
    return;
  int mid = (left + right) / 2;
  mergeSort(A, left, mid);
  mergeSort(A, mid, right);
  merge(A, left, mid, right);
}

int main() {
  int n;
  cin >> n;
  int A[10];
  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;
  cout << count << endl;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#define PI 3.14159265359
#define INF 2000000000
#define MAX 500000
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

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

void merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  //?????????2??????????????????????????¨??????????????§????????±??????
  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] = INF;
  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 n, A[MAX + 1], i;
  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 << cnt << endl;

  return 0;
}

Error: 0

Fixed submission:

#define PI 3.14159265359
#define INF 200000000
#define MAX 500000
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

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

void merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  //?????????2??????????????????????????¨??????????????§????????±??????
  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] = INF;
  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 n, A[MAX + 1], i;
  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 << cnt << endl;

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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

int t = 0;

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 - 1; i++)
    L[i] = A[left + i];
  for (int i = 0; i <= n2 - 1; i++)
    R[i] = A[mid + i];
  L[n1] = 1000000000;
  R[n2] = 1000000000;
  int i = 0;
  int j = 0;
  for (int k = left; k <= right - 1; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i = i + 1;
      t++;
    } else {
      A[k] = R[j];
      j = j + 1;
      t++;
    }
  }
}

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);
  }
}

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

int t = 0;

void Merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int L[25000], R[25000];
  for (int i = 0; i <= n1 - 1; i++)
    L[i] = A[left + i];
  for (int i = 0; i <= n2 - 1; i++)
    R[i] = A[mid + i];
  L[n1] = 100000001;
  R[n2] = 100000001;
  int i = 0;
  int j = 0;
  for (int k = left; k <= right - 1; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i = i + 1;
      t++;
    } else {
      A[k] = R[j];
      j = j + 1;
      t++;
    }
  }
}

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);
  }
}

int main() {
  int n, a[500000];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  MergeSort(a, 0, n);
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " " << a[i];
    else
      cout << a[i];
  }
  cout << endl << t << endl;
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>

using namespace std;

int n;
int a[500005];
int count = 0;

void merge(int l, int mid, int r) {
  int n1 = mid - l;
  int n2 = r - mid;
  int *lf = new int[n1];
  int *rt = new int[n2];
  for (int i = 0; i < n1; i++) {
    lf[i] = a[l + i];
  }
  for (int i = 0; i < n2; i++) {
    rt[i] = a[mid + i];
  }
  int i = 0;
  int j = 0;
  for (int k = l; k < r; k++) {
    count++;
    if ((lf[i] <= rt[j] && i != n1) || (j == n2)) {
      a[k] = lf[i];
      i++;
    } else {
      a[k] = rt[j];
      j++;
    }
  }
  delete (lf);
  delete (rt);
}

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

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 << count << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>

using namespace std;

int n;
int a[500005];
int count = 0;

void merge(int l, int mid, int r) {
  int n1 = mid - l;
  int n2 = r - mid;
  int *lf = new int[n1];
  int *rt = new int[n2];
  for (int i = 0; i < n1; i++) {
    lf[i] = a[l + i];
  }
  for (int i = 0; i < n2; i++) {
    rt[i] = a[mid + i];
  }
  int i = 0;
  int j = 0;
  for (int k = l; k < r; k++) {
    count++;
    if ((lf[i] <= rt[j] && i != n1) || (j == n2)) {
      a[k] = lf[i];
      i++;
    } else {
      a[k] = rt[j];
      j++;
    }
  }
}

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

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 << count << endl;
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define INFTY 1001001001

int S[500100] = {0};
int n, cnt = 0;

void merge(int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;

  int *L = new int[n1 + 1], *R = new int[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] = INFTY;
  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;
    } else {
      S[k] = R[j];
      ++j;
    }
    ++cnt;
  }
  delete[] L, R;
}

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 >> S[i];
  }

  mergeSort(0, n);
  for (int i = 0; i < n; ++i) {
    cout << S[i];
    if (i != n - 1) {
      cout << " ";
    } else {
      cout << "\\n";
    }
  }
  cout << cnt << "\\n";
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define INFTY 100100100

int S[500100] = {0};
int n, cnt = 0;

void merge(int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;

  int *L = new int[n1 + 1], *R = new int[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] = INFTY;
  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;
    } else {
      S[k] = R[j];
      ++j;
    }
    ++cnt;
  }
  delete[] L, R;
}

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 >> S[i];
  }

  mergeSort(0, n);
  for (int i = 0; i < n; ++i) {
    cout << S[i];
    if (i != n - 1) {
      cout << " ";
    } else {
      cout << "\\n";
    }
  }
  cout << cnt << "\\n";
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#define scanf_s scanf
// #include <string>
#include <stdio.h>
// #include <math.h>
// #include <stack>
// #include <queue>
// #include <list>
#include <iostream>
#include <vector>
using namespace std;
#define MAX 10000000000
int cou = 0;
void merge(int A[], int left, int mid, int right) {
  int i = 0, j = 0;
  int n1 = mid - left;
  int n2 = right - mid;
  vector<int> L, R;
  for (i = 0; i < n1; i++) {
    L.push_back(A[left + i]);
  }
  for (i = 0; i < n2; i++) {
    R.push_back(A[mid + i]);
  }
  L.push_back(MAX);
  R.push_back(MAX);
  i = 0;
  j = 0;
  for (int k = left; k < right; k++) {
    if (L.at(i) <= R.at(j)) {
      A[k] = L.at(i);
      i++;
    } else {
      A[k] = R.at(j);
      j++;
    }
    cou++;
  }
}
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);
  }
}
int main(void) {
  int n, S[500000];
  scanf_s("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf_s("%d", &S[i]);
  }
  mergeSort(S, 0, n);
  for (int i = 0; i < n; i++) {
    if (i != 0) {
      printf(" ");
    }
    printf("%d", S[i]);
  }
  printf("\\n%d\\n", cou);
}

Error: 0

Fixed submission:

#define scanf_s scanf
// #include <string>
#include <stdio.h>
// #include <math.h>
// #include <stack>
// #include <queue>
// #include <list>
#include <iostream>
#include <vector>
using namespace std;
#define MAX 100
int cou = 0;
void merge(int A[], int left, int mid, int right) {
  int i = 0, j = 0;
  int n1 = mid - left;
  int n2 = right - mid;
  vector<int> L, R;
  for (i = 0; i < n1; i++) {
    L.push_back(A[left + i]);
  }
  for (i = 0; i < n2; i++) {
    R.push_back(A[mid + i]);
  }
  L.push_back(MAX);
  R.push_back(MAX);
  i = 0;
  j = 0;
  for (int k = left; k < right; k++) {
    if (L.at(i) <= R.at(j)) {
      A[k] = L.at(i);
      i++;
    } else {
      A[k] = R.at(j);
      j++;
    }
    cou++;
  }
}
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);
  }
}
int main(void) {
  int n, S[500];
  scanf_s("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf_s("%d", &S[i]);
  }
  mergeSort(S, 0, n);
  for (int i = 0; i < n; i++) {
    if (i != 0) {
      printf(" ");
    }
    printf("%d", S[i]);
  }
  printf("\\n%d\\n", cou);
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

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

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

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

  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);
  }
}

int main() {
  int n, *S;
  cin >> n;
  S = new int[n];
  for (int i = 0; i < n; i++)
    cin >> S[i];
  mergeSort(S, 0, n);

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

  delete[] S;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

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

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

  delete[] L;
  delete[] R;
}

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

int main() {
  int n, *S;
  int counter = 0;
  cin >> n;
  S = new int[n];
  for (int i = 0; i < n; i++)
    cin >> S[i];
  mergeSort(S, 0, n, &counter);

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

  delete[] S;
  return 0;
}