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

#include <stdio.h>

int count = 0;
int temp[500000];

void MergeSort(int *, int, int);

int main(void) {
  int i, n;

  int A[1000000];

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

  MergeSort(A, 0, n - 1);

  /*出力*/

  for (i = 0; i < n - 1; i++)
    printf("%d ", A[i]);
  printf("%d\\n", A[n - 1]);
  printf("%d\\n", count);
  return 0;
}

/* left から right マージソート */
void MergeSort(int *A, int left, int right) {
  int mid, i, j, k;
  /* 配列の要素が一つの場合 */
  if (left >= right)
    return;

  mid = (left + right) / 2;
  MergeSort(A, left, mid);
  MergeSort(A, mid + 1, right);

  /* left から mid*/
  for (i = left; i <= mid; i++)
    temp[i] = A[i];

  /* mid + 1からright */
  for (i = mid + 1, j = right; i <= right; i++, j--)
    temp[i] = A[j];

  i = left;
  j = right;

  for (k = left; k <= right; k++) {
    if (temp[i] <= temp[j])
      A[k] = temp[i++];

    else
      A[k] = temp[j--];

    count++;
  }
}


Error: -11

Fixed submission:

#include <stdio.h>

int count = 0;
int temp[500000];

void MergeSort(int *, int, int);

int main(void) {
  int i, n;

  int A[1000000000];

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

  MergeSort(A, 0, n - 1);

  /*出力*/

  for (i = 0; i < n - 1; i++)
    printf("%d ", A[i]);
  printf("%d\\n", A[n - 1]);
  printf("%d\\n", count);
  return 0;
}

/* left から right マージソート */
void MergeSort(int *A, int left, int right) {
  int mid, i, j, k;
  /* 配列の要素が一つの場合 */
  if (left >= right)
    return;

  mid = (left + right) / 2;
  MergeSort(A, left, mid);
  MergeSort(A, mid + 1, right);

  /* left から mid*/
  for (i = left; i <= mid; i++)
    temp[i] = A[i];

  /* mid + 1からright */
  for (i = mid + 1, j = right; i <= right; i++, j--)
    temp[i] = A[j];

  i = left;
  j = right;

  for (k = left; k <= right; k++) {
    if (temp[i] <= temp[j])
      A[k] = temp[i++];

    else
      A[k] = temp[j--];

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

#include <iostream>
#include <limits.h>
#define SENTINEL INT_MAX // SENTINELをとても大きい値で初期化する
using namespace std;
typedef long long ll;
ll counter = 0;
void Merge(long long S[], int left, int mid,
           int right) { // 擬似コードに従った、Merge
  int n1 = mid - left;
  int n2 = right - mid;
  ll L[n1 + 1];
  ll R[n2 + 1];

  for (int i = 0; i < n1; i++) {
    L[i] = S[left + i];
  }
  for (int i = 0; i < n2; i++) {
    R[i] = S[mid + i];
  }
  L[n1] = R[n2] = SENTINEL; // 番兵設置
  int i = 0;
  int j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      S[k] = L[i];
      i = i + 1;
      counter++;
    } else {
      S[k] = R[j];
      j = j + 1;
      counter++;
    }
  }
}

void Merge_Sort(long long S[], int left,
                int right) { // 擬似コードに従った,Merge_Sort

  if (left + 1 < right) {
    int mid = (left + right) / 2;
    Merge_Sort(S, left, mid);
    Merge_Sort(S, mid, right);
    Merge(S, left, mid, right);
  }
}

