alexjercan/bugnet huggingface dataset:
Problem p02272 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

long long cnt;

void print(vector<int> &data) {
  for (int i = 0; i < data.size(); ++i) {
    cout << data[i];
    if (i == data.size() - 1)
      cout << endl;
    else
      cout << " ";
  }
}

vector<int> tmp;

void helper(vector<int> &data, int l, int m, int r) {
  for (int i = l; i < r; ++i)
    tmp[i] = data[i];

  int p1(l), p2(m), p(l);
  while (p1 < m && p2 < r) {
    if (tmp[p1] < tmp[p2])
      data[p++] = tmp[p1++];
    else
      data[p++] = tmp[p2++];
  }

  while (p1 < m)
    data[p++] = tmp[p1++];
  while (p2 < r)
    data[p++] = tmp[p2++];
  cnt += (r - l);
}

void mergeSort(vector<int> &data, int l, int r) {
  if (l + 1 < r) {
    int m = l + (r - l) / 2;
    mergeSort(data, l, m);
    mergeSort(data, m, r);
    helper(data, l, m, r);
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> data(n);
  for (int i = 0; i < n; ++i)
    cin >> data[i];

  tmp.resize(data.size());
  mergeSort(data, 0, data.size());
  print(data);
  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

long long cnt;

void print(vector<int> &data) {
  for (int i = 0; i < data.size(); ++i) {
    cout << data[i];
    if (i == data.size() - 1)
      cout << endl;
    else
      cout << " ";
  }
}

void helper(vector<int> &data, int l, int m, int r) {
  vector<int> tmp(data);

  int p1(l), p2(m), p(l);
  while (p1 < m && p2 < r) {
    if (tmp[p1] < tmp[p2])
      data[p++] = tmp[p1++];
    else
      data[p++] = tmp[p2++];
  }

  while (p1 < m)
    data[p++] = tmp[p1++];
  while (p2 < r)
    data[p++] = tmp[p2++];
  cnt += (r - l);
}

void mergeSort(vector<int> &data, int l, int r) {
  if (l + 1 < r) {
    int m = l + (r - l) / 2;
    mergeSort(data, l, m);
    mergeSort(data, m, r);
    helper(data, l, m, r);
  }
}

int main() {
  int n;
  cin >> n;
  vector<int> data(n);
  for (int i = 0; i < n; ++i)
    cin >> data[i];

  mergeSort(data, 0, data.size());
  print(data);
  cout << cnt << endl;

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

#include <iostream>
using namespace std;

int L[250002], R[250002], cnt; // 分割したものをL,Rとする

// 統治
void merge(int A[], int left, int mid, int right, int n) {
  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] = 2000000000; // 番兵
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    cnt++; // 比較回数をカウント
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i = i + 1;
    } else {
      A[k] = R[j];
      j = j + 1;
    }
  }
}

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

int main() {
  int A[600000], n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];

  mergeSort(A, 0, n, n);

  for (int i = 0; i < n - 1; i++)
    cout << A[i] << " ";
  cout << A[n - 1] << endl;
  cout << cnt << endl; // 比較回数を出力
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int L[300000], R[300000], cnt; // 分割したものをL,Rとする

// 統治
void merge(int A[], int left, int mid, int right, int n) {
  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] = 100000000; // 番兵
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    cnt++; // 比較回数をカウント
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i = i + 1;
    } else {
      A[k] = R[j];
      j = j + 1;
    }
  }
}

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

int main() {
  int A[600000], n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];

  mergeSort(A, 0, n, n);

  for (int i = 0; i < n - 1; i++)
    cout << A[i] << " ";
  cout << A[n - 1] << endl;
  cout << cnt << endl; // 比較回数を出力
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

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

Error: 0

Fixed submission:

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

#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 500000;
const int INFTY = 1000000010;
int cnt = 0;
int L[maxn / 2 + 2], R[maxn / 2 + 2];
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] = R[n2] = INFTY;

  int i = 0, 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 Merge_sort(int *A, int left, int right) // 归并排序
{
  if (left < right - 1) {
    // int mid = (right - left)/2 + left;
    int mid = (right + left) / 2;
    Merge_sort(A, left, mid);   // 分治递归
    Merge_sort(A, mid, right);  // 分治递归
    Merge(A, left, mid, right); // 归并
  }
}
int main() {
  // freopen("guibing1.txt","r",stdin);
  int num = 0;
  int A[maxn];
  scanf("%d", &num);
  for (int i = 0; i < num; i++) {
    scanf("%d", &A[i]);
  }
  // int left = 0,right = num;
  Merge_sort(A, 0, num);
  for (int i = 0; i < num; i++) {
    if (i > 0) {
      printf(" ");
    }
    printf("%d", A[i]);
  }
  printf("\\n");
  printf("%d\\n", cnt);
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int ans = 0;

void merge(int *a, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int l[10000], r[10000];
  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] = 123456789;
  r[n2] = 123456789;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    ans++;
    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 n;
  int s[50000];
  int i, j;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &s[i]);
  }
  mergesort(s, 0, n);
  for (i = 0; i < n; i++) {
    if (i != 0)
      printf(" ");
    printf("%d", s[i]);
  }
  printf("\\n%d\\n", ans);

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

