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

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;

#define FOR(k, m, n) for (int(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define LL long long

#define MAX 600000
#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;
  //?????????????????????LR?????\\??????
  REP(i, n1) L[i] = A[left + i];
  REP(i, n2) R[i] = A[mid + i];
  L[n1] = R[n2] = SENTINEL;
  int i = 0, j = 0;

  //??????????????????????°?????????????A???????????¶????????????
  FOR(k, left, right) {
    cnt++;
    L[i] <= R[j] ? A[k] = L[i++] : A[k] = R[j++];
  }
}

void mergeSort(int A[], int n, int left, int right) {
  //????´???°???1??????????????§???????????????????????°
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    //?????°???????????????????????????????????????
    mergeSort(A, n, left, mid);
    mergeSort(A, n, mid, right);
    //??????????????????2?????????????????????
    merge(A, n, left, mid, right);
  }
}

int main() {
  int A[MAX], 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 << cnt << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;

#define FOR(k, m, n) for (int(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define LL long long

#define MAX 600000
#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;
  //?????????????????????LR?????\\??????
  REP(i, n1) L[i] = A[left + i];
  REP(i, n2) R[i] = A[mid + i];
  L[n1] = R[n2] = SENTINEL;
  int i = 0, j = 0;

  //??????????????????????°?????????????A???????????¶????????????
  FOR(k, left, right) {
    cnt++;
    L[i] <= R[j] ? A[k] = L[i++] : A[k] = R[j++];
  }
}

void mergeSort(int A[], int n, int left, int right) {
  //????´???°???1??????????????§???????????????????????°
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    //?????°???????????????????????????????????????
    mergeSort(A, n, left, mid);
    mergeSort(A, n, mid, right);
    //??????????????????2?????????????????????
    merge(A, n, left, mid, right);
  }
}

int main() {
  int A[MAX], 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 << cnt << endl;

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

#include <algorithm>
#include <limits.h>
#include <stdio.h>

using namespace std;

int cmp_count = 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];
  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] = INT_MAX;

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

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;
  scanf("%d", &n);
  int S[n];
  for (int i = 0; i < n; i++)
    scanf("%d", &S[i]);

  mergeSort(S, 0, n);

  for (int i = 0; i < n - 1; i++)
    printf("%d ", S[i]);
  printf("%d\\n", S[n - 1]);
  printf("%d\\n", cmp_count);
}


Error: 0

Fixed submission:

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

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

void Merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  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] = 200000005;
  R[n2] = 200000005;
  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[500000], n, i;
  cnt = 0;
  scanf("%d", &n);
  for (i = 0; i < n; ++i) {
    scanf("%d", &A[i]);
  }
  mergesort(A, n, 0, n);
  for (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>
using namespace std;

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];
  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] = R[n2] = INT_MAX;
  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);
  }
}

int main() {
  int A[500000], n, i;
  cnt = 0;
  scanf("%d", &n);
  for (i = 0; i < n; ++i) {
    scanf("%d", &A[i]);
  }
  mergesort(A, 0, n);
  for (i = 0; i < n; ++i) {
    if (i)
      printf(" ");
    printf("%d", A[i]);
  }
  printf("\\n");

  printf("%d\\n", cnt);

  return 0;
}


Error: 0

Fixed submission:

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

int cnt;