int main() {
  int n;
  ll S[600000];

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

  Merge_Sort(S, 0, n);

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

  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  int n, q, A[3000], M[3000];
  int dp[3000];

  cin >> n;
  rep(i, n) cin >> A[i]; // sortされて与えられるみたいなのでソートは略
  cin >> q;
  rep(h, q) {
    cin >> M[h];
    rep(i, M[h] + 1) dp[i] = -1;
    dp[0] = 0;
    rep(i, n) {
      for (
          int j = 0; j <= M[h];
          j++) { // 0からM[h](確かめたい値)まで１つずつ増やして作れるか確かめていく
        if (dp[j] >=
            0) { // もし0 or
                 // 1ならばその値は作れているので１をいれる(今回は１だが複数回使えるときは使える回数を代入しても良い)
          dp[j] = 1;
        } else if (
            dp[j - A[i]] <= 0 ||
            j < A[i]) { // 今の値の場所からA[i]（与えられた値）を引いたところが０orマイナスならもう既に使っているかもとよりつくれないのでスルー　jがA[i]より小さいのであれば作れないので明らかなのでスルー
          dp[j] = -1;
        } else { // 以上の条件を満たしていないなら作れるので配列DPにいれる　マイナス１しているのは使った値をまた使わないため
          dp[j] = dp[j - A[i]] - 1;
        }
        //  cout << dp[j] << endl;
      }
    }
    if (dp[M[h]] >= 0)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

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

#include <cstdio>
#include <iostream>

#define NMAX 500000
#define SENTINEL 1000000001

int cnt = 0;

using namespace std;

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

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(void) {

  int n;
  int A[NMAX];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  merge_sort(A, 0, n);
  for (int i = 0; i < n - 1; i++) {
    cout << A[i] << " ";
  }
  cout << A[n - 1] << endl;
  cout << cnt << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>

#define NMAX 5000
#define SENTINEL 1000000001

int cnt = 0;

using namespace std;

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

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(void) {

  int n;
  int A[NMAX];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  merge_sort(A, 0, n);
  for (int i = 0; i < n - 1; i++) {
    cout << A[i] << " ";
  }
  cout << A[n - 1] << endl;
  cout << cnt << endl;

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

#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;

int cnt = 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);
  vector<int> 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] = 0x7FFFFFFF;
  R[n2] = 0x7FFFFFFF;

  int i = 0;
  int j = 0;
  for (int 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(vector<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;
  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  mergesort(A, 0, n);
  for (int i = 0; i < n; i++) {
    cout << A[i];
    if (i != n - 1) {
      cout << " ";
    }
  }
  cout << endl;
  cout << cnt << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;

int cnt = 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);
  vector<int> R(n1 + 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] = 0x7FFFFFFF;
  R[n2] = 0x7FFFFFFF;

  int i = 0;
  int j = 0;
  for (int 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(vector<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;
  vector<int> A(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }

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

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

#define rep(i, n) for (i = 0; i < (n); i++)
#define For(i, a, b) for (i = (a); i < (b); i++)
#define size 500000

int count = 0;

void merge(int a[], int left, int mid, int right) {
  int n1 = mid - left, n2 = right - mid;
  int l[n1 + 1], r[n2 + 1];
  int i, j, k;
  rep(i, n1) l[i] = a[left + i];
  rep(i, n2) r[i] = a[mid + i];
  l[n1] = INT_MAX;
  r[n2] = INT_MAX;
  i = 0;
  j = 0;
  For(k, left, right) {
    if (l[i] <= r[j]) {
      a[k] = l[i];
      // printf("%d\\n",a[k]);
      i++;
    } else {
      a[k] = r[j];
      // printf("%d\\n",a[k]);
      j++;
    }
    count++;
  }
}

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

int main() {
  int n, s[size];
  int i, j, k, l;

  scanf("%d", &n);
  rep(i, n) scanf("%d", &s[i]);

  mergesort(s, 0, n);
  rep(i, n) {
    printf("%d", s[i]);
    if (i == n - 1) {
      printf("\\n");
    } else {
      printf(" ");
    }
  }
  printf("%d\\n", count);

  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

#define rep(i, n) for (i = 0; i < (n); i++)
#define For(i, a, b) for (i = (a); i < (b); i++)
#define size 100000

int count = 0;

void merge(int a[], int left, int mid, int right) {
  int n1 = mid - left, n2 = right - mid;
  int l[n1 + 1], r[n2 + 1];
  int i, j, k;
  rep(i, n1) l[i] = a[left + i];
  rep(i, n2) r[i] = a[mid + i];
  l[n1] = size;
  r[n2] = size;
  i = 0;
  j = 0;
  For(k, left, right) {
    if (l[i] <= r[j]) {
      a[k] = l[i];
      // printf("%d\\n",a[k]);
      i++;
    } else {
      a[k] = r[j];
      // printf("%d\\n",a[k]);
      j++;
    }
    count++;
  }
}

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

int main() {
  int n, s[size];
  int i, j, k, l;

  scanf("%d", &n);
  rep(i, n) scanf("%d", &s[i]);

  mergesort(s, 0, n);
  rep(i, n) {
    printf("%d", s[i]);
    if (i == n - 1) {
      printf("\\n");
    } else {
      printf(" ");
    }
  }
  printf("%d\\n", count);

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

#include <iostream>
#include <math.h>
#define MAX 500000
// #define INFINITY 200000000
using namespace std;

void merge(int[], int, int, int, int);
void mergeSort(int[], int, int, int);
// int n,S[MAX];
int L[MAX / 2 + 2], R[MAX / 2 + 2];
int cnt;

int main() {
  int i, n;
  cnt = 0;
  int A[MAX];
  cin >> n;
  // scanf("%d",&n);
  for (i = 0; i < n; i++) {
    cin >> A[i];
    // scanf("%d",&A[i]);
    // mergeSort(S[i],0,i);
  }
  mergeSort(A, n, 0, n);
  for (i = 0; i < n; i++) {
    if (0 < i) {
      cout << " ";
      // printf(" ");
    }
    cout << A[i];
    // printf("%d",A[i]);
  }
  cout << endl << cnt << endl; /**/
  // printf("\\n%d\\n",cnt);
  return 0;
}
void merge(int A[], int n, int left, int mid, int right) {
  int n1, n2, i, j, k;
  n1 = mid - left;
  n2 = right - mid;
  /*L[0...n1],R[0...n2]?????????*/
  for (i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }
  for (i = 0; i < n2; i++) {
    R[i] = A[mid + i];
  }
  L[n1] = INFINITY;
  R[n2] = INFINITY;
  // L[n1] = SMAX;
  // R[n2] = SMAX;
  i = 0;
  j = 0;
  for (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) {
  int mid;
  if (left + 1 < right) {
    mid = (left + right) / 2;
    mergeSort(A, n, left, mid);
    mergeSort(A, n, mid, right);
    merge(A, n, left, mid, right);
  }
}

Error: 0

Fixed submission:

#include <iostream>
// #include <math.h>
#define MAX 500000
#define INFINITY 200000000
using namespace std;

void merge(int[], int, int, int, int);
void mergeSort(int[], int, int, int);
// int n,S[MAX];
int L[MAX / 2 + 2], R[MAX / 2 + 2];
int cnt;

int main() {
  int i, n;
  cnt = 0;
  int A[MAX];
  cin >> n;
  // scanf("%d",&n);
  for (i = 0; i < n; i++) {
    cin >> A[i];
    // scanf("%d",&A[i]);
    // mergeSort(S[i],0,i);
  }
  mergeSort(A, n, 0, n);
  for (i = 0; i < n; i++) {
    if (0 < i) {
      cout << " ";
      // printf(" ");
    }
    cout << A[i];
    // printf("%d",A[i]);
  }
  cout << endl << cnt << endl; /**/
  // printf("\\n%d\\n",cnt);
  return 0;
}
void merge(int A[], int n, int left, int mid, int right) {
  int n1, n2, i, j, k;
  n1 = mid - left;
  n2 = right - mid;
  /*L[0...n1],R[0...n2]?????????*/
  for (i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }
  for (i = 0; i < n2; i++) {
    R[i] = A[mid + i];
  }
  L[n1] = INFINITY;
  R[n2] = INFINITY;
  // L[n1] = SMAX;
  // R[n2] = SMAX;
  i = 0;
  j = 0;
  for (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) {
  int mid;
  if (left + 1 < right) {
    mid = (left + right) / 2;
    mergeSort(A, n, left, mid);
    mergeSort(A, n, mid, right);
    merge(A, n, left, mid, right);
  }
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int a[500009], t[500009], r = 0;
void mergesort(int lo, int hi) {
  if (lo == hi)
    return;
  int mid = (lo + hi) / 2;
  mergesort(lo, mid);
  mergesort(mid + 1, hi);

  int i, j, k;
  i = lo;
  j = mid + 1;
  for (k = lo; k <= hi; k++) {

    if (i == mid + 1)
      t[k] = a[j++];
    else if (j == hi + 1)
      t[k] = a[i++];
    else if (a[i] < a[j])
      t[k] = a[i++];
    else
      t[k] = a[j++];
    r++;
  }

  for (k = lo; k <= hi; k++) {
    a[k] = t[k];
  }
}
int main() {

  int m, n, i, j, s = 0, t = 0, te;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];

  mergesort(0, n - 1);
  for (i = 0; i < n - 1; i++)
    cout << a[i] << " ";
  cout << a[n - 1] << endl;
  cout << r;
  cout << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a[120000], t[120000], r = 0;
void mergesort(int lo, int hi) {
  if (lo == hi)
    return;
  int mid = (lo + hi) / 2;
  mergesort(lo, mid);
  mergesort(mid + 1, hi);

  int i, j, k;
  i = lo;
  j = mid + 1;
  for (k = lo; k <= hi; k++) {

    if (i == mid + 1)
      t[k] = a[j++];
    else if (j == hi + 1)
      t[k] = a[i++];
    else if (a[i] < a[j])
      t[k] = a[i++];
    else
      t[k] = a[j++];
    r++;
  }

  for (k = lo; k <= hi; k++) {
    a[k] = t[k];
  }
}
int main() {

  int m, n, i, j, s = 0, t = 0, te;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];

  mergesort(0, n - 1);
  for (i = 0; i < n - 1; i++)
    cout << a[i] << " ";
  cout << a[n - 1] << endl;
  cout << r;
  cout << endl;

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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;
#define MAX_N (500010)
int A[MAX_N];
int L[(MAX_N >> 1) + 1];
int R[(MAX_N >> 1) + 1];
int cnt = 0;

void merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  // vector<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] = 1000000001;
  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++];
  }
  // return cnt;
  // vector<int>().swap(L);
  // vector<int>().swap(R);
}

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

void solve() {
  int n, sum;
  cin >> n;
  // vector<int> A(n);
  for (int i = 0; i < n; i++)
    cin >> A[i];
  merge_sort(A, 0, n);
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  // cout << endl << sum << endl;
  cout << endl << cnt << endl;
}

signed main() { solve(); }

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;
#define int long long
int cnt = 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);
  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] = 500001;
  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++];
  }
  // return cnt;
  vector<int>().swap(L);
  vector<int>().swap(R);
}

void merge_sort(vector<int> &A, int left, int right) {
  // int sum = 0;
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    // sum = merge_sort(A, left, mid) + merge_sort(A, mid, right);
    // sum += merge(A, left, mid, right);
    merge_sort(A, left, mid);
    merge_sort(A, mid, right);
    merge(A, left, mid, right);
  }
  // return sum;
}

void solve() {
  int n, sum;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; i++)
    cin >> A[i];
  merge_sort(A, 0, n);
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  // cout << endl << sum << endl;
  cout << endl << cnt << endl;
}

