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

#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

int A[200000];

long long int merge(int a1[], int a2[], int a[], int n, int m) {
  int i = 0, j = 0;
  long long int inv = 0;
  while (i < n || j < m) {
    if (i == n) {
      a[i + j] = a2[j];
      j++;
    } else if (j == m) {
      a[i + j] = a1[i];
      i++;
    } else {
      if (a1[i] < a2[j]) {
        a[i + j] = a1[i];
        i++;
      } else {
        a[i + j] = a2[j];
        j++;
        inv += n - i;
      }
    }
  }
  return inv;
}

long long int mergeSort(int a[], int l) {
  if (l > 1) {
    long long int inv = 0;
    int m = l / 2;
    int n = l - m;
    int *a1 = new int[m];
    int *a2 = new int[n];
    for (int i = 0; i < m; i++) {
      a1[i] = a[i];
    }
    for (int i = 0; i < n; i++) {
      a2[i] = a[m + i];
    }

    inv += mergeSort(a1, m);
    inv += mergeSort(a2, n);
    inv += merge(a1, a2, a, m, n);
    delete[] a1;
    delete[] a2;
    return inv;
  }
  return 0;
}

int main() {
  int N;
  scanf("%d", &N);

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

  printf("%lld\\n", mergeSort(A, N));
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

int A[200000];

int main() {
  int n;

  scanf("%d", &n);

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

  int counter = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (A[i] > A[j]) {
        counter++;
      }
    }
  }

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

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
#define MAX_N 200010
#define int long long

int bit[MAX_N], n, a[MAX_N], ma, b[MAX_N];

int sum(int i) {
  int s = 0;

  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}

void add(int i, int x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

signed main() {
  int ans = 0;
  map<int, int> ma;
  cin >> n;

  for (int i = 0; i < n; i++) {
    scanf("%d", &b[i]);
    b[i]++;
    a[i] = b[i];
  }
  sort(b, b + n);
  for (int i = 0; i < n; i++)
    ma[b[i]] = i + 1;
  for (int i = 0; i < n; i++)
    a[i] = ma[a[i]];

  for (int i = 0; i < n; i++) {
    ans += i - sum(a[i]);
    add(a[i], 1);
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
#define MAX_N 400010
#define int long long

int bit[MAX_N], n, a[MAX_N], ma;

int sum(int i) {
  int s = 0;

  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}

void add(int i, int x) {
  while (i <= ma) {
    bit[i] += x;
    i += i & -i;
  }
}

signed main() {
  int ans = 0;

  cin >> n;

  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    a[i]++;
    ma = max(a[i], ma);
  }
  for (int i = 0; i < n; i++) {
    ans += i - sum(a[i]);
    add(a[i], 1);
  }
  cout << ans << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

#define MAX 200000
#define SENTINEL 1200000000

typedef long long hyper;
hyper cnt = 0;

void Merge(int *A, int left, int mid, int right) {
  int R[MAX], L[MAX];
  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] = SENTINEL;
  R[n2] = SENTINEL;
  i = j = 0;

  // sorting.
  for (k = left; k < right; k++) {

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

    else {
      A[k] = R[j];
      j++;
      cnt = cnt + n1 - i;
    } // add n1 - i.
  }
}

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 i, n, A[MAX];
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> A[i];

  MergeSort(A, 0, n);
  cout << cnt << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

#define MAX 200
#define SENTINEL 1200000

typedef long long hyper;

hyper Merge(int *A, int left, int mid, int right) {
  int R[MAX], L[MAX];
  int i, j, k, n1, n2, v;
  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] = SENTINEL;
  R[n2] = SENTINEL;
  i = j = 0;

  // sorting.
  v = 0;
  for (k = left; k < right; k++) {

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

    else {
      A[k] = R[j];
      j++;
      v = v + n1 - i;
    } // add n1 - i.
  }
  return v;
}

hyper MergeSort(int *A, int left, int right) {
  int mid;
  int v1, v2, v3;
  if (left + 1 < right) {
    mid = (left + right) / 2;
    v1 = MergeSort(A, left, mid);
    v2 = MergeSort(A, mid, right);
    v3 = Merge(A, left, mid, right);
    return v1 + v2 + v3;
  }
  return 0;
}

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

  cout << MergeSort(A, 0, n) << endl;

  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

// void solve(){
//	long long n, cnt = 0;
//	cin >> n;
//	vector<int> a(n), b(n);
//	for (int i = 0; i < n; i++) cin >> a[i];
//	b = a;
//	sort(b.begin(), b.end());
//	for (int i = n - 1; i >= 0; i--){
//		vector<int>::iterator iter = find(a.begin(), a.end(), b[i]);
//		int index = distance(a.begin(), iter);
//		a.erase(iter);
//		cnt += i - index;
//	}
//	cout << cnt << endl;
// }

long long merge(vector<int> &A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  vector<int> L(n1 + 1), R(n2 + 1);
  for (int i = 0; i < n1; i++)
    L[i] = A[left + i];
  for (int i = 0; i < n2; i++)
    R[i] = A[mid + i];
  L[n1] = R[n2] = 1000000001;
  long long i = 0, j = 0, cnt = 0;
  for (int k = left; k < right; k++) {
    // cnt++;
    if (L[i] <= R[j])
      A[k] = L[i++];
    else {
      cnt += n1 - i;
      A[k] = R[j++];
    }
  }
  return cnt;
}

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

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

signed main() { solve(); }

Error: TLE

Fixed submission:

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

using namespace std;

void solve() {
  long long n, cnt = 0;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  b = a;
  sort(b.begin(), b.end());
  for (int i = n - 1; i >= 0; i--) {
    vector<int>::iterator iter = find(a.begin(), a.end(), b[i]);
    int index = distance(a.begin(), iter);
    a.erase(iter);
    cnt += i - index;
  }
  cout << cnt << endl;
}

signed main() { solve(); }
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <iostream>
#include <queue>

#define INF 922337203685477580
typedef long long ll;

using namespace std;

ll cnt = 0LL;

void merge(ll *A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  ll L[n1 + 10];
  ll R[n2 + 10];

  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++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else {
      A[k] = R[j];
      cnt += n1 - i;
      j++;
    }
  }
}

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

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

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

  mergeSort(A, 0, n);

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <queue>