void Merge(int A[], int n, int left, int mid, int right) {
  int L[200005], R[200005];
  int n1 = mid - left;
  int n2 = right - mid;
  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] = 200000005;
  R[n2] = 200000005;
  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[500000], n, i;
  cnt = 0;
  scanf("%d", &n);
  for (i = 0; i < n; ++i) {
    scanf("%d", &A[i]);
  }
  mergesort(A, n, 0, n);
  for (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 <stdio.h>
#define INF 999999999

int l[250002] = {};
int r[250002] = {};
int c;

void merge(int *s, int n, int left, int mid, int right) {

  int i, j, k;
  int n1 = mid - left;
  int n2 = right - mid;

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

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

  i = j = 0;

  for (k = left; k < right; k++) {
    c++;
    if (l[i] <= r[j]) {
      s[k] = l[i];
      i++;
    } else {
      s[k] = r[j];
      j++;
    }
  }
}

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

int main() {

  int n, i, left, right;
  int s[500000];
  c = 0;

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

  left = 0;
  right = n;

  mergesort(s, n, left, right);

  for (i = 0; i < n; i++) {
    if (i != n - 1)
      printf("%d ", s[i]);
    else
      printf("%d\\n", s[i]);
  }
  printf("%d\\n", c);
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#define INF 999999999

int l[25002] = {};
int r[25002] = {};
int c;

void merge(int *s, int n, int left, int mid, int right) {

  int i, j, k;
  int n1 = mid - left;
  int n2 = right - mid;

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

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

  i = j = 0;

  for (k = left; k < right; k++) {
    c++;
    if (l[i] <= r[j]) {
      s[k] = l[i];
      i++;
    } else {
      s[k] = r[j];
      j++;
    }
  }
}

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

int main() {

  int n, i, left, right;
  int s[500000];
  c = 0;

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

  left = 0;
  right = n;

  mergesort(s, n, left, right);

  for (i = 0; i < n; i++) {
    if (i != n - 1)
      printf("%d ", s[i]);
    else
      printf("%d\\n", s[i]);
  }
  printf("%d\\n", c);
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = int(m); i < (int)(n); i++)

using namespace std;

int num = 0;

void merge(vector<int> &A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  vector<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] = 1001001001;
  R[n2] = 1001001001;

  int i = 0;
  int j = 0;

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

void merge_sort(vector<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 n;
  cin >> n;

  vector<int> A(n);
  rep(i, n) cin >> A[i];

  merge_sort(A, 0, n);

  cout << A[0];
  for (int i = 1; i < n; i++)
    cout << ' ' << A[i];
  cout << endl;

  cout << num << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = int(m); i < (int)(n); i++)

using namespace std;

int num = 0;

void merge(vector<int> &A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  vector<int> L(n1), R(n2);

  rep(i, n1) { L[i] = A[left + i]; }
  rep(i, n2) { R[i] = A[mid + i]; }

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

  int i = 0;
  int j = 0;

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

void merge_sort(vector<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 n;
  cin >> n;

  vector<int> A(n);
  rep(i, n) cin >> A[i];

  merge_sort(A, 0, n);

  cout << A[0];
  for (int i = 1; i < n; i++)
    cout << ' ' << A[i];
  cout << endl;

  cout << num << endl;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <stdlib.h>

#define INFTY 1000000001

int comp = 0;
int *L, *R;

void merge(int *A, const int left, const int mid, const 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] = 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++;
      comp++;
    } else {
      A[k] = R[j];
      j++;
      comp++;
    }
  }
}

void merge_sort(int *A, const int left, const 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(void) {

  int n;

  scanf("%d", &n);
  int *A = new int[n];
  L = new int[n];
  R = new int[n];

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

  merge_sort(A, 0, n);

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


Error: 0

Fixed submission:

#include <stdio.h>
#include <stdlib.h>

#define INFTY 500001

int comp = 0;

void merge(int *A, const int left, const int mid, const 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++;
      comp++;
    } else {
      A[k] = R[j];
      j++;
      comp++;
    }
  }
}

void merge_sort(int *A, const int left, const 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(void) {

  int n;

  scanf("%d", &n);
  int *A = new int[n];

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

  merge_sort(A, 0, n);

  for (int i = 0; i < n; i++) {
    printf("%d", A[i]);
    if (i == n - 1)
      printf("\\n");
    else
      printf(" ");
  }
  printf("%d\\n", comp);
}
Problem p02272 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <limits.h>
#include <vector>

using namespace std;

void merge(vector<int> &A, int left, int mid, int right, int &c) {
  int n1 = mid - left;
  int n2 = right - mid;
  vector<int> L(n1 + 1, 0);
  vector<int> R(n2 + 1, 0);
  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_MAX;
  R[n2] = INT_MAX;
  int i = 0;
  int j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i++];
      c++;
    } else {
      A[k] = R[j++];
      c++;
    }
  }
}
void mergeSort(vector<int> &A, int left, int right, int &c) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(A, left, mid, c);
    mergeSort(A, mid, right, c);
    merge(A, left, mid, right, c);
  }
}

