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

#include <iostream>
#include <vector>

unsigned long long merge(std::vector<int>::iterator left,
                         std::vector<int>::iterator mid,
                         std::vector<int>::iterator right) {

  unsigned long long cnt = 0;
  std::vector<int> L(mid - left), R(right - mid);

  for (std::vector<int>::iterator i = left; i != mid; i++)
    L[i - left] = *i;
  L.push_back(1000000000);
  for (std::vector<int>::iterator i = mid; i != right; i++)
    R[i - mid] = *i;
  R.push_back(1000000000);

  int ri = 0, li = 0;
  for (int k = 0; k < right - left; k++) {
    if (L[li] > R[ri]) {
      *(left + k) = R[ri];
      if ((mid + ri) > (left + k))
        cnt += (mid + ri) - (left + k);
      ri += 1;
    } else {
      *(left + k) = L[li];
      li += 1;
    }
  }
  return cnt;
}

unsigned long long mergeSort(std::vector<int>::iterator left,
                             std::vector<int>::iterator right) {
  unsigned long long cnt = 0;
  if (right - left > 1) {
    int num = right - left;
    std::vector<int>::iterator mid = left + num / 2;
    cnt += mergeSort(left, mid);
    cnt += mergeSort(mid, right);
    cnt += merge(left, mid, right);
  }
  return cnt;
}

int main(int argc, char const *argv[]) {
  int n;
  unsigned long long cnt;
  std::cin >> n;
  std::vector<int> S(n);
  // for(int i=0; i<n; i++) S[i] = n-i;
  for (int i = 0; i < n; i++)
    std::cin >> S[i];
  cnt = mergeSort(S.begin(), S.end());
  // for(int i=0; i<n; i++) std::cout << S[i] << ((i<n-1)?" ":"\\n");
  std::cout << cnt << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

int main() {
  int a, n, cnt = 0;
  int max_id;
  std::vector<int> A;

  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> a;
    A.push_back(a);
  }

  while (A.size() > 1) {
    max_id = 0;
    for (int i = 1; i < A.size(); i++) {
      if (A[max_id] < A[i])
        max_id = i;
    }
    cnt += A.size() - max_id - 1;
    A.erase(A.begin() + max_id);
  }

  std::cout << cnt << std::endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int INF = 1e9 + 7;

ll merge(vector<int> &A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  vector<int> L(n1 + 1);
  vector<int> R(n2 + 1);
  for (int i = 0; i < n1; ++i) {
    L.at(i) = A.at(left + i);
  }
  for (int i = 0; i < n2; ++i) {
    R.at(i) = A.at(mid + i);
  }
  L.at(n1) = INF;
  R.at(n2) = INF;
  ll cnt = 0;
  int i = 0;
  int j = 0;
  for (int k = left; k < right; ++k) {
    if (L.at(i) <= R.at(j)) {
      A.at(k) = L.at(i);
      ++i;
    } else {
      A.at(k) = R.at(j);
      ++j;
      cnt += (mid - left) - i;
    }
  }
  return cnt;
}

ll mergeSort(vector<int> &A, int left, int right) {
  ll cnt = 0;
  if (right - left > 1) {
    int mid = (left + right) / 2;
    cnt += mergeSort(A, left, mid);
    cnt += mergeSort(A, mid, right);
    cnt += merge(A, left, mid, right);
  }
  return cnt;
}

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

  int n;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; ++i) {
    cin >> A.at(i);
  }
  cout << mergeSort(A, 0, n) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int inversion(int A[], int length) {
  if (length) {
    int cnt = 0;
    for (int i = 0; i < length; ++i) {
      if (A[i] > A[length - 1])
        ++cnt;
    }
    return inversion(A, length - 1) + cnt;
  } else {
    return 0;
  }
}

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

  cout << inversion(A, n) << endl;

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

#include <cmath>
#include <cstdio>
using namespace std;
long long int count = 0;
void merge(int a[], int left, int mid, int right) {
  int n1 = mid - left, 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] = (int)1e9 + 1;
  r[n2] = (int)1e9 + 1;
  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    if (l[i] <= r[j]) {
      a[k] = l[i];
      i++;
    } else {
      count += n1 - i;
      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;
  scanf("%d", &n);
  int data[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &data[i]);
  }
  mergesort(data, 0, n);
  printf("%lld\\n", count);
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

