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

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

Error: 0

Fixed submission:

#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;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int s[500000], count = 0;

void margeSort(int left, int right, int mid) {
  int index, i, j = mid, t[500000];
  index = i = left;
  while (i < mid && j < right) {
    if (s[i] < s[j]) {
      t[index++] = s[i++];
    } else {
      t[index++] = s[j++];
    }
  }
  while (j < right)
    t[index++] = s[j++];
  while (i < mid)
    t[index++] = s[i++];
  j = left;
  for (int i = left; i < right; i++, j++) {
    s[i] = t[j];
  }
  count += right - left;
}

void marge(int left, int right) {
  if (right - 1 != left) {
    int mid = (left + right) / 2;
    marge(left, mid);
    marge(mid, right);
    margeSort(left, right, mid);
  }
}

int main(void) {
  int n;

  cin >> n;

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

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

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int s[500000];

int marge(int left, int right) {
  int count = 0;
  if (right - 1 == left) {
    return 0;
  }
  int mid = (left + right) / 2;
  count += marge(left, mid);
  count += marge(mid, right);
  int index, i, j = mid, t[500000];
  index = i = left;
  while (i < mid && j < right) {
    if (s[i] < s[j]) {
      t[index++] = s[i++];
    } else {
      t[index++] = s[j++];
    }
  }
  while (j < right)
    t[index++] = s[j++];
  while (i < mid)
    t[index++] = s[i++];
  j = left;
  for (int i = left; i < right; i++, j++) {
    s[i] = t[j];
  }
  return count + right - left;
}

int main(void) {
  int n;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  int count = marge(0, n);

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

#include <cstdio>
#define SENTINEL 2000000000

int *L, *R;
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 n;
  scanf("%d", &n);

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

  L = new int[int(n / 2 + 1)];
  R = new int[int((n + 1) / 2 + 1)];
  mergeSort(A, n, 0, n);

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

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

  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#define SENTINEL 2000000000

int *L, *R;
int cnt;

void merge(int A[], int n, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  L = new int[int(n / 2 + 1)];
  R = new int[int((n + 1) / 2 + 1)];

  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = R[n2] = 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 n;
  scanf("%d", &n);

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

  mergeSort(A, n, 0, n);

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

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

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

#include <iostream>
int cnt = 0;
void merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int L[500000], R[500000];
  for (int i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }
  L[n1] = 10000000000;
  for (int i = 0; i < n2; i++) {
    R[i] = A[mid + i];
  }
  R[n2] = 10000000000;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
      cnt++;
    } else {
      A[k] = R[j];
      j++;
      cnt++;
    }
  }
}
void mergeSort(int A[], int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(A, left, mid);
    mergeSort(A, mid, right);
    merge(A, left, mid, right);
  }
}
int main() {
  int n, A[500000];
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> A[i];
  }
  mergeSort(A, 0, n);
  for (int i = 0; i < n; i++) {
    if (i != n - 1) {
      std::cout << A[i] << " ";
    } else {
      std::cout << A[i] << "\\n";
    }
  }
  std::cout << cnt << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
int cnt = 0;
void merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int L[100000], R[100000];
  for (int i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }
  L[n1] = 1000000;
  for (int i = 0; i < n2; i++) {
    R[i] = A[mid + i];
  }
  R[n2] = 1000000;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
      cnt++;
    } else {
      A[k] = R[j];
      j++;
      cnt++;
    }
  }
}
void mergeSort(int A[], int left, int right) {
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(A, left, mid);
    mergeSort(A, mid, right);
    merge(A, left, mid, right);
  }
}
int main() {
  int n, A[100000];
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> A[i];
  }
  mergeSort(A, 0, n);
  for (int i = 0; i < n; i++) {
    if (i != n - 1) {
      std::cout << A[i] << " ";
    } else {
      std::cout << A[i] << "\\n";
    }
  }
  std::cout << cnt << "\\n";
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

//
//  main.cpp
//  ALDS1_5_B
//
//  Created by ptyuan on 2018/10/28.
//  Copyright © 2018 Sy. All rights reserved.
//

#include <cstdio>
#include <iostream>
using namespace std;

const int kMaxValue = 2000000000;
const int kMax = 500000;

int A[kMax] = {0};

int L[kMax / 2 + 2];
int R[kMax / 2 + 2];
int cmpCount = 0;

void _merge(int *A, int n, int left, int mid, int right) {
  int n1 = mid - left;
  for (int i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }

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

  L[n1] = kMaxValue;
  R[n2] = kMaxValue;
  int l1 = 0;
  int l2 = 0;
  for (int k = left; k < right; k++) {
    ++cmpCount;
    if (L[l1] <= R[l2]) {
      A[k] = L[l1++];
    } else {
      A[k] = R[l2++];
    }
  }
}

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