#define INF 922337203685477580
typedef long long ll;

using namespace std;

// TODO
// ????????±???????????????????????????????????¢?????´??????????????????
int bubbleSort(ll *A, int n) {
  int cnt = 0LL;
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j >= i + 1; j--) {
      if (A[j] < A[j - 1]) {
        ll a = A[j];
        A[j] = A[j - 1];
        A[j - 1] = a;

        cnt++;
      }
    }
  }
  return cnt;
}

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

  ll A[n];
  ll B[n]; // sorted array
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    B[i] = A[i];
  }
  sort(B, B + n);

  // count inversions
  // ll cnt = 0;
  // for (int i = 0; i < n; i++) {
  //   ll key = A[i];
  //
  //   // search the index of 'key' in B
  //   ll* pos = lower_bound(B, B + n, key);
  //   ll idx = distance(B, pos);
  //   assert(key == *pos);
  //
  //   // defference is the inversions of key
  //   cnt += max(idx - i, 0LL);
  // }

  int cnt = bubbleSort(A, n);

  cout << cnt << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long lli;

lli rec(int l, int r, vector<int> &A) {
  if (r - l <= 1)
    return 0;
  int m = (l + r) / 2;
  lli res = rec(l, m, A) + rec(m, r, A);
  queue<int> a, b;
  for (int i = l; i < m; ++i)
    a.push(A[i]);
  for (int i = m; i < r; ++i)
    b.push(A[i]);
  int k = l;
  while (a.size() && b.size()) {
    if (a.front() <= b.front()) {
      A[k++] = a.front();
      a.pop();
    } else {
      A[k++] = b.front();
      b.pop();
      res += a.size();
    }
  }
  while (a.size()) {
    A[k++] = a.front();
    a.pop();
  }
  while (b.size()) {
    A[k++] = b.front();
    b.pop();
  }
  return res;
}