#include <iostream>

using namespace std;

int cnt = 0;
void merge(int a[], int left, int right) {
  int i = left;
  int mid = left + (right - left) / 2;
  int j = mid + 1;
  int k = 0;
  int temporary[500100];

  while (i <= mid && j <= right) {
    if (a[i] <= a[j]) {
      cnt++;
      temporary[k] = a[i];
      i++;
    } else {
      cnt++;
      temporary[k] = a[j];
      j++;
    }
    k++;
  }
  while (i <= mid) {
    cnt++;
    temporary[k] = a[i];
    i++;
    k++;
  }
  while (j <= right) {
    cnt++;
    temporary[k] = a[j];
    j++;
    k++;
  }

  for (int i = 0; i < k; i++) {
    a[left + i] = temporary[i];
  }
}
void mergeSort(int a[], int left, int right) {
  if (left < right) {
    int mid = (left + right) / 2;

    mergeSort(a, left, mid);
    mergeSort(a, mid + 1, right);
    merge(a, left, right);
  }
}

int main() {
  int n;

  cin >> n;

  int a[n];

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

  mergeSort(a, 0, n - 1);

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

  cout << cnt << endl;
  return 0;
}


Error: WA

Fixed submission:


#include <bits/stdc++.h>

#define x first
#define y second
#define mp make_pair
// everything goes according to my plan
#define pb push_back
#define sz(a) (int)(a.size())
#define vec vector
// shimkenttin kyzdary, dzyn, dzyn, dzyn...
#define y1 Y_U_NO_y1
#define left Y_U_NO_left
#define right Y_U_NO_right

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

const int Mod = (int)1e9 + 7;
const int MX = 1073741822;
const ll MXLL = 4e18;
const int Sz = 1110111;
// a pinch of soul
inline void Read_rap() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
inline void randomizer3000() {
  unsigned int seed;
  asm("rdtsc" : "=A"(seed));
  srand(seed);
}
void files(string problem) {
  if (fopen((problem + ".in").c_str(), "r")) {
    freopen((problem + ".in").c_str(), "r", stdin);
    freopen((problem + ".out").c_str(), "w", stdout);
  }
}
void localInput(const char in[] = "s") {
  if (fopen(in, "r")) {
    freopen(in, "r", stdin);
  } else
    cerr << "Warning: Input file not found" << endl;
}

void merge(int *cl, int *cr, int *al, int *ar, int *bl, int *br) {
  static vec<int> c;
  c.clear();
  while (al != ar || bl != br) {
    if (al == ar)
      c.pb(*bl++);
    else if (bl == br)
      c.pb(*al++);
    else if (*al < *bl)
      c.pb(*al++);
    else
      c.pb(*bl++);
  }
  for (int x : c) {
    *cl = x;
    cl++;
  }
}

void merge_sort(int *l, int *r) {
  if (l + 1 == r)
    return;
  int *m = l + (r - l) / 2;
  merge_sort(l, m);
  merge_sort(m, r);
  merge(l, r, l, m, m, r);
}
inline void taskB() {
  int a[Sz];
  int n = 0;

  for (int x; cin >> x;) {
    a[++n] = x;
  }
  merge_sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; i++)
    cout << a[i] << ' ';
}

int main() {
#ifdef Local
  localInput("s.txt");
#endif
  Read_rap();
  taskB();

  return 0;
}
//

// Coded by Z..
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int count = 0;
void merge(int arr[], int l, int m, int r) {
  int i, j, k;
  int n1 = m - l + 1;
  int n2 = r - m;

  int L[n1], R[n2];

  for (i = 0; i < n1; i++)
    L[i] = arr[l + i];
  for (j = 0; j < n2; j++)
    R[j] = arr[m + 1 + j];

  i = 0;
  j = 0;
  k = l;
  while (i < n1 && j < n2) {
    count++;
    if (L[i] <= R[j]) {
      arr[k] = L[i];
      i++;
    } else {
      arr[k] = R[j];
      j++;
    }
    k++;
  }

  while (i < n1) {
    arr[k] = L[i];
    i++;
    k++;
    count++;
  }

  while (j < n2) {
    arr[k] = R[j];
    j++;
    k++;
    count++;
  }
}