signed main() { solve(); }
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

#define SIZE 500000
#define INFTY 1200000000
int count = 0;

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

void Merge(int *A, int left, int mid, int right) {
  int R[SIZE], L[SIZE];
  int i, j, k, n1, n2;
  n1 = mid - left;
  n2 = right - mid;
  for (i = 0; i < n1; i++)
    L[i] = A[i + left];
  for (j = 0; j < n2; j++)
    R[j] = A[j + mid];

  L[n1] = INFTY;
  R[n2] = INFTY;
  i = j = 0;

  for (k = left; k < right; k++) {

    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    }

    else {
      A[k] = R[j];
      j++;
    }

    count++;
  }
}

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

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

  MergeSort(S, 0, n);
  show(S, n);
  printf("%d\\n", count);

  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

#define SIZE 500
#define INFTY 120000
int count = 0;

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

void Merge(int *A, int left, int mid, int right) {
  int R[SIZE], L[SIZE];
  int i, j, k, n1, n2;
  n1 = mid - left;
  n2 = right - mid;
  for (i = 0; i < n1; i++)
    L[i] = A[i + left];
  for (j = 0; j < n2; j++)
    R[j] = A[j + mid];

  L[n1] = INFTY;
  R[n2] = INFTY;
  i = j = 0;

  for (k = left; k < right; k++) {

    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    }

    else {
      A[k] = R[j];
      j++;
    }

    count++;
  }
}

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

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

  MergeSort(S, 0, n);
  show(S, n);
  printf("%d\\n", count);

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


#include <iostream>
using namespace std;

int INF = 1234567890;

int L[300000];
int R[300000];
int cnt;

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

  int i = 0;
  int 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 n, A[600000];
  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 > 0)
      cout << " ";
    cout << A[i];
  }
  cout << endl << cnt << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int INF = 123456789;

int L[300000];
int R[300000];
int cnt;

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

  int i = 0;
  int 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 n, A[500000];
  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 > 0)
      cout << " ";
    cout << A[i];
  }
  cout << endl << cnt << endl;
  return 0;
}