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

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

int n, a[500105], m;
long long inf = 2e9;
void Merge(int left, int mid, int rigth) {
  int n1 = mid - left, n2 = rigth - 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] = inf;
  r[n2] = inf;
  int x = 0, y = 0;
  for (int i = left; i < rigth; i++) {
    if (l[x] > r[y]) {
      a[i] = r[y];
      y++;
    } else {
      a[i] = l[x];
      x++;
    }

    m++;
  }
}

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++) {
    cout << a[i];
    if (i != n - 1)
      printf(" ");
  }
  cout << endl;
  cout << m << endl;
}

/*

*/


Error: 0

Fixed submission:

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

int n, a[50105], m;
long long inf = 2e9;
void Merge(int left, int mid, int rigth) {
  int n1 = mid - left, n2 = rigth - 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] = inf;
  r[n2] = inf;
  int x = 0, y = 0;
  for (int i = left; i < rigth; i++) {
    if (l[x] > r[y]) {
      a[i] = r[y];
      y++;
    } else {
      a[i] = l[x];
      x++;
    }

    m++;
  }
}

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++) {
    cout << a[i];
    if (i != n - 1)
      printf(" ");
  }
  cout << endl;
  cout << m << endl;
}

/*

*/
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() {
  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:

#include <iostream>

using namespace std;

#define MAX 5000
#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;
}
Problem p02272 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define square(x) (x) * (x)
#define cube(x) (x) * (x) * (x)

const int INF = 1e9;

using namespace std;
int c = 0;

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];
  REP(i, n1) l[i] = a[left + i];
  REP(i, n2) r[i] = a[mid + i];
  l[n1] = INF;
  r[n2] = INF;
  int i = 0;
  int j = 0;
  for (int k = left; k < right; k++) {
    c++;
    if (l[i] <= r[j]) {
      a[k] = l[i];
      i++;
    } else {
      a[k] = r[j];
      j++;
    }
  }
  return;
}

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

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

  REP(i, n - 1) cout << a[i] << " ";
  cout << a[n - 1] << endl << c << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define square(x) (x) * (x)
#define cube(x) (x) * (x) * (x)

const int INF = 1e9;

using namespace std;
int c = 0;

vector<int> merge(vector<int> a, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int l[n1 + 1], r[n2 + 1];
  REP(i, n1) l[i] = a[left + i];
  REP(i, n2) r[i] = a[mid + i];
  l[n1] = INF;
  r[n2] = INF;
  int i = 0;
  int j = 0;
  for (int k = left; k < right; k++) {
    c++;
    if (l[i] <= r[j]) {
      a[k] = l[i];
      i++;
    } else {
      a[k] = r[j];
      j++;
    }
  }
  return a;
}

vector<int> merge_sort(vector<int> a, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    a = merge_sort(a, left, mid);
    a = merge_sort(a, mid, right);
    a = merge(a, left, mid, right);
  }
  return a;
}

int main() {
  int n;
  cin >> n;
  vector<int> a;
  REP(i, n) {
    int x;
    cin >> x;
    a.push_back(x);
  }
  a = merge_sort(a, 0, n);

  REP(i, n - 1) cout << a[i] << " ";
  cout << a[n - 1] << endl << c << endl;

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

#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

int counter = 0;

void Merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int L[500000], R[500000];
  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++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else {
      A[k] = R[j];
      j++;
    }
    counter++;
  }
}

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

int main() {
  int S[500000];
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }
  Merge_sort(S, 0, n);
  for (int i = 0; i < n; i++) {
    printf("%d", S[i]);
    if (i < (n - 1)) {
      printf(" ");
    }
  }
  printf("\\n");
  printf("%d\\n", counter);
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

int counter = 0;

void Merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int L[500], R[500];
  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++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else {
      A[k] = R[j];
      j++;
    }
    counter++;
  }
}

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

int main() {
  int S[500];
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &S[i]);
  }
  Merge_sort(S, 0, n);
  for (int i = 0; i < n; i++) {
    printf("%d", S[i]);
    if (i < (n - 1)) {
      printf(" ");
    }
  }
  printf("\\n");
  printf("%d\\n", counter);
  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];
  }
  int i = 0, j = 0;
  L[n1] = R[n2] = Sentinel;
  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 = (right + left) / 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:

#include <iostream>
using namespace std;
#define MAX 500000
#define Sentinel 19970328

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];
  }
  int i = 0, j = 0;
  L[n1] = R[n2] = Sentinel;
  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 = (right + left) / 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>

using namespace std;

int num_comp = 0;
const int MAX = 500000;
int L[MAX / 2 + 2];
int R[MAX / 2 + 2];
const int SENTINEL = 2000000000;

// Merge
void Merge(int A[], 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++) {
    num_comp++;
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else {
      A[k] = R[j];
      j++;
    }
  }
}

// Merge sort
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);
  }
}

// MAIN FUNCTION
int main(int argc, char **argv) {
  // Input
  int A[MAX];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];

  // Merge sort
  Mergesort(A, 0, n);

  // Print all elements
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
  cout << num_comp << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int num_comp = 0;
