prithivMLmods/Coder-Stat huggingface dataset:
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#include <iostream>

using namespace std;

#define MAX 500000
#define SENTINEL 2000000000

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

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

  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];

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

  int i = 0, j = 0;

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

void mergeSort(int A[], int n, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 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 (status: Runtime Error):
#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;
}

/*

*/


Fixed submission:
#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;
}

/*

*/
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#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 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;
}


Fixed submission:
#include <iostream>
using namespace std;
#define MAX 500000
#define SENTINEL 2000000000

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

void merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = R[n2] = SENTINEL;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    cnt++;
    if (L[i] <= R[j]) {
      A[k] = L[i++];
    } else {
      A[k] = R[j++];
    }
  }
}

void mergeSort(int A[], int n, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 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 (status: 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; i++) {
    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;
}

Fixed submission:
#include <iostream>
using namespace std;
#define MAX 500000
#define SENTINEL 2000000000

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

void merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = R[n2] = SENTINEL;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    cnt++;
    if (L[i] <= R[j])
      A[k] = L[i++];
    else
      A[k] = R[j++];
  }
}

void mergeSort(int A[], int n, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 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 (status: Runtime Error):
//
//  test.cpp
//  AtCoder
//
//  Created by A on 2018/06/01.
//  Copyright © 2018年 A. All rights reserved.
//

#include <iostream>
using namespace std;
#define MAX 500000
#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;
  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;
}


Fixed submission:
//
//  test.cpp
//  AtCoder
//
//  Created by A on 2018/06/01.
//  Copyright © 2018年 A. All rights reserved.
//

#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;
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stack>
#include <stdio.h>
#include <string>
using namespace std;
int merges(int start, int end) {
  int middle;
  if (start + 1 < end) {

    middle = (start + end) / 2;
    return (merges(start, middle) + merges(middle, end) - start + end);
  } else {
    return 0;
  }
}

int main() {
  int n, dat[5000000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> dat[i];
  }
  sort(dat, dat + n);
  for (int i = 0; i < n; i++) {
    if (i != 0) {
      cout << " ";
    }
    cout << dat[i];
  }

  cout << endl << merges(0, n) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stack>
#include <stdio.h>
#include <string>
using namespace std;
int merges(int start, int end) {
  int middle;
  if (start + 1 < end) {

    middle = (start + end) / 2;
    return (merges(start, middle) + merges(middle, end) - start + end);
  } else {
    return 0;
  }
}

int main() {
  int n, dat[500000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> dat[i];
  }
  sort(dat, dat + n);
  for (int i = 0; i < n; i++) {
    if (i != 0) {
      cout << " ";
    }
    cout << dat[i];
  }

  cout << endl << merges(0, n) << endl;
  return 0;
}
Problem p02272 (C++)

Buggy submission (status: 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++;
    }
  }
}

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

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++;
    }
  }
  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;
}
Problem p02272 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cstdlib>
#include <iostream>
using namespace std;
#define INF 1000000000
int count = 0;
void merge(int a[], int left, int mid, int right) {
  int n1 = mid - left, n2 = right - mid, i, j, k;
  int *m1 = (int *)malloc((n1 + 1) * sizeof(int)),
      *m2 = (int *)malloc((n2 + 1) * sizeof(int));
  for (i = 0; i < n1; i++)
    m1[i] = a[left + i];
  for (i = 0; i < n2; i++)
    m2[i] = a[mid + i];
  m1[n1] = m2[n2] = INF;
  i = j = 0;
  for (k = left; k < right; k++) {
    count++;
    if (m1[i] <= m2[j]) {
      a[k] = m1[i];
      i++;
    } else {
      a[k] = m2[j];
      j++;
    }
  }
}
void mergesort(int a[], int left, int right) {
  int mid;
  if (right - left > 1) {
    mid = (left + right) / 2;
    mergesort(a, left, mid);
    mergesort(a, mid, right);
    merge(a, left, mid, right);
  }
}
int main() {
  int n, i;
  int a[500000];
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  mergesort(a, 0, n);
  for (i = 0; i < n; i++) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << endl << count << endl;
  return 0;
}

