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

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define int long long
using namespace std;

int b[200005];
int BIT[200005];
void tasu(int a) {
  while (a < 200005) {
    BIT[a]++;
    a += a & -a;
  }
}
int sum(int a) {
  int b = 0;
  while (a > 0) {
    b += BIT[a];
    a -= a & -a;
  }
  return b;
}
signed main() {
  int a;
  scanf("%lld", &a);
  set<int> S;
  for (int c = 0; c < a; c++) {
    scanf("%d", &b[c]);
    S.insert(b[c]);
  }
  unordered_map<int, int> U;
  int c = 1;
  for (auto d = S.begin(); d != S.end(); d++) {
    U[*d] = c;
    c++;
  }
  for (int c = 0; c < a; c++) {
    b[c] = U[b[c]];
  }
  int ans = 0;
  for (int i = 0; i < a; i++) {
    ans += i - sum(b[i]);
    tasu(b[i]);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define int long long
using namespace std;

int b[100005];
int BIT[100005];
void tasu(int a) {
  while (a < 100005) {
    BIT[a]++;
    a += a & -a;
  }
}
int sum(int a) {
  int b = 0;
  while (a > 0) {
    b += BIT[a];
    a -= a & -a;
  }
  return b;
}
signed main() {
  int a;
  scanf("%lld", &a);
  set<int> S;
  for (int c = 0; c < a; c++) {
    scanf("%d", &b[c]);
    S.insert(b[c]);
  }
  unordered_map<int, int> U;
  int c = 1;
  for (auto d = S.begin(); d != S.end(); d++) {
    U[*d] = c;
    c++;
  }
  for (int c = 0; c < a; c++) {
    b[c] = U[b[c]];
  }
  int ans = 0;
  for (int i = 0; i < a; i++) {
    ans += i - sum(b[i]);
    tasu(b[i]);
  }
  cout << ans << endl;
}
Problem p02274 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll NumberOfInversions(int l, int r, vector<int> &a) {
  if (r - l <= 1)
    return 0;

  ll res = 0;
  int mid = (r + l) / 2;

  res += NumberOfInversions(l, mid, a);
  res += NumberOfInversions(mid, r, a);

  vector<int> tmp(r - l);
  int idx = 0, idx_l = l, idx_r = mid;
  while (idx < (r - l)) {
    if (idx_l == mid)
      tmp[idx++] = a[idx_r++];
    else if (idx_r == r)
      tmp[idx++] = a[idx_l++];
    else {
      if (a[idx_l] <= a[idx_r])
        tmp[idx++] = a[idx_l++];
      else {
        res += mid - idx_l;
        tmp[idx++] = a[idx_r++];
      }
    }
  }

  for (int i = l; i < r; ++i)
    a[i] = tmp[i - l];

  return res;
}

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

  cout << NumberOfInversions(0, n, a) << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int NumberOfInversions(int l, int r, vector<int> &a) {
  if (r - l <= 1)
    return 0;

  int res = 0, mid = (r + l) / 2;

  res += NumberOfInversions(l, mid, a);
  res += NumberOfInversions(mid, r, a);

  vector<int> tmp(r - l);
  int idx = l, idx_l = l, idx_r = mid;
  while (idx < r) {
    if (idx_l == mid)
      tmp[idx++] = a[idx_r++];
    else if (idx_r == r)
      tmp[idx++] = a[idx_l++];
    else {
      if (a[idx_l] <= a[idx_r])
        tmp[idx++] = a[idx_l++];
      else {
        res += mid - idx_l;
        tmp[idx++] = a[idx_r++];
      }
    }
  }

  for (int i = l; i < r; ++i)
    a[i] = tmp[i];

  return res;
}

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

  cout << NumberOfInversions(0, n, a) << endl;

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

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

long c = 0;

void merge(unsigned long long a[], long l, long m, long r) {
  long n1 = m - l;
  long n2 = r - m;
  vector<unsigned long long> L(n1 + 1), R(n2 + 1);

  for (long i = 0; i < n1; i++)
    L[i] = a[l + i];

  for (long i = 0; i < n2; i++)
    R[i] = a[m + i];

  L[n1] = 1000000001;
  R[n2] = 1000000002;
  long i = 0, j = 0;

  for (long k = l; k < r; k++) {
    if (L[i] <= R[j]) {
      a[k] = L[i];
      i++;
    } else {
      a[k] = R[j];
      j++;
      c += n1 - i;
    }
  }
}

void mergeSort(unsigned long long a[], long l, long r) {
  if (l + 1 < r) {
    long m = (l + r) / 2;
    mergeSort(a, l, m);
    mergeSort(a, m, r);
    merge(a, l, m, r);
  }
}

int main() {
  long n = 0;
  unsigned long long a[200000] = {0};

  cin >> n;

  for (long i = 0; i < n; i++)
    cin >> a[i];

  mergeSort(a, 0, n);

  cout << c << endl;

  return 0;
}

Error: 0

Fixed submission:

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

long long c = 0;

void merge(long long a[], long long l, long long m, long long r) {
  long long n1 = m - l;
  long long n2 = r - m;
  vector<long long> L(n1 + 1), R(n2 + 1);

  for (long long i = 0; i < n1; i++)
    L[i] = a[l + i];

  for (long long i = 0; i < n2; i++)
    R[i] = a[m + i];

  L[n1] = 100000001;
  R[n2] = 100000002;
  long long i = 0, j = 0;

  for (long long k = l; k < r; k++) {
    if (L[i] <= R[j]) {
      a[k] = L[i];
      i++;
    } else {
      a[k] = R[j];
      j++;
      c += n1 - i;
    }
  }
}

void mergeSort(long long a[], long long l, long long r) {
  if (l + 1 < r) {
    long long m = (l + r) / 2;
    mergeSort(a, l, m);
    mergeSort(a, m, r);
    merge(a, l, m, r);
  }
}

int main() {
  long long n = 0;
  long long a[20001] = {0};

  cin >> n;

  for (long long i = 0; i < n; i++)
    cin >> a[i];

  mergeSort(a, 0, n);

  cout << c << endl;

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int N;
void input(int **line) {
  cin >> N;
  *line = new int[N];
  for (int i = 0; i < N; ++i) {
    cin >> (*line)[i];
  }
}
long long merge(int *line, int lp, int mid, int rp) {
  int ln = mid - lp;
  int rn = rp - mid;
  int *L = new int[ln + 1];
  int *R = new int[rn + 1];
  for (int i = 0; i < ln; ++i) {
    L[i] = line[lp + i];
  }
  for (int i = 0; i < rn; ++i) {
    R[i] = line[mid + i];
  }
  L[ln] = R[rn] = 2000000000;
  long long res = 0;
  int lu = 0;
  int ru = 0;
  for (int i = 0, I = rp - lp; i < I; ++i) {
    if (L[lu] <= R[ru]) {
      line[lp + i] = L[lu];
      ++lu;
    } else {
      line[lp + i] = R[ru];
      ++ru;
      res += ln - lu;
    }
  }
  return res;
}
long long mergesort(int *line, int lp, int rp) {
  long long res = 0;
  if (rp - lp >= 2) {
    int mid = (lp + rp) / 2;
    res += mergesort(line, lp, mid);
    res += mergesort(line, mid, rp);
    res += merge(line, lp, mid, rp);
  }
  return res;
}
int main() {
  int *line;
  input(&line);
  cout << mergesort(line, 0, N) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int *line;
  line = new int[n];
  for (int i = 0; i < n; ++i) {
    cin >> line[i];
  }
  int ans = 0;
  for (int i = n - 1; i >= 0; --i) {
    for (int j = 0; j < i; ++j) {
      if (line[j] > line[j + 1]) {
        swap(line[j], line[j + 1]);
        ++ans;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

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

random_device rd;
mt19937 mt(rd());

template <class T> struct tree {
  struct node {
    T v;
    int c, p;
    node *n[2];

    node(T x) {
      v = x;
      c = 1;
      p = mt();
      n[0] = n[1] = nullptr;
    }
  };

  typedef node N;
  N *root;
  long counter;

  tree() {
    root = nullptr;
    counter = 0;
  }

  inline int count(N *n) { return n ? n->c : 0; }

  N *rotate(N *r, int b) {
    N *s = r->n[1 - b];

    r->c += count(s->n[b]) - count(r->n[1 - b]);
    r->n[1 - b] = s->n[b];

    s->c += r->c - count(s->n[b]);
    s->n[b] = r;
    return s;
  }

  N *insert(int x, N *t, int cnt) {
    if (!t) {
      counter += cnt;
      return new N(x);
    }
    int b = !(x < t->v);
    if (b)
      cnt -= 1 + count(t->n[0]);
    t->n[b] = insert(x, t->n[b], cnt);
    t->c++;
    return rotate(t, 1 - b);
  }

  void insert(T x) { root = insert(x, root, count(root)); }
};

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

  tree<int> t;
  while (n--) {
    scanf("%d", &x);
    t.insert(x);
  }

  printf("%ld\\n", t.counter);
}


Error: TLE

Fixed submission:

#include <cstdio>
using namespace std;

template <class T> struct node {
  T v;
  int c;
  node *l, *r;
  node(T x) {
    v = x;
    c = 1;
    l = r = nullptr;
  }
};

template <class T> struct tree {
  typedef node<T> N;
  N *root;
  long counter;
  tree() {
    root = nullptr;
    counter = 0;
  }
  void insert(N *n, N *t, int cnt) {
    if (n->v < t->v)
      if (t->l)
        insert(n, t->l, cnt);
      else
        t->l = n, counter += cnt;
    else if (t->r)
      insert(n, t->r, cnt - 1 - (t->l ? t->l->c : 0));
    else
      t->r = n, counter += cnt - 1 - (t->l ? t->l->c : 0);
    t->c++;
  }
  void insert(T x) {
    N *n = new N(x);
    if (root)
      insert(n, root, root->c);
    else
      root = n;
  }
};

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

  tree<int> t;
  while (n--) {
    scanf("%d", &x);
    t.insert(x);
  }

  printf("%ld\\n", t.counter);
}
Problem p02274 (C++) — buggy submission, verdict: Memory Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
// #include <Windows.h>
// #include "Library.h"
// #include "Math.h"
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef vector<int> vecint;
typedef vector<vecint> dvecint;
typedef vector<dvecint> ddvecint;
const int INF = 2000000000;
const double EPS = 10e-9;
vecint a;
ll mergesort(vecint &a) {
  int n = a.size();
  if (n <= 1)
    return 0;
  ll res = 0;
  vecint b(a.begin(), a.begin() + n / 2);
  vecint c(a.begin() + n / 2, a.end());
  res += mergesort(b);
  res += mergesort(c);
  int ai = 0, bi = 0, ci = 0;
  while (ai < n) {
    if (bi < b.size() && (ci == c.size() || b[bi] <= c[ci])) {
      a[ai++] = b[bi++];
    } else {
      a[ai++] = c[ci++];
      res += n / 2 - bi;
    }
  }
  return res;
}
int main() {
  int n;
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << mergesort(a) << endl;
  return 0;
}

Error: MLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
// #include "MyMath.h"
// #include "MyDisjointset.h"
#include <algorithm>
#include <functional>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long int ll;
const int INF = 2000000000;
int bit[1000000005], a[200000];
int n, ma = 0;
void add(int j, int t) {
  while (j <= ma + 1) {
    bit[j] += t;
    j += j & -j;
  }
}
ll sum(int j) {
  ll ans = 0;
  while (j != 0) {
    ans += bit[j];
    j -= j & -j;
  }
  // cout << "j " << j << " ans " << ans << endl;
  return ans;
}
int main() {
  cin >> n;
  ll res = 0;
  memset(bit, 0, sizeof(bit));
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ma = max(ma, a[i]);
  }
  for (int i = 0; i < n; i++) {
    res += i - sum(++a[i]);
    add(a[i], 1);
  }
  cout << res << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
#define INF 2e9
const int maxn = 200000 + 10;
int n;
int a[maxn];
int L[maxn / 2 + 2], R[maxn / 2 + 2];
LL merge(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, j = 0;
  LL cnt = 0;
  for (int k = left; k < right; k++) {
    if (L[i] <= R[j]) {
      a[k] = L[i++];

    } else {
      a[k] = R[j++];
      cnt += n1 - i;
    }
  }
  return cnt;
}
LL merge_sort(int left, int right) {
  if (right - left > 1) {
    int mid = (left + right) / 2;
    LL n1 = merge_sort(left, mid);
    LL n2 = merge_sort(mid, right);
    LL n3 = merge(left, mid, right);
    return n1 + n2 + n3;
  }
  return 0;
}
int main() {
  //    freopen("in.txt","r",stdin);
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  cout << merge_sort(0, n) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 200000 + 10;
int n;
int a[maxn];
int main() {
  //    freopen("in.txt","r",stdin);
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
      if (a[j] < a[j - 1]) {
        swap(a[j], a[j - 1]);
        cnt++;
      }
    }
  }
  printf("%d\\n", cnt);

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

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

int main() {
  int M = 1 << 18 + 1;
  int N;
  cin >> N;
  vector<int> A(N), a(N), ord(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    a[i] = A[i];
  }

  sort(a.begin(), a.end());
  for (int i = 0; i < N; i++) {
    ord[i] = lower_bound(a.begin(), a.end(), A[i]) - a.begin();
  }

  ll ans = 0;
  vector<int> bit(M);
  int k, sum;
  for (int i = 0; i < N; i++) {
    // add
    k = ord[i] + 1;
    while (k <= M) {
      bit[k]++;
      k += k & -k;
    }
    // sum
    sum = 0;
    k = ord[i] + 1;
    while (k > 0) {
      sum += bit[k];
      k -= k & -k;
    }

    ans += i + 1 - sum;
  }

  cout << ans << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int N;
  cin >> N;
  ll a, ans = 0;
  set<ll> s;
  for (int i = 0; i < N; i++) {
    cin >> a;
    s.insert(a);
    ans += distance(s.find(a), s.end()) - 1;
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Runtime Error

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

#define MAX 1000000001
long long CNT = 0;

void merge(long long a[], int begin, int end, int mid) {
  queue<long long> left;
  queue<long long> right;
  for (int i = begin; i < mid; i++)
    left.push(a[i]);
  for (int i = mid; i < end; i++)
    right.push(a[i]);
  left.push(MAX);
  right.push(MAX);

  int cnt = left.size() - 1;
  for (int i = begin; i < end; i++) {
    if (left.front() < right.front()) {
      a[i] = left.front();
      left.pop();
      cnt--;
    } else {
      a[i] = right.front();
      right.pop();
      CNT += cnt;
    }
  }
}

void m_sort(long long a[], int begin, int end) {
  if (begin >= end - 1)
    return;
  int mid = (begin + end) / 2;

  m_sort(a, begin, mid);
  m_sort(a, mid, end);

  merge(a, begin, end, mid);
}

int main() {
  long long ary[200000];
  int n;

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

  m_sort(ary, 0, n);

  cout << CNT << endl;
  return 0;
}

Error: 0

Fixed submission:

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

#define MAX 1000000001
int CNT = 0;

void merge(long long a[], int begin, int end, int mid) {
  queue<long long> left;
  queue<long long> right;
  for (int i = begin; i < mid; i++)
    left.push(a[i]);
  for (int i = mid; i < end; i++)
    right.push(a[i]);
  left.push(MAX);
  right.push(MAX);

  int cnt = left.size() - 1;
  for (int i = begin; i < end; i++) {
    if (left.front() < right.front()) {
      a[i] = left.front();
      left.pop();
      cnt--;
    } else {
      a[i] = right.front();
      right.pop();
      CNT += cnt;
    }
  }
}

void m_sort(long long a[], int begin, int end) {
  if (begin >= end - 1)
    return;
  int mid = (begin + end) / 2;

  m_sort(a, begin, mid);
  m_sort(a, mid, end);

  merge(a, begin, end, mid);
}

int main() {
  long long ary[1000];
  int n;

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

  m_sort(ary, 0, n);

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

#include <algorithm>
#include <iostream>
#include <vector>

#pragma warning(disable : 4996)

using namespace std;

int n, bit[262145];

vector<int> a, b;

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

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

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

  return ret;
}

int sum(int l, int r) { return sum(r - 1) - sum(l - 1); }

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

  a.resize(n);

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

  b = a;
  sort(b.begin(), b.end());

  long long ret = 0;

  for (int i = 0; i < n; i++) {
    int ptr = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
    add(ptr + 1, 1);

    ret += sum(ptr + 2, n + 1);
  }

  printf("%lld\\n", ret);

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

int n, a[200000];

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

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

  long long ret = 0;

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (a[i] > a[j])
        ret++;
    }
  }

  printf("%lld\\n", ret);

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

#include <algorithm>
#include <iostream>
using namespace std;
int n, a[200000], b[200000], bit[262145];
void add(int i, int x) {
  while (i <= n)
    bit[i] += x, i += i & -i;
}
int sum(int i) {
  int ret = 0;
  while (i > 0)
    ret += bit[i], i -= i & -i;
  return ret;
}
int sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]), b[i] = a[i];
  sort(b, b + n);
  long long ret = 0;
  for (int i = 0; i < n; i++) {
    int ptr = lower_bound(b, b + n, a[i]) - b;
    add(ptr + 1, 1);
    ret += sum(ptr + 2, n + 1);
  }
  printf("%lld\\n", ret);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>

using namespace std;

int n, a[200000];

list<int> l;

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

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

  long long ret = 0;

  for (int i = 0; i < n; i++) {
    list<int>::iterator ptr = lower_bound(l.begin(), l.end(), a[i]);
    l.insert(ptr, a[i]);

    ret += distance(ptr, l.end());
  }

  printf("%lld\\n", ret);

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

// ALDS1_5_D Recursion / Divide and Conquer - The Number of Inversions
#include <cstdio>

#define MAX 500000
#define SENTINAL 2000000000

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

long long merge(int *A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  long long c = 0;

  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] = SENTINAL;
  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++];
      c += mid + j - k - 1;
    }
  }
  return c;
}

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