int main(int argc, const char *argv[]) {
  int n = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", A + i);
  }

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

  return 0;
}


Error: -11

Fixed submission:

//
//  main.cpp
//  ALDS1_5_B
//
//  Created by ptyuan on 2018/10/28.
//  Copyright © 2018 Sy. All rights reserved.
//

#include <cstdio>
#include <iostream>
using namespace std;

const long kMaxValue = 10 ^ 9 + 1;
const int kMax = 500000;

int A[kMax] = {0};

int L[kMax / 2 + 2];
int R[kMax / 2 + 2];
int cmpCount = 0;

void _merge(int *A, int n, int left, int mid, int right) {
  int n1 = mid - left;
  for (int i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }

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

  L[n1] = kMaxValue;
  R[n2] = kMaxValue;
  int l1 = 0;
  int l2 = 0;
  for (int k = left; k < right; k++) {
    ++cmpCount;
    if (L[l1] < R[l2]) {
      A[k] = L[l1++];
    } else {
      A[k] = R[l2++];
    }
  }
}

void mergeSort(int *A, int n, int left, int right) {
  if (left < right) {
    int mid = (left + right) / 2;
    mergeSort(A, n, left, mid);
    mergeSort(A, n, mid, right);
    _merge(A, n, left, mid, right);
  }
}

int main(int argc, const char *argv[]) {
  int n = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", A + i);
  }

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

  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: 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 < 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;
}

Error: TLE

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 < 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;
}
Problem p02272 (C++) — buggy submission, verdict: 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(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;
}


Error: 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(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;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
const int inf = 2e9;
const int N = 500100;

int n, a[N], sum;
int L[N / 2 + 2], R[N / 2 + 2];

/******???a[1]??°a[n]????°???°??§**************/
void Merge(int a[], int p, int q, int r) {
  int i, j, n1 = q - p + 1, n2 = r - q;
  for (i = 1; i <= n1; i++)
    L[i] = a[p + i - 1];
  for (j = 1; j <= n2; j++)
    R[j] = a[q + j];
  L[n1 + 1] = inf;
  R[n2 + 1] = inf;
  i = j = 1;
  for (int k = p; k <= r; k++) {
    sum++;
    if (L[i] <= R[j]) {
      a[k] = L[i];
      i++;
    } else {
      a[k] = R[j];
      j++;
    }
  }
}
void MergeSort(int a[], int p, int r) {
  int q;
  if (p < r) {
    q = (p + r) / 2;
    MergeSort(a, p, q);
    MergeSort(a, q + 1, r);
    Merge(a, p, q, r);
  }
}
/*************************/
void print(int a[], int n) {
  for (int i = 1; i <= n; i++) {
    cout << a[i];
    if (i != n)
      cout << " ";
  }
  cout << endl;
}
int main() {
  int n;
  sum = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  MergeSort(a, 1, n);
  print(a, n);
  cout << sum << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
const int inf = 100000000;
const int N = 50010;

int n, a[N], sum;
int L[N / 2 + 1], R[N / 2 + 1];

/******???a[1]??°a[n]????°???°??§**************/
void Merge(int a[], int p, int q, int r) {
  int i, j, n1 = q - p + 1, n2 = r - q;
  for (i = 1; i <= n1; i++)
    L[i] = a[p + i - 1];
  for (j = 1; j <= n2; j++)
    R[j] = a[q + j];
  L[n1 + 1] = inf;
  R[n2 + 1] = inf;
  i = j = 1;
  for (int k = p; k <= r; k++) {
    sum++;
    if (L[i] <= R[j]) {
      a[k] = L[i];
      i++;
    } else {
      a[k] = R[j];
      j++;
    }
  }
}
void MergeSort(int a[], int p, int r) {
  int q;
  if (p < r) {
    q = (p + r) / 2;
    MergeSort(a, p, q);
    MergeSort(a, q + 1, r);
    Merge(a, p, q, r);
  }
}
/*************************/
void print(int a[], int n) {
  for (int i = 1; i <= n; i++) {
    cout << a[i];
    if (i != n)
      cout << " ";
  }
  cout << endl;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  MergeSort(a, 1, n);
  print(a, n);
  cout << sum << endl;
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

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


Error: 0

Fixed submission:

#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";
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

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

Error: 0

Fixed submission:

#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;
}
Problem p02272 (C++) — buggy submission, verdict: 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++;
  }
  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;
}

Error: MLE

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

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