Fixed submission:
#include <cstdlib>
#include <iostream>
using namespace std;
#define INF 1000000000
int count = 0;
void merge(int a[], int left, int mid, int right) {
  int n1 = mid - left, n2 = right - mid, i, j, k;
  int *m1 = (int *)malloc((n1 + 1) * sizeof(int)),
      *m2 = (int *)malloc((n2 + 1) * sizeof(int));
  for (i = 0; i < n1; i++)
    m1[i] = a[left + i];
  for (i = 0; i < n2; i++)
    m2[i] = a[mid + i];
  m1[n1] = m2[n2] = INF;
  i = j = 0;
  for (k = left; k < right; k++) {
    count++;
    if (m1[i] <= m2[j]) {
      a[k] = m1[i];
      i++;
    } else {
      a[k] = m2[j];
      j++;
    }
  }
  free(m1);
  free(m2);
}
void mergesort(int a[], int left, int right) {
  int mid;
  if (right - left > 1) {
    mid = (left + right) / 2;
    mergesort(a, left, mid);
    mergesort(a, mid, right);
    merge(a, left, mid, right);
  }
}
int main() {
  int n, i;
  int a[500000];
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  mergesort(a, 0, n);
  for (i = 0; i < n; i++) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << endl << count << endl;
  return 0;
}
Problem p02272 (C++)

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

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

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

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

  mergeSort(A, n, 0, n);

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

  cout << cnt << endl;

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
#define MAX 500000
#define SENTINEL 2000000000

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

void merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = R[n2] = SENTINEL;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    cnt++;
    if (L[i] <= R[j]) {
      A[k] = L[i++];
    } else {
      A[k] = R[j++];
    }
  }
}

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

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

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

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

  mergeSort(A, n, 0, n);

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

  cout << cnt << endl;

  return 0;
}
Problem p02272 (C++)

Buggy submission (status: 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 * 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);
}

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 + 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);
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#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";
}


Fixed submission:
#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";
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

const int INF = 1 << 30;

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

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

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

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

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

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

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

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

  return count_;
}

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

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

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

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

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

const int INF = 1 << 30;

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

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

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

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

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

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

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

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

  return count_;
}

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

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

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

  printf("%zu\n", t);
  return 0;
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#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++;
  }
}


Fixed submission:
#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++;
  }
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;

int A[50001], 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 = j = 0;
  for (int k = 0; k < r - l; k++, S++) {
    if (j == n2 || (i < n1 && L[i] <= R[j]))
      A[k + l] = L[i++];
    else
      A[k + l] = R[j++];
  }
}

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

Fixed submission:
#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 = j = 0;
  for (int k = 0; k < r - l; k++, S++) {
    if (j == n2 || (i < n1 && L[i] <= R[j]))
      A[k + l] = L[i++];
    else
      A[k + l] = R[j++];
  }
}

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 (status: Runtime Error):
#include <iostream>

using namespace std;

int b[100099], n = 0, ans = 0;

void Merge(int a[], int l, int r) {
  int m = (l + r) / 2;
  int i = l, j = m + 1, sum[r - l + 1], cnt = -1;
  ans += r - l + 1;
  while (i <= m && j <= r) {
    if (a[i] < a[j])
      sum[++cnt] = a[i++];
    else
      sum[++cnt] = a[j++];
  }
  while (i <= m)
    sum[++cnt] = a[i++];
  while (j <= r)
    sum[++cnt] = a[j++];
  for (int i = l; i <= r; i++)
    a[i] = sum[i - l];
}

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

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> b[i];
  Merge_sort(b, 0, n - 1);
  for (int i = 0; i < n - 1; i++)
    cout << b[i] << " ";
  cout << b[n - 1] << endl;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int b[500099], n = 0, ans = 0;

void Merge(int a[], int l, int r) {
  int m = (l + r) / 2;
  int i = l, j = m + 1, sum[r - l + 1], cnt = -1;
  ans += r - l + 1;
  while (i <= m && j <= r) {
    if (a[i] < a[j])
      sum[++cnt] = a[i++];
    else
      sum[++cnt] = a[j++];
  }
  while (i <= m)
    sum[++cnt] = a[i++];
  while (j <= r)
    sum[++cnt] = a[j++];
  for (int i = l; i <= r; i++)
    a[i] = sum[i - l];
}

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

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> b[i];
  Merge_sort(b, 0, n - 1);
  for (int i = 0; i < n - 1; i++)
    cout << b[i] << " ";
  cout << b[n - 1] << endl;
  cout << ans << endl;
  return 0;
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define MAX 500000
#define SENTINEL 2000000000

int L[MAX / 4], R[MAX / 4];
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;
}


Fixed submission:
#include <iostream>
using namespace std;
#define MAX 500000
#define SENTINEL 2000000000

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

void merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = R[n2] = SENTINEL;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    cnt++;
    if (L[i] <= R[j]) {
      A[k] = L[i++];
    } else {
      A[k] = R[j++];
    }
  }
}