int main(void) {
  int A[MAX];

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

  long long cnt = mergeSort(A, 0, n);
  printf("%lld\\n", cnt);

  return 0;
}


Error: TLE

Fixed submission:

// ALDS1_5_D Recursion / Divide and Conquer - The Number of Inversions
#include <cstdio>
#include <utility>

int bubbleSort(int *A, int len) {
  int cnt = 0;
  for (int i = 0; i < len; i++) {
    for (int j = len - 1; j > i; j--) {
      if (A[j] < A[j - 1]) {
        std::swap(A[j], A[j - 1]);
        cnt++;
      }
    }
  }

  return cnt;
}

int main() {
  int n;
  int ar[200000];

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

  int c = bubbleSort(ar, n);
  printf("%d\\n", c);

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

#include <stdio.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define INF 3333333333
#define uint unsigned int
#define llong long long

llong merge(uint S[], int left, int mid, int right) {
  llong numInv = 0;
  int n1 = mid - left;
  int n2 = right - mid;
  uint L[n1 + 1], R[n2 + 1];
  REP(i, n1) { L[i] = S[left + i]; }
  REP(i, n2) { R[i] = S[mid + i]; }
  L[n1] = INF;
  R[n2] = INF;

  int i = 0, j = 0;
  FOR(k, left, right) {
    if (L[i] <= R[j]) {
      S[k] = L[i];
      i += 1;
    } else {
      S[k] = R[j];
      j += 1;
      numInv += n1 - i;
    }
  }
  return numInv;
}

llong mergeSort(uint S[], int left, int right) {
  llong numInv = 0, mid;
  if ((left + 1) < right) {
    mid = (left + right) / 2;
    numInv += mergeSort(S, left, mid);
    numInv += mergeSort(S, mid, right);
    numInv += merge(S, left, mid, right);
  }
  return numInv;
}

int main(void) {
  int n;
  scanf("%d", &n);
  uint S[n];
  REP(i, n) { scanf("%d", &S[i]); }

  llong numInv = mergeSort(S, 0, n);

  printf("%lld\\n", numInv);

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define uint unsigned int

int count(uint A[], int n) {
  int count = 0;
  REP(i, n) {
    FOR(j, i, n) {
      if (A[i] > A[j])
        count++;
    }
  }
  return count;
}

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

  uint A[n];
  REP(i, n) { scanf("%d", &A[i]); }
  printf("%d\\n", count(A, n));
}