#include "string.h"
#include <algorithm>
#include <iostream>
#include <limits>
#include <math.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using intll_t = long long;

intll_t mergeCounting(vector<int> &inOutList) {
  if (inOutList.size() == 0) {
    throw exception();
  }
  if (inOutList.size() == 1) {
    return 0;
  }
  int formerLen = floor(inOutList.size() / 2);
  vector<int> former, latter;
  former.reserve(formerLen);
  latter.reserve(inOutList.size() - formerLen);
  copy(inOutList.begin(), inOutList.begin() + formerLen, back_inserter(former));
  copy(inOutList.begin() + formerLen, inOutList.end(), back_inserter(latter));
  intll_t transCnt = 0;
  transCnt += mergeCounting(former);
  transCnt += mergeCounting(latter);
  auto formerIt = former.begin();
  auto latterIt = latter.begin();
  auto outIt = inOutList.begin();
  while (outIt != inOutList.end()) {
    int formerVal =
        (formerIt == former.end()) ? numeric_limits<int>::max() : *formerIt;
    int latterVal =
        (latterIt == latter.end()) ? numeric_limits<int>::max() : *latterIt;
    if (formerVal < latterVal) {
      *outIt = formerVal;
      ++formerIt;
    } else {
      *outIt = latterVal;
      ++latterIt;
      transCnt += distance(formerIt, former.end());
    }
    ++outIt;
  }
  return transCnt;
}

int main() {
  int n;
  scanf("%d", &n);
  vector<int> ais;
  ais.reserve(n);
  for (int i = 0; i < n; i++) {
    int number;
    scanf("%d", &number);
    ais.push_back(number);
  }
  intll_t cnt = mergeCounting(ais);
  printf("%lld", cnt);
  printf("\\n");
  return 0;
}


Error: TLE

Fixed submission:

#include "string.h"
#include <algorithm>
#include <iostream>
#include <limits>
#include <math.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
  int N;
  scanf("%d", &N);
  int numOfTrs = 0;
  set<int> Ais;
  for (int i = 0; i < N; i++) {
    int Ai;
    scanf("%d", &Ai);
    auto it = Ais.insert(Ai).first;
    numOfTrs += distance(it, Ais.end()) - 1;
  }
  printf("%d", numOfTrs);
  printf("\\n");
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <ios>
#include <iostream>
#include <limits.h>
#include <vector>
using namespace std;
#define FOR(i, s, e) for (int i = s; i < e; ++i)
typedef vector<long> vec;
long long c = 0;

void Merge(vec &vA, int l, int m, int r) {
  int n1 = m - l;
  int n2 = r - m;
  vec vL(n1 + 1, 0);
  vec vR(n2 + 1, 0);
  FOR(i, 0, n1) vL[i] = vA[l + i];
  FOR(i, 0, n2) vR[i] = vA[m + i];
  vL[n1] = LONG_MAX;
  vR[n2] = LONG_MAX;
  int s = 0, t = 0;
  FOR(i, l, r) {
    if (vL[s] > vR[t]) {
      vA[i] = vR[t];
      ++t;
      c += n1 - s;
    } else {
      vA[i] = vL[s];
      ++s;
    }
  }
}

void CountCross(vec &vA, int l, int r) {
  if (l + 1 >= r)
    return;
  int m = (l + r) / 2;
  CountCross(vA, l, m);
  CountCross(vA, m, r);
  Merge(vA, l, m, r);
}