int main() {

  int count = 0;
  int n = 0;
  cin >> n;
  vector<int> array(n, 0);
  for (int i = 0; i < n; i++)
    cin >> array[i];

  mergeSort(array, 0, array.size(), count);

  for (int i = 0; i < array.size(); i++) {
    if (i != array.size() - 1) {
      cout << array[i] << ' ';
    } else
      cout << array[i] << endl;
  }
  cout << count << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <limits.h>
#include <vector>

using namespace std;

void merge(vector<int> &A, int left, int mid, int right, int &c) {
  int n1 = mid - left;
  int n2 = right - mid;
  vector<int> L(n1 + 1, 0);
  vector<int> R(n2 + 1, 0);
  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_MAX;
  R[n2] = INT_MAX;
  int i = 0;
  int j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i++];
      c++;
    } else {
      A[k] = R[j++];
      c++;
    }
  }
}
void mergeSort(vector<int> &A, int left, int right, int &c) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(A, left, mid, c);
    mergeSort(A, mid, right, c);
    merge(A, left, mid, right, c);
  }
}

int main() {

  int count = 0;
  int n = 0;
  cin >> n;
  vector<int> array(n, 0);
  for (int i = 0; i < n; i++)
    cin >> array[i];

  mergeSort(array, 0, array.size(), count);

  for (int n : array)
    cout << n << ' ';
  cout << endl;
  cout << count << endl;

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

#include <algorithm>
#include <functional>
#include <iostream>
#include <limits>
#include <vector>

using namespace std;

int main() {
  int n;
  vector<int> v;

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

  auto merge = [&v](int left, int mid, int right) {
    int count = 0;
    int n1 = mid - left;
    int n2 = right - mid;
    vector<int> l(n1 + 1);
    vector<int> r(n2 + 1);
    for (int i = 0; i < n1; ++i) {
      l[i] = v[left + i];
    }
    for (int i = 0; i < n2; ++i) {
      r[i] = v[mid + i];
    }
    l[n1] = numeric_limits<int>::max();
    r[n2] = numeric_limits<int>::max();

    int i = 0;
    int j = 0;
    for (int k = left; k < right; ++k) {
      count += 1;
      if (l[i] <= r[j]) {
        v[k] = l[i];
        i += 1;
      } else {
        v[k] = r[j];
        j += 1;
      }
    }
    return count;
  };

  function<int(int, int)> mergeSort = [&merge, &mergeSort](int left,
                                                           int right) {
    int count = 0;
    if (left + 1 < right) {
      int mid = (left + right) / 2;
      count += mergeSort(left, mid);
      count += mergeSort(mid, right);
      count += merge(left, mid, right);
    }
    return count;
  };

  auto r = mergeSort(0, n);
  for (int i = 0; i < n; ++i) {
    if (i != 0) {
      cout << ' ';
    }
    cout << v[i];
  }
  cout << endl;
  cout << r << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <vector>

using namespace std;

int main() {
  int n;
  vector<int> v;

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

  auto merge = [&v](int left, int mid, int right) {
    int count = 0;
    int n1 = mid - left;
    int n2 = right - mid;
    vector<int> l(n1);
    vector<int> r(n2);
    for (int i = 0; i < n1; ++i) {
      l[i] = v[left + i];
    }
    for (int i = 0; i < n2; ++i) {
      r[i] = v[mid + i];
    }
    l[n1] = numeric_limits<int>::max();
    r[n2] = numeric_limits<int>::max();

    int i = 0;
    int j = 0;
    for (int k = left; k < right; ++k) {
      count += 1;
      if (l[i] <= r[j]) {
        v[k] = l[i];
        i += 1;
      } else {
        v[k] = r[j];
        j += 1;
      }
    }
    return count;
  };

  function<int(int, int)> mergeSort = [&merge, &mergeSort](int left,
                                                           int right) {
    int count = 0;
    if (left + 1 < right) {
      int mid = (left + right) / 2;
      count += mergeSort(left, mid);
      count += mergeSort(mid, right);
      count += merge(left, mid, right);
    }
    return count;
  };

  auto r = mergeSort(0, n);
  for (int i = 0; i < n; ++i) {
    if (i != 0) {
      cout << ' ';
    }
    cout << v[i];
  }
  cout << endl;
  cout << r << endl;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

void merge(int A[], int n, int left, int mid, int right, int &ans) {
  int *tmp = new int[n];

  for (int i = left; i <= mid; ++i) {
    tmp[i] = A[i];
  }

  for (int i = mid + 1, j = right; i <= right; --j, ++i) {
    tmp[i] = A[j];
  }

  int i = left;
  int j = right;
  for (int k = left; k <= right; ++k) {
    ++ans;
    if (tmp[i] <= tmp[j]) {
      A[k] = tmp[i++];
    } else {
      A[k] = tmp[j--];
    }
  }

  delete[] tmp;
}

void mergeSort(int A[], int n, int left, int right, int &ans) {
  if (right <= left)
    return;

  int mid = (left + right) / 2;
  mergeSort(A, n, left, mid, ans);
  mergeSort(A, n, mid + 1, right, ans);
  merge(A, n, left, mid, right, ans);
}

int main() {

  int n;
  cin >> n;

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

  int ans = 0;
  mergeSort(S, n, 0, n - 1, ans);

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

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

void merge(int A[], int left, int mid, int right, int &ans) {
  int l = mid - left;
  int r = right - mid;
  int *L = new int[l];
  int *R = new int[r];
  for (int i = 0; i < l; ++i) {
    L[i] = A[left + i];
  }
  for (int i = 0; i < r; ++i) {
    R[i] = A[mid + i];
  }
  L[l] = R[r] = 2000000000;
  int i = 0;
  int j = 0;
  for (int k = left; k < right; ++k) {
    ++ans;
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i += 1;
    } else {
      A[k] = R[j];
      j += 1;
    }
  }
  delete[] L;
  delete[] R;
}

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

  int n;
  cin >> n;

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

  int ans = 0;
  mergeSort(S, 0, n, ans);

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

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

#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define MAX_LEN 500000
#define BIG 2000000000

int L[MAX_LEN / 2 + 2], R[MAX_LEN / 2 + 2];

int cnt;

void merge_sort_helper(int *A, int n, int l, int m, int r) {
  for (int i = l; i < m; ++i)
    L[i - l] = A[i];
  for (int i = m; i < r; ++i)
    R[i - m] = A[i];
  L[m - l] = R[r - m] = BIG;

  int idx1(0), idx2(0);
  for (int idx0 = l; idx0 < r; ++idx0) {
    A[idx0] = (L[idx1] <= R[idx2]) ? L[idx1++] : R[idx2++];
    cnt++;
  }
}

void merge_sort(int *A, int n, int l, int r) {
  if (l < r - 1) {
    int m = l + (r - l) / 2;
    merge_sort(A, n, l, m);
    merge_sort(A, n, m, r);
    merge_sort_helper(A, n, l, m, r);
  }
}

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

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

  merge_sort(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 <iostream>
#include <string>
#include <vector>

using namespace std;

#define MAX_LEN 50000
#define BIG 2000000000

int L[MAX_LEN / 2 + 2], R[MAX_LEN / 2 + 2];

int cnt;

void merge_sort_helper(int *A, int n, int l, int m, int r) {
  for (int i = l; i < m; ++i)
    L[i - l] = A[i];
  for (int i = m; i < r; ++i)
    R[i - m] = A[i];
  L[m - l] = R[r - m] = BIG;

  int idx1(0), idx2(0);
  for (int idx0 = l; idx0 < r; ++idx0) {
    A[idx0] = (L[idx1] <= R[idx2]) ? L[idx1++] : R[idx2++];
    cnt++;
  }
}

void merge_sort(int *A, int n, int l, int r) {
  if (l < r - 1) {
    int m = l + (r - l) / 2;
    merge_sort(A, n, l, m);
    merge_sort(A, n, m, r);
    merge_sort_helper(A, n, l, m, r);
  }
}

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

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

  merge_sort(A, n, 0, n);
  for (int i = 0; i < n; ++i) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
  cout << cnt << endl;
  return 0;
}