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

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

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

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
#define INF 1000000000
#define nmax 500005
using namespace std;

int n;
int cnt = 0;
vector<int> a;

void merge(int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  vector<int> l(nmax);
  vector<int> r(nmax);

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

  i = 0;
  j = 0;

  for (int k = left; k < right; k++) {
    if (l[i] <= r[j])
      a[k] = l[i++];

    else
      a[k] = r[j++];

    cnt++;
  }
}

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;
  a.resize(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 << cnt << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
#define INF 1000000000
#define nmax 500005
using namespace std;

int n;
int cnt = 0;
vector<int> a;

void merge(int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  vector<int> l(n1 + 5);
  vector<int> r(n1 + 5);

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

  i = 0;
  j = 0;

  for (int k = left; k < right; k++) {
    if (l[i] <= r[j])
      a[k] = l[i++];

    else
      a[k] = r[j++];

    cnt++;
  }
}

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;
  a.resize(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 << cnt << endl;

  return 0;
}
Problem p02272 (C++)

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

int MargeElem(int elem[], int start, int mid, int end) {
  int i, j, k, l;
  int num = 0;
  int A[2500], B[2500];
  i = mid - start;
  j = end - mid;
  l = (i > j) ? i : j;
  for (k = 0; k < l; k++) {
    A[k] = elem[start + k];
    B[k] = elem[mid + k];
  }
  A[i] = 0x7FFFFFFF;
  B[j] = 0x7FFFFFFF;

  i = 0;
  j = 0;
  for (k = start; k < end; k++) {
    ++num;
    if (A[i] <= B[j]) {
      elem[k] = A[i];
      ++i;
    } else {
      elem[k] = B[j];
      ++j;
    }
  }

  return num;
}

int MargeSort(int elem[], int start, int end) {
  int mid;
  int cnt = 0;

  if ((start + 1) >= end) {
    return 0;
  }

  mid = (start + end) / 2;
  cnt += MargeSort(elem, start, mid);
  cnt += MargeSort(elem, mid, end);
  cnt += MargeElem(elem, start, mid, end);

  return cnt;
}

int main(void) {
  int i;
  int num;
  int cnt;
  int elem[500002];

  scanf("%d", &num);
  for (i = 0; i < num; i++) {
    scanf("%d", &elem[i]);
  }

  cnt = MargeSort(elem, 0, num);

  printf("%d", elem[0]);
  for (i = 1; i < num; i++) {
    printf(" %d", elem[i]);
  }
  printf("\n%d\n", cnt);

  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;

int MargeElem(int elem[], int start, int mid, int end) {
  int i, j, k, l;
  int num = 0;
  int A[250001], B[250001];
  i = mid - start;
  j = end - mid;
  l = (i > j) ? i : j;
  for (k = 0; k < l; k++) {
    A[k] = elem[start + k];
    B[k] = elem[mid + k];
  }
  A[i] = 0x7FFFFFFF;
  B[j] = 0x7FFFFFFF;

  i = 0;
  j = 0;
  for (k = start; k < end; k++) {
    ++num;
    if (A[i] <= B[j]) {
      elem[k] = A[i];
      ++i;
    } else {
      elem[k] = B[j];
      ++j;
    }
  }

  return num;
}

int MargeSort(int elem[], int start, int end) {
  int mid;
  int cnt = 0;

  if ((start + 1) >= end) {
    return 0;
  }

  mid = (start + end) / 2;
  cnt += MargeSort(elem, start, mid);
  cnt += MargeSort(elem, mid, end);
  cnt += MargeElem(elem, start, mid, end);

  return cnt;
}

int main(void) {
  int i;
  int num;
  int cnt;
  int elem[500002];

  scanf("%d", &num);
  for (i = 0; i < num; i++) {
    scanf("%d", &elem[i]);
  }

  cnt = MargeSort(elem, 0, num);

  printf("%d", elem[0]);
  for (i = 1; i < num; i++) {
    printf(" %d", elem[i]);
  }
  printf("\n%d\n", cnt);

  return 0;
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
const int INFTY = 1000000;
int cnt = 0;

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

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);
  }
}
int main() {
  int n;
  int *A;
  cin >> n;
  A = new int[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];
  mergeSort(A, 0, n);
  for (int i = 0; i < n - 1; i++)
    cout << A[i] << ' ';
  cout << A[n - 1] << endl << cnt << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
const int INFTY = 1000000000;
int cnt = 0;

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

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);
  }
}
int main() {
  int n;
  int *A;
  cin >> n;
  A = new int[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];
  mergeSort(A, 0, n);
  for (int i = 0; i < n - 1; i++)
    cout << A[i] << ' ';
  cout << A[n - 1] << endl << cnt << endl;
  return 0;
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
//??????????????????
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep3(i, k, n) for (int i = k; i < n; i++)
#define INFTY 200000000
using namespace std;

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

//???????????????????£????????????§????????????
void merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  rep(i, n1) { L[i] = A[left + i]; }
  rep(i, n2) { R[i] = A[mid + i]; }
  L[n1] = INFTY;
  R[n2] = INFTY;
  int i = 0, j = 0, k;
  rep3(k, left, right) {
    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(void) {
  int A[500000], 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;

  cout << cnt << endl;

  return 0;
}

Fixed submission:
//??????????????????
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep3(i, k, n) for (int i = k; i < n; i++)
#define INFTY 2000000000
using namespace std;

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

//???????????????????£????????????§????????????
void merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  rep(i, n1) { L[i] = A[left + i]; }
  rep(i, n2) { R[i] = A[mid + i]; }
  L[n1] = INFTY;
  R[n2] = INFTY;
  int i = 0, j = 0, k;
  rep3(k, left, right) {
    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(void) {
  int A[500000], 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;

  cout << cnt << endl;

  return 0;
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
// #include<cmath>
using namespace std;

const int INFTY = 1000000001;
int S[200010], n, cmp;

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

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(void) {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  mergeSort(0, n);
  int i = 0;
  for (int j = 0; j < n; j++) {
    if (i++)
      cout << " ";
    cout << S[j];
  }
  cout << endl;
  cout << cmp << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
// #include<cmath>
using namespace std;

const int INFTY = 1000000001;
int S[500010], n, cmp;

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

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(void) {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  mergeSort(0, n);
  int i = 0;
  for (int j = 0; j < n; j++) {
    if (i++)
      cout << " ";
    cout << S[j];
  }
  cout << endl;
  cout << cmp << endl;
  return 0;
}
Problem p02272 (C++)

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

#define MAX 50000
#define INF 200000000

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

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] = INF;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    count++;
    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;
  count = 0;

  cin >> n;
  for (int 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 << count << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

#define MAX 500000
#define INF 2000000000

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

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] = INF;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    count++;
    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;
  count = 0;

  cin >> n;
  for (int 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 << count << endl;

  return 0;
}
Problem p02272 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

#define MAX 500000
#define INFTY 2000000000

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

void marge(int A[], int n, int left, int mid, int right) {

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

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

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

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

  i = 0;
  j = 0;

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

    count++;

    if (L[i] <= R[j]) {

      A[k] = L[i];
      i++;

    } else {

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

void margeSort(int A[], int n, int left, int right) {

  int mid;

  if (left + 1 < right) {

    mid = (left + right) / 2;

    margeSort(A, n, left, mid);
    margeSort(A, n, mid, right);

    marge(A, n, left, mid, right);
  }
}

int main() {

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

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

  margeSort(A, n, 0, n);

  for (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;

#define MAX 500000
#define INFTY 2000000000

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

void marge(int A[], int n, int left, int mid, int right) {

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

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

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

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

  i = 0;
  j = 0;

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

    count++;

    if (L[i] <= R[j]) {

      A[k] = L[i];
      i++;

    } else {

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

void margeSort(int A[], int n, int left, int right) {

  int mid;

  if (left + 1 < right) {

    mid = (left + right) / 2;

    margeSort(A, n, left, mid);
    margeSort(A, n, mid, right);

    marge(A, n, left, mid, right);
  }
}

int main() {

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

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

  margeSort(A, n, 0, n);

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

    if (i) {
      cout << " ";
    }

    cout << A[i];
  }

  cout << endl;

  cout << count << endl;

  return 0;
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

static void inputArray(uint32_t A[], int num) {
  for (int i = 0; i < num; i++) {
    cin >> A[i];
  }
}

static void printArray(uint32_t A[], int num) {
  for (int i = 0; i < num; i++) {
    cout << (i != 0 ? " " : "") << A[i];
  }
  cout << endl;
}

static int compared = 0;

static vector<uint32_t> L(25000);
static vector<uint32_t> R(25000);

static inline void merge(uint32_t A[], uint32_t left, uint32_t mid,
                         uint32_t right) {
  uint32_t n1 = mid - left;
  uint32_t 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] = UINT32_MAX;

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

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

int main() {
  int n;
  cin >> n;

  vector<uint32_t> A(n);
  inputArray(&A[0], n);

  mergeSort(&A[0], 0, n);

  printArray(&A[0], n);

  cout << compared << endl;

  return 0;
}


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

using namespace std;

static void inputArray(uint32_t A[], int num) {
  for (int i = 0; i < num; i++) {
    cin >> A[i];
  }
}

static void printArray(uint32_t A[], int num) {
  for (int i = 0; i < num; i++) {
    cout << (i != 0 ? " " : "") << A[i];
  }
  cout << endl;
}

static int compared = 0;

static vector<uint32_t> L(250000);
static vector<uint32_t> R(250000);

static inline void merge(uint32_t A[], uint32_t left, uint32_t mid,
                         uint32_t right) {
  uint32_t n1 = mid - left;
  uint32_t 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] = UINT32_MAX;

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

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

int main() {
  int n;
  cin >> n;

  vector<uint32_t> A(n);
  inputArray(&A[0], n);

  mergeSort(&A[0], 0, n);

  printArray(&A[0], n);

  cout << compared << endl;

  return 0;
}
Problem p02272 (C++)

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n, a[10005], l[10005], r[10005], inf = 5e5 + 1, s;
void merge(int x, int y) {
  if (x == y)
    return;
  int c1 = (x + y) / 2;
  int c2 = ((x + y) / 2) + 1;

  merge(x, c1);
  merge(c2, y);

  for (int i = 0; i <= c1 - x; i++)
    l[i] = a[i + x];
  for (int i = 0; i <= y - c2; i++)
    r[i] = a[i + c2];

  l[c1 - x + 1] = inf;
  r[y - c2 + 1] = inf;

  int k = x, l1 = 0, r1 = 0;

  for (int i = 0; i < y - x + 1; i++) {
    a[k] = min(l[l1], r[r1]);
    k++;
    if (l[l1] > r[r1])
      r1++;
    else
      l1++;
  }
  s += y - x + 1;
}

int main() {

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

  merge(0, n - 1);
  for (int i = 0; i < n; i++) {
    if (i != 0)
      cout << ' ';
    cout << a[i];
  }
  cout << "\n" << s << "\n";
}


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

int n, a[500005], l[500005], r[500005], inf = 1e9 + 1, s;
void merge(int x, int y) {
  if (x == y)
    return;
  int c1 = (x + y) / 2;
  int c2 = ((x + y) / 2) + 1;

  merge(x, c1);
  merge(c2, y);

  for (int i = 0; i <= c1 - x; i++)
    l[i] = a[i + x];
  for (int i = 0; i <= y - c2; i++)
    r[i] = a[i + c2];

  l[c1 - x + 1] = inf;
  r[y - c2 + 1] = inf;

  int k = x, l1 = 0, r1 = 0;

  for (int i = 0; i < y - x + 1; i++) {
    a[k] = min(l[l1], r[r1]);
    k++;
    if (l[l1] > r[r1])
      r1++;
    else
      l1++;
  }
  s += y - x + 1;
}

int main() {

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

  merge(0, n - 1);
  for (int i = 0; i < n; i++) {
    if (i != 0)
      cout << ' ';
    cout << a[i];
  }
  cout << "\n" << s << "\n";
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define MAX 500000
#define NIL 1000000
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] = NIL;
  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 Sort(int A[], int n, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    Sort(A, n, left, mid);
    Sort(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];
  Sort(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 NIL 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] = NIL;
  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 Sort(int A[], int n, int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    Sort(A, n, left, mid);
    Sort(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];
  Sort(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: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int S[500000];
int n;
int cnt;

void merge(int l, int m, int r) {
  int n1 = m - l;
  int n2 = r - m;
  int *L = (int *)malloc(sizeof(int) * (n1 + 1));
  int *R = (int *)malloc(sizeof(int) * (n2 + 1));
  for (int i = 0; i < n1; i++)
    L[i] = S[l + i];
  for (int i = 0; i < n2; i++)
    R[i] = S[m + i];
  L[n1] = INT_MAX;
  R[n2] = INT_MAX;
  int i = 0, j = 0;
  for (int k = l; k < r; k++) {
    if (L[i] <= R[j]) {
      S[k] = L[i];
      i++;
    } else {
      S[k] = R[j];
      j++;
    }
    cnt++;
  }
}

void 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() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  mergesort(0, n);
  for (int i = 0; i < n - 1; i++) {
    cout << S[i] << ' ';
  }
  cout << S[n - 1] << endl;
  cout << cnt << endl;
}

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

int S[500000];
int n;
int cnt;

void merge(int l, int m, int r) {
  int n1 = m - l;
  int n2 = r - m;
  int *L = (int *)malloc(sizeof(int) * (n1 + 1));
  int *R = (int *)malloc(sizeof(int) * (n2 + 1));
  for (int i = 0; i < n1; i++)
    L[i] = S[l + i];
  for (int i = 0; i < n2; i++)
    R[i] = S[m + i];
  L[n1] = INT_MAX;
  R[n2] = INT_MAX;
  int i = 0, j = 0;
  for (int k = l; k < r; k++) {
    if (L[i] <= R[j]) {
      S[k] = L[i];
      i++;
    } else {
      S[k] = R[j];
      j++;
    }
    cnt++;
  }
  free(L);
  free(R);
}

void 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() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  mergesort(0, n);
  for (int i = 0; i < n - 1; i++) {
    cout << S[i] << ' ';
  }
  cout << S[n - 1] << endl;
  cout << cnt << endl;
}
Problem p02272 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>

#define MAX 500000
#define INFINITY 100000000

int n;
int L[MAX / 2 + 1], R[MAX / 2 + 1];
int sum(0);

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] = INFINITY;
  R[n2] = INFINITY;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
      sum++;
    } else {
      A[k] = R[j];
      j++;
      sum++;
    }
  }
}

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

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

  mergeSort(A, 0, n);
  for (int i = 0; i < n; i++) {
    if (i)
      printf(" ");
    printf("%d", A[i]);
  }
  printf("\n%d\n", sum);

  return 0;
}

Fixed submission:
#include <iostream>
#include <stdio.h>

#define MAX 500000
#define INFINITY 1000000000

int n;
int L[MAX / 2 + 1], R[MAX / 2 + 1];
int sum(0);

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] = INFINITY;
  R[n2] = INFINITY;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
      sum++;
    } else {
      A[k] = R[j];
      j++;
      sum++;
    }
  }
}

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

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

  mergeSort(A, 0, n);
  for (int i = 0; i < n; i++) {
    if (i)
      printf(" ");
    printf("%d", A[i]);
  }
  printf("\n%d\n", sum);

  return 0;
}
Problem p02272 (C++)

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

#define INF 123456789;
int L[300000], 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(void) {
  int n, A[600000];
  cnt = 0;

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

  mergeSort(A, 0, n);

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

  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;

#define INF 1234567890;
int L[300000], 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(void) {
  int n, A[600000];
  cnt = 0;

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

  mergeSort(A, 0, n);

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

  return 0;
}
Problem p02272 (C++)

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

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

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN 100000 + 2
int AA[MAXN], TT[MAXN];
int n, g = 0;
void input() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> AA[i];
  }
}
void syf(int *A, int x, int y, int *T) {
  if (y - x > 1) {
    int m = (x + y) / 2;
    int i = x, p = x, q = m;
    syf(A, x, m, T);
    syf(A, m, y, T);
    while (p < m || q < y) {
      if (q >= y || (p < m && A[p] <= A[q]))
        T[i++] = A[p++];
      else
        T[i++] = A[q++];
      g++;
    }
    for (int i = x; i < y; ++i) {
      A[i] = T[i];
      AA[i] = T[i];
    }
  }
}
int main() {
  input();
  syf(AA, 0, n, TT);
  for (int i = 0; i < n - 1; ++i) {
    cout << AA[i] << " ";
  }
  cout << AA[n - 1] << endl;
  cout << g << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN 500000 + 2
int AA[MAXN], TT[MAXN];
int n, g = 0;
void input() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> AA[i];
  }
}
void syf(int *A, int x, int y, int *T) {
  if (y - x > 1) {
    int m = (x + y) / 2;
    int i = x, p = x, q = m;
    syf(A, x, m, T);
    syf(A, m, y, T);
    while (p < m || q < y) {
      if (q >= y || (p < m && A[p] <= A[q]))
        T[i++] = A[p++];
      else
        T[i++] = A[q++];
      g++;
    }
    for (int i = x; i < y; ++i) {
      A[i] = T[i];
      AA[i] = T[i];
    }
  }
}
int main() {
  input();
  syf(AA, 0, n, TT);
  for (int i = 0; i < n - 1; ++i) {
    cout << AA[i] << " ";
  }
  cout << AA[n - 1] << endl;
  cout << g << endl;
  return 0;
}
Problem p02272 (C++)

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

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

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
// 常に心してかかれ
// 簡単なことを簡単にできるように練習する
// びっくりするくらいつまらないことで詰まってることも多い
int n; // 問題で設定された呼び出し階層の深さ
class dot {
public:
  double x;
  double y;