int main() {
  int n;
  cin >> n;
  vec vA(n, 0);
  FOR(i, 0, n) cin >> vA[i];
  CountCross(vA, 0, n);
  cout << c << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <ios>
#include <iostream>
#include <vector>
using namespace std;
typedef vector<long> vec;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, i, k, c = 0;
  cin >> n;
  vec vA(n, 0);
  for (i = 0; i < n; ++i) {
    cin >> vA[i];
    k = vA[i];
    sort(vA.begin(), vA.begin() + i);
    vec::iterator it = std::lower_bound(vA.begin(), vA.begin() + i, k);
    c += i - (it - vA.begin());
  }

  cout << c << "\\n";

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

#include <iostream>
using namespace std;

#define MAX 200000
#define SENTINEL 2000000000
typedef long long ll;

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

ll merge(int A[], int left, int mid, int right) {
  ll cnt = 0;
  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 l = 0;
  int r = 0;
  for (int i = left; i < right; i++) {
    if (L[l] <= R[r]) {
      A[i] = L[l++];
    } else {
      A[i] = R[r++];
      cnt += n1 - l;
    }
  }
  return cnt;
}

ll merge_sort(int A[], int left, int right) {
  int mid;
  ll v1, v2, v3;
  if (left + 1 < right) {
    mid = (left + right) / 2;
    v1 = merge_sort(A, left, mid);
    v2 = merge_sort(A, mid, right);
    v3 = merge(A, left, mid, right);
    return v1 + v2 + v3;
  } else {
    return 0;
  }
}

int main() {
  int A[MAX], N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll ans = merge_sort(A, 0, N);
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#define MAX 200000

int cnt = 0;

int main() {
  int A[MAX], N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int src;
  for (int i = 0; i < N - 1; i++) {
    src = A[i];
    for (int j = i + 1; j < N; j++) {
      if (src > A[j])
        cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX_N = 1000100;
int bit[MAX_N], n_, 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;
  }
}

int a[MAX_N], b[MAX_N];
P c[MAX_N];
int main() {
  ll ans = 0;

  cin >> n;
  //    n_=1;
  //    while(n_ < n) n_ *=2;

  for (int j = 0; j < n; j++) {
    cin >> b[j];
    c[j] = P(b[j], j);
  }
  sort(c, c + n);
  for (int j = 0; j < n; j++) {
    c[j].first = j + 1;
    swap(c[j].first, c[j].second);
  }
  sort(c, c + n);
  for (int j = 0; j < n; j++) {
    a[j] = c[j].second;
  }
  for (int j = 0; j < n; j++) {
    ans += j - sum(a[j]);
    add(a[j], 1);
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX_N = 100010;
int bit[MAX_N], n_, 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;
  }
}

int a[MAX_N], b[MAX_N];
P c[MAX_N];
int main() {
  ll ans = 0;

  cin >> n;
  //    n_=1;
  //    while(n_ < n) n_ *=2;

  for (int j = 0; j < n; j++) {
    cin >> b[j];
    c[j] = P(b[j], j);
  }
  sort(c, c + n);
  for (int j = 0; j < n; j++) {
    c[j].first = j + 1;
    swap(c[j].first, c[j].second);
  }
  sort(c, c + n);
  for (int j = 0; j < n; j++) {
    a[j] = c[j].second;
  }
  for (int j = 0; j < n; j++) {
    ans += j - sum(a[j]);
    add(a[j], 1);
  }
  cout << ans << endl;
}
Problem p02274 (C++) — buggy submission, verdict: Runtime Error

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

int arr[200005];
int n;

long long dnc(int from, int to) {
  if (from >= to - 1)
    return 0;

  long long num = 0;
  int mid = (from + to) / 2;
  num += dnc(from, mid);
  num += dnc(mid, to);

  int i = from, j = mid, cnt = 0;
  while (i < mid && j < to) {
    if (arr[i] > arr[j]) {
      num += j - i - cnt++;
      j++;
    } else {
      i++;
    }
  }

  sort(arr + from, arr + to);
  return num;
}

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

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

  cout << dnc(0, n) << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int arr[7];
int n;

int dnc(int from, int to) {
  if (from >= to - 1)
    return 0;

  int num = 0;
  int mid = (from + to) / 2;
  num += dnc(from, mid);
  num += dnc(mid, to);

  int i = from, j = mid, cnt = 0;
  while (i < mid && j < to) {
    if (arr[i] > arr[j]) {
      num += j - i - cnt++;
      j++;
    } else {
      i++;
    }
  }

  sort(arr + from, arr + to);
  return num;
}

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

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

  cout << dnc(0, n) << endl;

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
#define U(v) cerr << #v << ": " << (v) << endl

template <class T> struct BIT {
  vector<T> a;
  BIT(int size) : a(size, 0) {}
  T sum(int r) const {
    T sm = 0;
    for (--r; r >= 0; r = (r & r + 1) - 1)
      sm += a[r];
    return sm;
  }
  T sum(int l, int r) const { return sum(r) - sum(l); }
  T at(int i) const { return sum(i, i + 1); }
  void add(int i, T v) {
    for (; i < (int)a.size(); i |= i + 1)
      a[i] += v;
  }
  void set(int i, T v) { add(i, v - at(i)); }
};

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<pair<int, int>> b(n);
  for (int i = 0; i < n; ++i) {
    cin >> b[i].first;
    b[i].second = i;
  }
  sort(b.begin(), b.end());
  for (int i = 0; i < n; ++i)
    a[b[i].second] = i;
  BIT<int> bit(n);
  long long sm = 0;
  for (int i = n - 1; i >= 0; --i) {
    sm += bit.sum(a[i]);
    bit.add(a[i], 1);
  }
  cout << sm << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
#define U(v) cerr << #v << ": " << (v) << endl

template <class T> struct BIT {
  vector<T> a;
  BIT(int size) : a(size, 0) {}
  T sum(int r) const {
    T sm = 0;
    for (--r; r >= 0; r = (r & r + 1) - 1)
      sm += a[r];
    return sm;
  }
  T sum(int l, int r) const { return sum(r) - sum(l); }
  T at(int i) const { return sum(i, i + 1); }
  void add(int i, T v) {
    for (; i < (int)a.size(); i |= i + 1)
      a[i] += v;
  }
  void set(int i, T v) { add(i, v - at(i)); }
};

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int &i : a) {
    cin >> i;
    --i;
  }
  BIT<int> bit(n);
  long long sm = 0;
  for (int i = n - 1; i >= 0; --i) {
    sm += bit.sum(a[i]);
    bit.add(a[i], 1);
  }
  cout << sm << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <vector>
typedef long long ll;

#define REP(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

int A[999999], T[999999];

ll dc(int l, int r) {
  if (l + 1 >= r)
    return 0;
  else if (l + 2 == r) {
    if (A[l] <= A[l + 1])
      return 0;
    swap(A[l], A[l + 1]);
    return 1;
  } else {
    int m = (l + r) / 2;
    ll cl = dc(l, m);
    ll cr = dc(m, r);
    ll c = 0;
    int i = l, j = m, k = l;
    while (i < m && j < r) {
      if (A[i] <= A[j])
        T[k++] = A[i++];
      else {
        T[k++] = A[j++];
        c += m - i;
      }
    }
    while (i < m)
      T[k++] = A[i++];
    while (j < r)
      T[k++] = A[j++];
    copy(T + l, T + r, A + l);
    return cl + cr + c;
  }
}
int main() {
  int i, n;
  scanf("%d", &n);
  rep(i, n) scanf("%d", A + i);
  printf("%lld\\n", dc(0, n));
}

Error: TLE

Fixed submission:

#include <iostream>

#define REP(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)

using namespace std;

int bubble(int n, int a[]) {
  int cnt = 0;
  int i, j;
  rep(i, n) REP(j, i + 1, n) if (a[i] > a[j]) cnt++;
  return cnt;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, i;
  cin >> n;
  int a[n];
  rep(i, n) cin >> a[i];
  cout << bubble(n, a) << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

#define MAX_N 500000

long long count = 0;
int A[MAX_N + 1];

void merge(int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  int L[n1 + 1];
  int 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] = 0x7FFFFFFF;
  int i = 0, j = 0;

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

void merge_sort(int left, int right) {
  if (right - left > 1) {
    int mid = (left + right) / 2;
    merge_sort(left, mid);
    merge_sort(mid, right);
    merge(left, mid, right);
  }
}

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

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

  cout << count << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

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

  int n, num, max, maxi, count = 0;
  cin >> n;

  vector<int> vec;
  for (int i = 0; i < n; i++) {
    cin >> num;
    vec.push_back(num);
  }

  while (!vec.empty()) {
    max = vec[0];
    maxi = 0;
    for (int i = 0; i < n; i++) {
      if (max < vec[i]) {
        max = vec[i];
        maxi = i;
      }
    }

    for (int i = maxi + 1; i < n; i++) {
      if (vec[maxi] > vec[i]) {
        count++;
      }
    }
    vec.erase(vec.begin() + maxi);
    n--;
  }
  cout << count << endl;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define Vi vector<int>
#define FOR(i, s, e) for (int i = s; i < e; ++i)
#define IFOR(i, s, e) for (int i = s; i < e; --i)
#define ITER(v) v.begin(), v.end()

constexpr int MAX_N = 200'000;
constexpr int64_t MAX_VAL = 1e9 + 1;

int64_t merge(int64_t *A, const int &left, const int &mid, const int &right) {
  const int n1 = mid - left;
  const int n2 = right - mid;

  auto L = new int64_t[n1 + 1];
  auto R = new int64_t[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] = MAX_VAL;
  R[n2] = MAX_VAL;

  int64_t count = 0;
  int 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++];
      count += n1 - i;
    }
  }
  delete[] L;
  delete[] R;
  return count;
}