int main() {
  int n;
  vector<int> A;
  cin >> n;
  A.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> A[i];
  cout << rec(0, n, A) << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int rec(int l, int r, vector<int> &A) {
  if (r - l <= 1)
    return 0;
  int n = A.size();
  int m = (l + r) / 2;
  int res = rec(l, m, A) + rec(m, r, A);
  queue<int> a, b;
  for (int i = l; i < m; ++i)
    a.push(A[i]);
  for (int i = m; i < r; ++i)
    b.push(A[i]);
  int k = l;
  while (a.size() && b.size()) {
    if (a.front() <= b.front()) {
      A[k++] = a.front();
      a.pop();
    } else {
      A[k++] = b.front();
      b.pop();
      res += a.size();
    }
  }
  while (a.size()) {
    A[k++] = a.front();
    a.pop();
  }
  while (b.size()) {
    A[k++] = b.front();
    b.pop();
  }
  return res;
}

int main() {
  int n;
  vector<int> A;
  cin >> n;
  A.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> A[i];
  cout << rec(0, n, A) << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

long long int mergecount(std::vector<int> &a) {
  long long int count = 0;
  int n = a.size();
  if (n > 1) {
    std::vector<int> b(a.begin(), a.begin() + n / 2);
    std::vector<int> c(a.begin() + n / 2, a.end());
    count += mergecount(b);
    count += mergecount(c);
    for (int i = 0, j = 0, k = 0; i < n; ++i)
      if (k == c.size())
        a[i] = b[j++];
      else if (j == b.size())
        a[i] = c[k++];
      else if (b[j] <= c[k])
        a[i] = b[j++];
      else {
        a[i] = c[k++];
        count += n / 2 - j;
      }
  }
  return count;
}

int main() {
  int n;
  std::cin >> n;
  std::vector<int> a(n, 0);

  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
  }
  std::cout << mergecount(a) << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
int main(void) {
  std::ios::sync_with_stdio(false);
  int n;
  std::cin >> n;
  std::vector<int> A(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> A[i];
  }
  int cnt = 0;
  for (unsigned int i = 0; i < A.size(); ++i) {
    for (unsigned int j = A.size() - 1; j >= i + 1; --j) {
      if (A[j] < A[j - 1]) {
        std::swap(A[j], A[j - 1]);
        cnt++;
      }
    }
  }
  std::cout << cnt << std::endl;
  // system("pause");
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
#define INF 2000000000
using namespace std;

long merge(int *A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int *L = new int[n1 + 1];
  int *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] = INF;
  R[n2] = INF;
  int i = 0;
  int j = 0;
  long count = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else { // L > R の時でかつ、i < n1 - 1 の場合順序の置き換えが発生している
      count += n1 - i;
      A[k] = R[j];
      j++;
    }
  }
  // delete[] L,R;
  return count;
}

long mergeSort(int *A, int left, int right) {
  long totalCount = 0;
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    totalCount += mergeSort(A, left, mid);
    totalCount += mergeSort(A, mid, right);
    totalCount += merge(A, left, mid, right);
  }
  return totalCount;
}

int main() {
  int n;
  cin >> n;
  int *A = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  long count = 0;
  count = mergeSort(A, 0, n);
  cout << count << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int inversionNumber(int *targetArray, int n) {
  int count = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (targetArray[i] > targetArray[j])
        count++;
    }
  }
  return count;
}

int main() {
  int n;
  cin >> n;
  int *A = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  cout << inversionNumber(A, n) << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

#define SENTINEL 1000000001

using namespace std;

long long cnt = 0;

void merge(vector<int> &A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  vector<int> L(n1 + 1), R(n2 + 1);
  for (int i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }
  for (int i = 0; i < n2; i++) {
    R[i] = A[mid + i];
  }
  L[n1] = SENTINEL;
  R[n2] = SENTINEL;
  int i = 0;
  int j = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i++;
    } else {
      A[k] = R[j];
      cnt += n1 - i; // L[]は昇順に整列されているので、i番目以降のL[]の要素(n1 -
                     // i)個はすべてR[j]より大きくなる
      j++;
    }
  }
}

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

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

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


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int *a;
int n;
int cnt = 0;

void compare() {
  for (int k = 1; k < n; k++) {
    for (int i = 0; i < n - k; i++) {
      if (a[i] > a[i + k])
        cnt++;
    }
  }
}