  dot(double X, double Y) {
    x = X;
    y = Y;
  }
};

void makeDots(dot A, dot B, int m) { // m:この関数の呼び出し元の呼び出し階層
  double tmp_x;
  double tmp_y;

  tmp_x = A.x + (B.x - A.x) / 3;
  tmp_y = A.y + (B.y - A.y) / 3;

  dot P(tmp_x, tmp_y);

  tmp_x = sqrt(3.0) * (B.x - A.x) - (B.y - A.y);
  tmp_y = (B.x - A.x) + sqrt(3.0) * (B.y - A.y);
  tmp_x = tmp_x / (2 * sqrt(3.0));
  tmp_y = tmp_y / (2 * sqrt(3.0));
  tmp_x += A.x;
  tmp_y += A.y;

  dot Q(tmp_x, tmp_y);

  tmp_x = A.x + 2 * (B.x - A.x) / 3;
  tmp_y = A.y + 2 * (B.y - A.y) / 3;

  dot R(tmp_x, tmp_y);

  m++;
  if (m == n) {
    cout << A.x << " " << A.y << endl;
    cout << P.x << " " << P.y << endl;
    cout << Q.x << " " << Q.y << endl;
    cout << R.x << " " << R.y << endl;
    return;
  }

  makeDots(A, P, m);
  makeDots(P, Q, m);
  makeDots(Q, R, m);
  makeDots(R, B, m);
}