void mergesort(int A[], int n, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 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 (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02272 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <climits>
#include <iostream>
#include <vector>

void merge(std::vector<int> *A, int left, int mid, int right, int *count) {
  int n1 = mid - left;
  int n2 = right - mid;

  std::vector<int> *L = new std::vector<int>(n1 + 1, 0);
  std::vector<int> *R = new std::vector<int>(n2 + 1, 0);

  for (int i = 0; i < n1; i++) {
    L->at(i) = A->at(left + i);
  }

  for (int i = 0; i < n2; i++) {
    R->at(i) = A->at(mid + i);
  }

  L->at(n1) = INT_MAX;
  R->at(n2) = INT_MAX;

  int i = 0;
  int j = 0;

  for (int k = left; k < right; k++) {
    if (L->at(i) <= R->at(j)) {
      A->at(k) = L->at(i);
      i++;
    } else {
      A->at(k) = R->at(j);
      j++;
    }

    (*count)++;
  }
}

void merge_sort(std::vector<int> *A, int left, int right, int *count) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    merge_sort(A, left, mid, count);
    merge_sort(A, mid, right, count);
    merge(A, left, mid, right, count);
  }
}

int main(void) {
  int n = 0;

  std::cin >> n;

  std::vector<int> *A = new std::vector<int>(n, 0);

  for (int i = 0; i < n; i++) {
    int v = 0;

    std::cin >> v;

    A->at(i) = v;
  }

  int count = 0;

  int left = 0;
  int right = A->size();
  int mid = (left + right) / 2;

  merge_sort(A, left, right, &count);

  for (int i = 0; i < n; i++) {
    if (i < (n - 1)) {
      std::cout << A->at(i) << " ";
    } else {
      std::cout << A->at(i) << std::endl;
    }
  }

  std::cout << count << std::endl;

  return 0;
}

Fixed submission:
#include <climits>
#include <iostream>
#include <vector>

void merge(std::vector<int> *A, int left, int mid, int right, int *count) {
  int n1 = mid - left;
  int n2 = right - mid;

  std::vector<int> *L = new std::vector<int>(n1 + 1, 0);
  std::vector<int> *R = new std::vector<int>(n2 + 1, 0);

  for (int i = 0; i < n1; i++) {
    L->at(i) = A->at(left + i);
  }

  for (int i = 0; i < n2; i++) {
    R->at(i) = A->at(mid + i);
  }

  L->at(n1) = INT_MAX;
  R->at(n2) = INT_MAX;

  int i = 0;
  int j = 0;

  for (int k = left; k < right; k++) {
    if (L->at(i) <= R->at(j)) {
      A->at(k) = L->at(i);
      i++;
    } else {
      A->at(k) = R->at(j);
      j++;
    }

    (*count)++;
  }

  delete R;
  delete L;
}

void merge_sort(std::vector<int> *A, int left, int right, int *count) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    merge_sort(A, left, mid, count);
    merge_sort(A, mid, right, count);
    merge(A, left, mid, right, count);
  }
}

int main(void) {
  int n = 0;

  std::cin >> n;

  std::vector<int> *A = new std::vector<int>(n, 0);

  for (int i = 0; i < n; i++) {
    int v = 0;

    std::cin >> v;

    A->at(i) = v;
  }

  int count = 0;

  int left = 0;
  int right = A->size();
  int mid = (left + right) / 2;

  merge_sort(A, left, right, &count);

  for (int i = 0; i < n; i++) {
    if (i < (n - 1)) {
      std::cout << A->at(i) << " ";
    } else {
      std::cout << A->at(i) << std::endl;
    }
  }

  std::cout << count << std::endl;

  return 0;
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define MAXN 100000 + 5
using namespace std;
int a[MAXN];
int t[MAXN];
int count = 0;
void merge(int *A, int x, int y, int *T) {
  if (y - x > 1) {
    int mid = x + (y - x) / 2;
    int p = x, q = mid, i = x;
    merge(A, x, mid, T);
    merge(A, mid, y, T);
    while (q < y || p < mid) {
      if (q >= y || (p < mid && A[p] <= A[q]))
        T[i++] = A[p++];
      else
        T[i++] = A[q++];
      count++;
    }
    for (i = x; i < y; i++)
      A[i] = T[i];
  }
}
int main() {
  int n;
  int i, j;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  merge(a, 0, n, t);
  for (i = 0; i < n - 1; i++)
    cout << t[i] << ' ';
  cout << t[n - 1] << endl;
  cout << count << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#define MAXN 500000 + 5
using namespace std;
int a[MAXN];
int t[MAXN];
int count = 0;
void merge(int *A, int x, int y, int *T) {
  if (y - x > 1) {
    int mid = x + (y - x) / 2;
    int p = x, q = mid, i = x;
    merge(A, x, mid, T);
    merge(A, mid, y, T);
    while (q < y || p < mid) {
      if (q >= y || (p < mid && A[p] <= A[q]))
        T[i++] = A[p++];
      else
        T[i++] = A[q++];
      count++;
    }
    for (i = x; i < y; i++)
      A[i] = T[i];
  }
}
int main() {
  int n;
  int i, j;
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  merge(a, 0, n, t);
  for (i = 0; i < n - 1; i++)
    cout << t[i] << ' ';
  cout << t[n - 1] << endl;
  cout << count << endl;
  return 0;
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}