int main() {
  cin >> n;
  a = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  compare();
  cout << cnt << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

#define MAX 200000
#define SENTINEL 2000000000

typedef long long llong;

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

llong merge(int A[], int n, int left, int mid, int right) {
  int i, j, k;
  llong cnt = 0;
  int n1 = mid - left;
  int n2 = right - mid;

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

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

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

  return cnt;
}

llong mergeSort(int A[], int n, int left, int right) {
  int mid;
  llong v1, v2, v3;

  if (left + 1 < right) {
    mid = (left + right) / 2;
    v1 = mergeSort(A, n, left, mid);
    v2 = mergeSort(A, n, mid, right);
    v3 = merge(A, n, left, mid, right);
    return v1 + v2 + v3;
  } else {
    return 0;
  }
}

int main() {
  int *A, n;

  cin >> n;
  A = new int[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];

  llong ans = mergeSort(A, n, 0, n);
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

void printv(int v[], int len) {
  for (int i = 0; i < len; i++)
    cout << " " << v[i];
  cout << endl;
}

int bubbleSort(int v[], int len) {
  int cnt = 0;

  printv(v, len);
  for (int i = 0; i < len; i++)
    for (int j = len - 1; j > i; j--)
      if (v[j] < v[j - 1]) {
        cout << v[j - 1] << ", " << v[j] << endl;
        swap(v[j], v[j - 1]);
        cnt++;
      }

  return cnt;
}

int inversions(int v[], int len) {
  int cnt = 0;

  for (int i = len - 1; i > 0; i--) {
    for (int j = 0; j < i; j++)
      if (v[i] < v[j])
        cnt++;
  }

  return cnt;
}

int main() {
  int n;
  int *p;

  cin >> n;
  p = new int[n];
  for (int i = 0; i < n; i++)
    cin >> p[i];
  cout << inversions(p, n) << endl;

  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 *  http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_5_D
 */
#include <iostream>
#include <vector>

using namespace std;

pair<long, vector<int>> mergeandcount(const vector<int> A,
                                      const vector<int> B) {
  auto itA = A.cbegin();
  auto itB = B.cbegin();
  long count = 0;
  vector<int> C;
  while (itA != A.end() && itB != B.end()) {
    if (*itB < *itA) {
      C.push_back(*itB);
      count += distance(itA, A.end());
      advance(itB, 1);
    } else {
      C.push_back(*itA);
      advance(itA, 1);
    }
  }
  if (itA != A.end()) {
    C.insert(C.end(), itA, A.end());
  } else {
    C.insert(C.end(), itB, B.end());
  }
  return make_pair(count, C);
}

pair<long, vector<int>> sortandcount(const vector<int> L) {
  if (L.size() == 1) {
    return make_pair(0, L);
  }

  int m = L.size() / 2;
  vector<int> A(L.cbegin(), L.cbegin() + m);
  vector<int> B(L.cbegin() + m, L.cend());
  pair<long, vector<int>> rA = sortandcount(A);
  pair<long, vector<int>> rB = sortandcount(B);
  pair<long, vector<int>> rC = mergeandcount(rA.second, rB.second);
  return make_pair(rA.first + rB.first + rC.first, rC.second);
}

int main() {
  ios_base::sync_with_stdio(false);

  int n;
  cin >> n;
  cin.ignore();

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

  cout << sortandcount(A).first << endl;

  return 0;
}

Error: TLE

Fixed submission:

/**
 *  http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_5_D
 */
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

int bubbleSort(vector<int> &a) {
  int cnt = 0;
  for (int i = 0; i < a.size(); i++) {
    for (int j = a.size() - 1; j > i; j--) {
      if (a[j] < a[j - 1]) {
        swap(a[j], a[j - 1]);
        cnt++;
      }
    }
  }
  return cnt;
}

int main() {
  ios_base::sync_with_stdio(false);

  int n;
  cin >> n;
  cin.ignore();

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

  cout << bubbleSort(A) << endl;

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

#include <iostream>
using namespace std;
void merge(int[], int, int, int);
void mergeSort(int[], int, int);
long long cnt = 0;

int main() {
  int n;
  int A[200001];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  mergeSort(A, 0, n);
  for (int i = 0; i < n; i++) {
    // cerr << A[i] << " ";
  }
  // cerr << endl;
  cout << cnt << endl;
  return 0;
}

void merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int L[n1 + 1], R[n2 + 1];
  for (int i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }
  for (int i = 0; i < n2; i++) {
    R[i] = A[mid + i];
  }
  L[n1] = 2000000000;
  R[n2] = 2000000000;
  int l = 0;
  int r = 0;
  for (int k = left; k < right; k++) {
    if (L[l] <= R[r]) {
      A[k] = L[l];
      l++;
    } else {
      A[k] = R[r];
      int k0 = k - left;
      int lr = n1 + r;
      if (lr > k0)
        cnt += lr - k0;
      r++;
    }
  }
}

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

Error: 0
1 2 3 4 5 


Fixed submission:

#include <iostream>
using namespace std;
void merge(int[], int, int, int);
void mergeSort(int[], int, int);
int cnt = 0;

int main() {
  int n;
  int A[200001];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  mergeSort(A, 0, n);
  for (int i = 0; i < n; i++) {
    cerr << A[i] << " ";
  }
  cerr << endl;
  cout << cnt << endl;
  return 0;
}

void merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int L[n1 + 1], R[n2 + 1];
  for (int i = 0; i < n1; i++) {
    L[i] = A[left + i];
  }
  for (int i = 0; i < n2; i++) {
    R[i] = A[mid + i];
  }
  L[n1] = 2000000000;
  R[n2] = 2000000000;
  int l = 0;
  int r = 0;
  for (int k = left; k < right; k++) {
    if (L[l] <= R[r]) {
      A[k] = L[l];
      l++;
    } else {
      A[k] = R[r];
      int k0 = k - left;
      int lr = n1 + r;
      if (lr > k0)
        cnt += lr - k0;
      r++;
    }
  }
}

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