int main() {
  dot A(0, 0);
  dot B(100, 0);

  cin >> n;

  if (n == 0) {
    cout << "0.00000000 0.00000000" << endl;
    cout << "100.00000000 0.00000000" << endl;
  }
  makeDots(A, B, 0);

  cout << 100 << " " << 0 << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <math.h>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
// 常に心してかかれ
// 簡単なことを簡単にできるように練習する
// びっくりするくらいつまらないことで詰まってることも多い
int n; // 問題で設定された呼び出し階層の深さ
class dot {
public:
  double x;
  double y;

  dot(double X, double Y) {
    x = X;
    y = Y;
  }
};

void makeDots(dot A, dot B, int m) { // m:この関数の呼び出し元の呼び出し階層
  double tmp_x;
  double tmp_y;

  tmp_x = A.x + (B.x - A.x) / 3;
  tmp_y = A.y + (B.y - A.y) / 3;

  dot P(tmp_x, tmp_y);

  tmp_x = sqrt(3.0) * (B.x - A.x) - (B.y - A.y);
  tmp_y = (B.x - A.x) + sqrt(3.0) * (B.y - A.y);
  tmp_x = tmp_x / (2 * sqrt(3.0));
  tmp_y = tmp_y / (2 * sqrt(3.0));
  tmp_x += A.x;
  tmp_y += A.y;

  dot Q(tmp_x, tmp_y);

  tmp_x = A.x + 2 * (B.x - A.x) / 3;
  tmp_y = A.y + 2 * (B.y - A.y) / 3;

  dot R(tmp_x, tmp_y);

  m++;
  if (m == n) {
    cout << A.x << " " << A.y << endl;
    cout << P.x << " " << P.y << endl;
    cout << Q.x << " " << Q.y << endl;
    cout << R.x << " " << R.y << endl;
    return;
  }

  makeDots(A, P, m);
  makeDots(P, Q, m);
  makeDots(Q, R, m);
  makeDots(R, B, m);
}

int main() {
  dot A(0, 0);
  dot B(100, 0);

  cin >> n;

  if (n == 0) {
    cout << "0.00000000 0.00000000" << endl;
    cout << "100.00000000 0.00000000" << endl;
    return 0;
  }
  makeDots(A, B, 0);

  cout << 100 << " " << 0 << endl;
  return 0;
}
Problem p02274 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <iostream>
using namespace std;
#define int long long
int tempmarge[1000100];
int N, A[10000000];

int MargeSort(int x[], int left, int right) {
  int mid, i, j, k, cl, cr, c;
  if (left >= right)
    return 0;
  mid = (left + right) / 2;
  cl = MargeSort(x, left, mid);
  cr = MargeSort(x, mid + 1, right);

  for (i = left; i <= mid; i++)
    tempmarge[i] = x[i];
  for (i = mid + 1, j = right; i <= right; i++, j--)
    tempmarge[i] = x[j];
  i = left;
  j = right;
  c = 0;
  for (k = left; k <= right; k++)
    if (tempmarge[i] <= tempmarge[j])
      x[k] = tempmarge[i++];
    else {
      c += mid + 1 - i;
      x[k] = tempmarge[j--];
    }
  return c + cl + cr;
}

signed main() {

  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  cout << MargeSort(A, 0, N - 1) << endl;
  for (int i = 0; i < N; i++)
    cerr << A[i] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <iostream>
using namespace std;
#define int long long
int tempmarge[1000100];
int N, A[10000000];

int MargeSort(int x[], int left, int right) {
  int mid, i, j, k, cl, cr, c;
  if (left >= right)
    return 0;
  mid = (left + right) / 2;
  cl = MargeSort(x, left, mid);
  cr = MargeSort(x, mid + 1, right);

  for (i = left; i <= mid; i++)
    tempmarge[i] = x[i];
  for (i = mid + 1, j = right; i <= right; i++, j--)
    tempmarge[i] = x[j];
  i = left;
  j = right;
  c = 0;
  for (k = left; k <= right; k++)
    if (tempmarge[i] <= tempmarge[j])
      x[k] = tempmarge[i++];
    else {
      c += mid + 1 - i;
      x[k] = tempmarge[j--];
    }
  return c + cl + cr;
}

signed main() {

  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  cout << MargeSort(A, 0, N - 1) << endl;
  return 0;
}
Problem p02274 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define INF (1000000001)
int N;
int A[(1 << 21) + 1];
int T[(1 << 21) + 1];
ll res = 0;
void marge(int l, int r) {
  if (r - l == 2) {
    if (A[l] > A[r - 1]) {
      res++;
      swap(A[l], A[r - 1]);
    }
  } else {
    int m = (l + r) / 2;
    marge(l, m);
    marge(m, r);
    for (int i = l, j = m, k = l; k < r; k++) {
      if ((j < r && A[i] > A[j]) || i == m) {
        res += (m - i);
        T[k] = A[j++];
      } else {
        T[k] = A[i++];
      }
    }
    for (int i = l; i < r; i++)
      A[i] = T[i];
  }
  /*
  cout <<"( " <<  l << " , "<< r << " ) " << endl;
  for(int i=l;i<r;i++) cout << A[i] << " ";
  cout << endl;
  cout << "res = "<< res << endl;
  */
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int n = 1;
  while (n < N)
    n *= 2;
  for (int i = N; i < n; i++)
    A[i] = INF + i;
  N = n;
  marge(0, N);
  cout << res << endl;
}

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

typedef long long ll;
#define INF (1000000001)
int N;
int A[(1 << 21) + 1];
int T[(1 << 21) + 1];
ll res = 0;
void marge(int l, int r) {
  if (r - l == 2) {
    if (A[l] > A[r - 1]) {
      res++;
      swap(A[l], A[r - 1]);
    }
  } else {
    int m = (l + r) / 2;
    marge(l, m);
    marge(m, r);
    for (int i = l, j = m, k = l; k < r; k++) {
      if ((j < r && A[i] > A[j]) || i == m) {
        res += (m - i);
        T[k] = A[j++];
      } else {
        T[k] = A[i++];
      }
    }
    for (int i = l; i < r; i++)
      A[i] = T[i];
  }
  /*
  cout <<"( " <<  l << " , "<< r << " ) " << endl;
  for(int i=l;i<r;i++) cout << A[i] << " ";
  cout << endl;
  cout << "res = "<< res << endl;
  */
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  if (N == 1)
    cout << 0 << endl;
  else {
    int n = 1;
    while (n < N)
      n *= 2;
    for (int i = N; i < n; i++)
      A[i] = INF + i;
    N = n;
    marge(0, N);
    cout << res << endl;
  }
}