int64_t mergeSort(int64_t *A, const int &left, const int &right) {
  int64_t count = 0;
  if (left + 1 < right) {
    const int mid = (left + right) / 2;
    count += mergeSort(A, left, mid);
    count += mergeSort(A, mid, right);
    count += merge(A, left, mid, right);
    return count;
  }
  return 0;
}

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

  int n;
  cin >> n;

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

  int64_t count = mergeSort(A, 0, n);
  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define Vi vector<int>
#define FOR(i, s, e) for (int i = s; i < e; ++i)
#define IFOR(i, s, e) for (int i = s; i < e; --i)
#define ITER(v) v.begin(), v.end()

int countReverse(int *a, int n) {
  int count = 0;
  bool flag = true;
  FOR(i, 0, n)
  FOR(j, i, n) {
    if (a[i] > a[j]) {
      ++count;
    }
  }
  return count;
}

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

  int n;
  cin >> n;

  int *a = new int[n];
  FOR(i, 0, n) { cin >> a[i]; }

  int count = countReverse(a, n);
  cout << count << endl;

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

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

int S[200000];
int L[100001];
int R[100001];
long long c = 0;

void merge(int A[], int left, int mid, int right) {
  int n1 = mid - left;
  int 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] = R[n2] = numeric_limits<int>::max();
  i = j = 0;
  for (k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      A[k] = L[i++];
      c += j;
    } 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() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> S[i];
  mergeSort(S, 0, n);
  cout << c << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int A[200000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, c = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < i; j++) {
      if (A[i] < A[j])
        c++;
    }
  }
  cout << c << '\\n';
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