void mergeSort(int arr[], int l, int r) {
  if (l < r) {
    int m = l + (r - l) / 2;

    mergeSort(arr, l, m);
    mergeSort(arr, m + 1, r);

    merge(arr, l, m, r);
  }
}

int main() {

  int n;

  cin >> n;

  int arr[n];

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

  mergeSort(arr, 0, n - 1);

  for (int i = 0; i < n; i++) {
    if (i == n - 1)
      cout << arr[i];
    else
      cout << arr[i] << " ";
  }

  cout << endl;
  cout << count;
  cout << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>

using namespace std;
int cnt = 0;

void Merge(int a[], int low, int high, int mid) {
  int i, j, k = 0, temp[high - low + 1];
  i = low;
  j = mid + 1;
  while (i <= mid && j <= high) {
    if (a[i] < a[j])
      temp[k++] = a[i++];
    else
      temp[k++] = a[j++];

    cnt++;
  }
  while (i <= mid) {
    temp[k++] = a[i++];
    cnt++;
  }

  while (j <= high) {
    temp[k++] = a[j++];
    cnt++;
  }

  for (i = low; i <= high; i++)
    a[i] = temp[i - low];
}
void MergeSort(int a[], int low, int high) {
  int mid;
  if (low < high) {
    mid = (low + high) / 2;

    MergeSort(a, low, mid);
    MergeSort(a, mid + 1, high);

    Merge(a, low, high, mid);
  }
}

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

  MergeSort(a, 0, n - 1);
  for (int i = 0; i < n - 1; ++i)
    cout << a[i] << " ";
  cout << a[n - 1] << endl;
  cout << cnt << endl;
}
Problem p02272 (C++) — buggy submission, verdict: Runtime Error

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


Error: 0

Fixed submission:

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

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

using namespace std;

const int maxn = 500100;

int t[maxn], a[maxn];

int c = 0;

void merge_sort(int *a, int x, int y, int *t) {
  if (y - x > 1) {
    int m = (x + y) / 2;
    int p = x, q = m, i = x;
    merge_sort(a, x, m, t);
    merge_sort(a, m, y, t);
    while (p < m || q < y) {
      if (q >= y || (p < m && a[p] <= a[q])) {
        c++;
        t[i++] = a[p++];
      } else {
        c++;
        t[i++] = a[q++];
      }
    }
    for (i = x; i < y; i++)
      a[i] = t[i];
  }
}

int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    int l = 0, r = n;
    merge_sort(a, l, r, t);
    for (int i = 0; i < n; i++) {
      if (i != 0)
        printf(" ");
      printf("%d", a[i]);
    }
    printf("\\n");
    printf("%d\\n", c);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int t = 0;

void mergee(int arr[], int l, int m, int r) {
  int i, j, k;
  int n1 = m - l + 1;
  int n2 = r - m;
  int Lrr[n1], Rrr[n2];
  for (i = 0; i < n1; i++) {
    Lrr[i] = arr[l + i];
  }
  for (j = 0; j < n2; j++) {
    Rrr[j] = arr[m + 1 + j];
  }
  i = 0;
  j = 0;
  k = l;
  while (i < n1 && j < n2) {
    if (Lrr[i] <= Rrr[j]) {
      t++;
      arr[k] = Lrr[i];
      i++;
    } else {
      t++;
      arr[k] = Rrr[j];
      j++;
    }
    k++;
  }

  while (i < n1) {
    t++;
    arr[k] = Lrr[i];
    i++;
    k++;
  }

  while (j < n2) {
    t++;
    arr[k] = Rrr[j];
    j++;
    k++;
  }
}

void merge_Sort(int arr[], int l, int r) {
  if (l < r) {
    int m = (l + r) / 2;
    merge_Sort(arr, l, m);
    merge_Sort(arr, m + 1, r);
    mergee(arr, l, m, r);
  }
}

int main() {
  int n;
  cin >> n;
  int arr[100000];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  merge_Sort(arr, 0, n - 1);
  for (int i = 0; i < n - 1; i++) {
    cout << arr[i] << " ";
  }
  cout << arr[n - 1] << endl;
  cout << t << endl;
  return 0;
}
Problem p02272 (C++) — buggy submission, verdict: 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) / 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;
}


Error: 0

Fixed submission:

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


Error: TLE

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

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

Error: 0

Fixed submission:

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