const int MAX = 500000;
int L[MAX / 2 + 2];
int R[MAX / 2 + 2];
const int SENTINEL = 200000000;

// Merge
void Merge(int A[], 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++) {
    num_comp++;
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else {
      A[k] = R[j];
      j++;
    }
  }
}

// Merge sort
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);
  }
}

// MAIN FUNCTION
int main(int argc, char **argv) {
  // Input
  int A[MAX];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];

  // Merge sort
  Mergesort(A, 0, n);

  // Print all elements
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
  cout << num_comp << endl;

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

#include <iostream>
using namespace std;

#define INFINITY 2000000000
#define MAX 500000

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

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

  L[n1] = R[n2] = INFINITY;

  for (int i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }

  for (int i = 0; i < n2; i++) {
    R[i] = A[mid + i];
  }

  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 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 A[MAX], n, l;
  cnt = 0;

  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 << " ";
    }
    cout << A[i];
  }
  cout << endl;

  cout << cnt << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

#define INFINITY 2000000000
#define MAX 50000

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

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

  L[n1] = R[n2] = INFINITY;

  for (int i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }

  for (int i = 0; i < n2; i++) {
    R[i] = A[mid + i];
  }

  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 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 A[MAX], n, l;
  cnt = 0;

  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 << " ";
    }
    cout << A[i];
  }
  cout << endl;

  cout << cnt << endl;

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

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

#define MAX_N 500000
#define INF 1000000001

void print_array(int A[], int n) {
  for (int i = 0; i < n; i++) {
    printf("%d", A[i]);
    if (i != n - 1) {
      printf(" ");
    }
  }
  printf("\\n");
}

int merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int *L = (int *)malloc((n1 + 1) * sizeof(int));
  int *R = (int *)malloc((n2 + 1) * sizeof(int));
  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 li = 0;
  int ri = 0;
  for (int i = left; i < right; i++) {
    if (L[li] <= R[ri]) {
      A[i] = L[li];
      li++;
    } else {
      A[i] = R[ri];
      ri++;
    }
  }

  free(L);
  free(R);
  return li + ri;
}

int mergeSort(int A[], int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    int x1 = mergeSort(A, left, mid);
    int x2 = mergeSort(A, mid, right);
    int x3 = merge(A, left, mid, right);
    return x1 + x2 + x3;
  }
  return 0;
}

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

  int cmpNum = mergeSort(A, 0, n);
  print_array(A, n);
  printf("%d\\n", cmpNum);
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

#define MAX_N 500000
#define INF 1000000001

void print_array(int A[], int n) {
  for (int i = 0; i < n; i++) {
    printf("%d", A[i]);
    if (i != n - 1) {
      printf(" ");
    }
  }
  printf("\\n");
}

int merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int *L = (int *)malloc(n1 * sizeof(int));
  int *R = (int *)malloc(n2 * sizeof(int));
  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 li = 0;
  int ri = 0;
  for (int i = left; i < right; i++) {
    if (L[li] <= R[ri]) {
      A[i] = L[li];
      li++;
    } else {
      A[i] = R[ri];
      ri++;
    }
  }

  free(L);
  free(R);
  return li + ri;
}

int mergeSort(int A[], int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    int x1 = mergeSort(A, left, mid);
    int x2 = mergeSort(A, mid, right);
    int x3 = merge(A, left, mid, right);
    return x1 + x2 + x3;
  }
  return 0;
}

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

  int cmpNum = mergeSort(A, 0, n);
  print_array(A, n);
  printf("%d\\n", cmpNum);
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <stdio.h>
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(void) {
  int n;
  cin >> n;
  int *s = new int[n];
  for (int i = 0; i < n; i++)
    cin >> s[i];

  mergeSort(s, 0, n - 1);

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

  delete[] s;
  return 0;
}

void merge(int a[], int left, int mid, int right) {
  int n1 = mid - left + 1;
  int n2 = right - (mid + 1) + 1;
  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 + 1) + i];
  l[n1] = 1000000000;
  r[n2] = 1000000000;

  int i = 0, 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++;
    }
    cnt++;
  }
  delete[] l, r;
}

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

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <stdio.h>
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(void) {
  int n, s[20];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> s[i];

  mergeSort(s, 0, n - 1);

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

  return 0;
}

void merge(int a[], int left, int mid, int right) {
  int n1 = mid - left + 1;
  int n2 = right - (mid + 1) + 1;
  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 + 1) + i];
  l[n1] = 1000000000;
  r[n2] = 1000000000;

  int i = 0, 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++;
    }
    cnt++;
  }
  delete[] l, r;
}

void mergeSort(int a[], int left, int right) {
  if (left < right) {
    int mid = (left + right) / 2;
    mergeSort(a, left, mid);
    mergeSort(a, mid + 1, right);
    merge(a, left, mid, right);
  }
}
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(int argc, char const *argv[]) {
  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:

#include <iostream>
using namespace std;
#define MAX 50000
#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 argc, char const *argv[]) {
  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;
}