struct BIT {
  int size;
  vector<ll> dat;
  void init(int n) {
    size = n;
    dat.clear();
    dat.resize(n + 1);
  }
  void add(int i, ll x) {
    while (i <= size) {
      dat[i] += x;
      i += i & -i;
    }
  }
  ll sum(int i) {
    ll s = 0;
    while (0 < i) {
      s += dat[i];
      i -= i & -i;
    }
    return s;
  }
};
int a[200001], a2[200001];
int main() {
  int n, ai;
  cin >> n;
  BIT b;
  b.init(n + 1);
  ll cnt = 0;
  rep(i, n) {
    cin >> ai;
    a[i] = a2[i] = ai;
  }
  sort(a2, a2 + n);
  rep(i, n) { a[i] = lower_bound(a2, a2 + n, a[i]) - a2 + 1; }
  rep(i, n) {
    cnt += i - b.sum(a[i]);
    b.add(a[i], 1);
  }
  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<int, int> pint;

struct BIT {
  int size;
  vector<ll> dat;
  void init(int n) {
    size = n;
    dat.clear();
    dat.resize(n + 1);
  }
  void add(int i, ll x) {
    while (i <= size) {
      dat[i] += x;
      i += i & -i;
    }
  }
  ll sum(int i) {
    ll s = 0;
    while (0 < i) {
      s += dat[i];
      i -= i & -i;
    }
    return s;
  }
};

int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n, ai;
  cin >> n;
  BIT b;
  b.init(n + 1);
  ll cnt = 0;
  rep(i, n) {
    cin >> ai;
    cnt += i - b.sum(ai);
    b.add(ai, 1);
  }
  cout << cnt << endl;
  return 0;
}