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

#include <stdio.h>
#define MAX 200000
#define INFTY 2000000000
double cnt = 0;
void bubble(int *, int, int, int);
void bubbleSort(int *, int, int);

void bubble(int *A, int left, int mid, int right) {
  int i, j, k;
  int x1, x2;
  int L[MAX], R[MAX];

  x1 = mid - left;
  x2 = right - mid;

  for (i = 0; i < x1; i++) {
    L[i] = A[left + i];
  }

  for (i = 0; i < x2; i++) {
    R[i] = A[mid + i];
  }

  L[x1] = INFTY;
  R[x2] = INFTY;

  i = 0;
  j = 0;

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

void bubbleSort(int *A, int left, int right) {
  int middle;

  if (left + 1 < right) {
    middle = (left + right + 1) / 2;
    bubbleSort(A, left, middle);
    bubbleSort(A, middle, right);
    bubble(A, left, middle, right);
  }
}

int main() {
  int i, n, A[MAX];

  scanf("%d", &n);

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

  bubbleSort(A, 0, n);

  printf("%.0f\\n", cnt);

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#define MAX 200000
#define INFTY 2000000000
int cnt = 0;
void bubble(int *, int, int, int);
void bubbleSort(int *, int, int);

void bubble(int *A, int left, int mid, int right) {
  int i, j, k;
  int x1 = mid - left, x2 = right - mid;
  int L[MAX] = {}, R[MAX] = {};

  for (i = 0; i < x1; i++) {
    L[i] = A[left + i];
  }

  for (i = 0; i < x2; i++) {
    R[i] = A[mid + i];
  }

  L[x1] = INFTY;
  R[x2] = INFTY;

  i = 0;
  j = 0;

  for (k = left; k < right; k++) {
    if (L[i] < R[j]) {
      A[k] = L[i++];
    } else {
      A[k] = R[j++];
      if (L[i] < INFTY)
        cnt = cnt + x1 - i;
    }
  }
}

void bubbleSort(int *A, int left, int right) {
  int middle;

  if (left + 1 < right) {
    middle = (left + right + 1) / 2;
    bubbleSort(A, left, middle);
    bubbleSort(A, middle, right);
    bubble(A, left, middle, right);
  }
}

int main() {
  int i, n, A[MAX];

  scanf("%d", &n);

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

  bubbleSort(A, 0, n);

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

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

#include <iostream>

using namespace std;

long long merge_sort(int *a, int l, int r) {
  if (l == r - 1)
    return a[l] > a[r] ? (swap(a[l], a[r]), 1) : 0;
  else if (l == r)
    return 0;

  int mid = (l + r) / 2;
  long long cnt = 0;
  cnt += merge_sort(a, l, mid);
  cnt += merge_sort(a, mid + 1, r);

  int i = l, j = mid + 1, k = 0, ln = mid - l + 1, rn = r - mid + 1;
  int b[r - l + 1];
  while (i <= mid && j <= r) {
    if (a[i] < a[j]) {
      b[k++] = a[i++];
      ln--;
    } else {
      b[k++] = a[j++];
      cnt += ln;
      rn--;
    }
  }
  while (i <= mid)
    b[k++] = a[i++];
  while (j <= r)
    b[k++] = a[j++];

  for (i = 0; i <= r - l; i++)
    a[l + i] = b[i];

  return cnt;
}

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

  cout << merge_sort(a, 0, n - 1) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

typedef struct NodeData *Node;

struct NodeData {
  Node left;
  Node right;
  size_t r_size;
  int val;
};

class BinTree {
private:
  Node root;

public:
  BinTree(void) { root = nullptr; };
  ~BinTree();
  size_t insert(int);
};

void deleteNode(Node n) {
  if (n != nullptr) {
    if (n->left != nullptr)
      deleteNode(n->left);
    if (n->right != nullptr)
      deleteNode(n->right);
  }
  delete n;
}

BinTree::~BinTree() {
  deleteNode(root->left);
  deleteNode(root->right);
  delete root;
}

Node newNode(size_t val) {
  Node node = new NodeData();
  node->left = nullptr;
  node->right = nullptr;
  node->r_size = 0;
  node->val = val;
  return node;
}

size_t BinTree::insert(int val) {
  size_t r_size = 0;
  if (root == nullptr) {
    root = newNode(val);
  } else {
    Node node = root;
    while (node != nullptr) {
      if (val < node->val) {
        r_size += node->r_size + 1;
        if (node->left == nullptr) {
          node->left = newNode(val);
          break;
        } else
          node = node->left;
      } else {
        node->r_size++;
        if (node->right == nullptr) {
          node->right = newNode(val);
          break;
        } else
          node = node->right;
      }
    }
  }

  return r_size;
}

int main(void) {
  size_t n, total = 0;
  BinTree t;

  cin >> n;

  for (size_t i = 0; i < n; i++) {
    size_t x;
    cin >> x;
    total += t.insert(x);
  }

  cout << total << endl;

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

//============================================================================
// Name        : MergeSort.cpp
// Author      : Ken
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

int n_comparison = 0;
long long int n_inversion = 0;

void writeVector(const vector<int> &A) {
  for (int i = 0; i < A.size() - 1; i++) {
    cout << A.at(i) << " ";
  }
  cout << A.at(A.size() - 1);
  // cout << "\\n";
}

void Merge(vector<int> &A, int left, int mid, int right) {
  // cout << "Merge([";
  // writeVector(A);
  // cout << "], " << left << ", " << mid << ", " << right << ")\\n";
  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);
  }
  L.at(n1) = pow(10, 9) + 1;
  // cout << "L: ";
  // writeVector(L);

  for (int i = 0; i < n2; i++) {
    R.at(i) = A.at(mid + i);
  }
  R.at(n2) = pow(10, 9) + 1;
  // cout << "R: ";
  // writeVector(R);

  int i = 0, j = 0;
  for (int k = left; k < right; k++) {
    if (L.at(i) <= R.at(j)) {
      // cout << "i: " << i << "\\n";
      A.at(k) = L.at(i);
      i++;
      // n_comparison ++;
    } else {
      // cout << "j: " << j << "\\n";
      // n_comparison ++;
      n_inversion += (n1 - i);
      A.at(k) = R.at(j);
      j++;
    }
  }
  // writeVector(A);
}

void MergeSort(vector<int> &A, int left, int right) {
  // cout << "MergeSort([";
  // writeVector(A);
  // cout << "], " << left << ", " << right << ")\\n";
  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, input;
  cin >> n;
  vector<int> A;
  for (int i = 0; i < n; i++) {
    cin >> input;
    A.push_back(input);
  }

  MergeSort(A, 0, n);
  // writeVector(A);
  // cout << "\\n";
  // cout << n_comparison << "\\n";
  cout << n_inversion << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

//============================================================================
// Name        : Inversions.cpp
// Author      : Ken
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

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

int main() {
  int n;
  cin >> n;
  vector<int> v;

  for (int i = 0; i < n; i++) {
    int input;
    cin >> input;
    v.push_back(input);
  }

  int count = 0;

  for (int i = 0; i < v.size(); i++) {
    for (int j = i + 1; j < v.size(); j++) {
      if (v.at(i) > v.at(j)) {
        count++;
      }
    }
  }

  cout << count << "\\n";

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int num;
long long int num_inverse;

void 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] = INT_MAX;
  R[n2] = INT_MAX;

  int i = 0;
  int j = 0;

  for (int k = left; k < right; k++) {
    num++;
    if (L[i] < R[j]) {
      a[k] = L[i];
      i += 1;
    } else {
      a[k] = R[j];
      num_inverse += (n1 - i);
      j += 1;
    }
  }
  return;
}

void mergeSort(int *a, int left, int right) {
  int num = 0;
  int num_inverse = 0;
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    mergeSort(a, left, mid);
    mergeSort(a, mid, right);
    merge(a, left, mid, right);
  }
  return;
}

int main() {
  num = 0;
  int n;
  cin >> n;
  int *S = new int[n];

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

  mergeSort(S, 0, n);

  for (int i = 0; i < n; i++) {
    // if(i!=0){cout<<" ";}
    // cout << S[i];
  }
  // cout << endl;

  // cout << num << endl;
  cout << num_inverse << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

void bubbleSort(int *a, int n) {
  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++;
      }
    }
  }
}

int inversion(int *a, int n) {
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (a[i] > a[j]) {
        cnt++;
      }
    }
  }
  return cnt;
}

int main() {

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

  cout << inversion(a, n) << endl;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <iostream>
#include <iterator>
#include <vector>

typedef std::vector<int> TList;
long long int c;
void merge(TList &A, int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  TList L(A.begin() + left, A.begin() + left + n1);
  L.push_back(INT_MAX);

  TList R(n2 + 1);
  for (int i = 0; i < n2; ++i) {
    R[i] = A[mid + i];
  }
  R[n2] = INT_MAX;

  int i = 0;
  int j = 0;
  for (int k = left; k < right; ++k) {
    if (L[i] <= R[j]) {
      A[k] = L[i];
      i = i + 1;
    } else {
      A[k] = R[j];
      j = j + 1;
      c += L.size() - 1 - i;
    }
  }
}

void mergeSort(TList &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;
  std::cin >> n;
  TList A(n);
  for (std::size_t i = 0; i < n; ++i) {
    std::cin >> A[i];
  }
  c = 0;
  mergeSort(A, 0, n);
  std::cout << c << std::endl;
}

Error: TLE

Fixed submission:

/*
3 5 2 1 4

3 2, 3 1, 5 2, 5 1, 5 4, 2 1

*/
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

struct Compare {
  int v_m;
  Compare(int v) : v_m(v) {}
  bool operator()(int a) { return a < v_m; }
};
int main() {
  int n;
  std::cin >> n;
  std::vector<int> A(n);
  for (std::size_t i = 0; i < n; ++i) {
    std::cin >> A[i];
  }

  int c = 0;
  for (std::vector<int>::iterator i = A.begin(); i != A.end(); ++i) {
    c += std::count_if(i, A.end(), Compare(*i));
  }

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

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

#define MAX_N 1000000

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

void add(int p) {
  while (p <= n) {
    bit[p] += 1;
    p += p & -p;
  }
}
int sum1(int p) {
  int sum2 = 0;
  while (p > 0) {
    sum2 += bit[p];
    p -= p & -p;
  }
  return sum2;
}
int sum(int l, int r) { return sum1(r - 1) - sum1(l - 1); }

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);
  long long res = 0LL;
  for (int i = 1; i <= n; i++) {
    int v = lower_bound(b + 1, b + n + 1, a[i]) - b;
    add(v);
    res += sum(v + 1, n + 1);
  }
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int x[200000];

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

#include <climits>
#include <cstdio>

int a[200000];
int L[100001];
int R[100001];

long long merge(int left, int mid, int right) {
  long long c = 0;
  int i;
  for (i = 0; i < mid - left; i++)
    L[i] = a[i + left];
  for (i = 0; i < right - mid; i++)
    R[i] = a[i + mid];
  L[mid - left] = INT_MAX;
  R[right - mid] = INT_MAX;
  int j = 0;
  int k = 0;
  for (i = left; i < right; i++) {
    if (R[k] < L[j]) {
      c += mid + k - i;
      a[i] = R[k];
      k++;
    } else {
      a[i] = L[j];
      j++;
    }
  }
  return c;
}

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

int main() {
  int i, n;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", a + i);
  printf("%lld\\n", mergeSort(0, n));
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>

int main() {
  int i, j, n;
  scanf("%d", &n);
  int a[n];
  for (i = 0; i < n; i++)
    scanf("%d", a + i);
  int c = 0;
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      if (a[i] > a[j])
        c++;
    }
  }
  printf("%d\\n", c);
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i, a, n) for (int i = (a), i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, n) for (int i = (a); i > n; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
constexpr int MAX = 200001;
vi v, L(MAX), R(MAX);
ll merge(int left, int mid, int right) {
  int n1 = mid - left;
  int n2 = right - mid;
  ll cnt = 0;
  rep(i, 0, n1) L[i] = v[left + i];
  rep(i, 0, n2) R[i] = v[mid + i];
  L[n1] = R[n2] = 1e9;
  int i = 0, j = 0;
  rep(k, left, right) {
    if (L[i] < R[j]) {
      v[k] = L[i];
      ++i;
    } else {
      v[k] = R[j];
      ++j;
      cnt += n1 - i;
    }
  }
  return cnt;
}
ll mergesort(int left, int right) {
  ll cnt = 0;
  if (left + 1 < right) {
    int mid = (left + right) / 2;
    cnt += mergesort(left, mid);
    cnt += mergesort(mid, right);
    cnt += merge(left, mid, right);
  }
  return cnt;
}
int main() {
  int n;
  cin >> n;
  v.resize(n);
  rep(i, 0, n) cin >> v[i];
  P(mergesort(0, n));
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i, a, n) for (int i = (a), i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, n) for (int i = (a); i > n; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
inline int f(int n, vi &v) {
  int cnt = 0;
  rep(i, 0, n - 1) {
    rep(j, i + 1, n) {
      if (v[i] > v[j])
        ++cnt;
    }
  }
  return cnt;
}
int main() {
  int n;
  cin >> n;
  vi v(n);
  rep(i, 0, n) cin >> v[i];
  P(f(n, v));
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define SORT(c) sort((c).begin(), (c).end())
const double EPS = 1e-10;                 // sample:if((double)>=(double)+EPS)
#define CLR(a) memset((&a), 0, sizeof(a)) // clear memory
#define dump(x) cerr << #x << " = " << (x) << endl; // debug
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
// cin,cout release
// std::cin.tie(0);
// ios::sync_with_stdio(false);

unsigned long long int cnt = 0;
using namespace std;
void merge(int A[], int left, int mid, int right) {
  int n1 = mid - left, n2 = right - mid, L[300000], R[300000];
  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] = INT_MAX;
  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++];
      cnt += n1 - i;
    }
  }
}
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;
  cin >> n;
  int S[300000];
  for (int i = 0; i < n; i++)
    scanf("%d", &S[i]);
  mergeSort(S, 0, n);
  // for(int i=0;i<n-1;i++)cout<<S[i]<<" ";
  // cout<<S[n-1]<<endl;
  cout << cnt << endl;
  return 0;
}


Error: 0

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 (int i = 0; i < (n); ++i)
#define SORT(c) sort((c).begin(), (c).end())
const double EPS = 1e-10;                 // sample:if((double)>=(double)+EPS)
#define CLR(a) memset((&a), 0, sizeof(a)) // clear memory
#define dump(x) cerr << #x << " = " << (x) << endl; // debug
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
// cin,cout release
// std::cin.tie(0);
// ios::sync_with_stdio(false);

int cont = 0;

void bsort(int A[], int n) {
  bool flag = 1;
  while (flag) {
    flag = 0;
    for (int j = n - 1; j >= 1; j--) {
      if (A[j] < A[j - 1]) {
        swap(A[j], A[j - 1]);
        flag = 1;
        cont++;
      }
    }
  }
}

int main(void) {
  int n, a[100];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  bsort(a, n);
  // for(int i=0;i<n-1;i++)printf("%d ",a[i]);cout<<a[n-1]<<endl;
  cout << cont << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;
typedef long long int llint;
typedef vector<llint>::iterator it;

void quicker_settings() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
}

llint merge(vector<llint> &V, vector<llint> &L, vector<llint> &R) {
  it l = L.begin();
  it r = R.begin();
  llint c = 0;
  for (it v = V.begin(); v != V.end(); ++v) {
    if (r == R.end() || l != L.end() && *l < *r) {
      *v = *l;
      ++l;
    } else {
      *v = *r;
      c += (L.size() + distance(R.begin(), r) - distance(V.begin(), v));
      ++r;
    }
  }
  return c;
}

llint merge_idx(vector<llint> &v) {
  if (v.size() == 1)
    return 0;
  vector<llint> L;
  vector<llint> R;
  L.assign(v.begin(), v.begin() + (v.size() / 2));
  R.assign(v.begin() + (v.size() / 2), v.end());
  llint idx = 0;
  idx += merge_idx(L);
  idx += merge_idx(R);
  idx += merge(v, L, R);
  return idx;
}

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

  vector<llint> v;
  v.resize(n, 0);

  for (llint i = 0; i < n; i++) {
    cin >> v[i];
  }
  llint c = merge_idx(v);

  cout << c << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;
typedef long long int llint;

int idx(vector<llint> &v, int x) {
  if (v.size() == 0)
    return 0;
  vector<llint>::iterator lb = v.begin();
  vector<llint>::iterator ub = v.end();
  vector<llint>::iterator i = lb + distance(lb, ub) / 2;
  while (lb != ub) {
    if (x < *i) {
      ub = i;
      i = lb + distance(lb, ub) / 2;
    } else {
      lb = i + 1;
      i = lb + distance(lb, ub) / 2;
    }
  }
  return distance(v.begin(), i);
}

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

  int n;
  cin >> n;

  vector<llint> v;
  v.resize(n, 0);

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

  vector<llint> v2;
  v2.reserve(n);

  int c = 0;
  for (int i = 0; i < n; i++) {
    int id = idx(v2, v[v.size() - 1 - i]);
    c += id;
    v2.insert(v2.begin() + id, v[v.size() - 1 - i]);
  }

  cout << c << endl;

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

#include <algorithm>
#include <limits.h>
#include <stdio.h>

using namespace std;

long long int sum = 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] = R[n2] = INT_MAX;

  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++];
      sum += n1 - i;
    }
  }
}

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

  mergeSort(A, 0, n);

  printf("%lld\\n", sum);
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int n, tmp, left, right, min, max;
  long long count = 0;
  scanf("%d", &n);
  vector<int> A;
  scanf("%d", &tmp);
  min = max = tmp;
  A.push_back(tmp);
  for (int i = 1; i < n; i++) {
    scanf("%d", &tmp);
    if (tmp > max) {
      A.push_back(tmp);
      max = tmp;
    } else if (tmp < min) {
      A.insert(A.begin(), tmp);
      min = tmp;
      count += i;
    } else {
      left = 0;
      right = i - 1;
      while (left + 1 < right) {
        if (A[(left + right) / 2] > tmp) {
          right = (left + right) / 2;
        } else {
          left = (left + right) / 2;
        }
      }
      A.insert(A.begin() + right, tmp);
      count += (i - (left + 1));
    }
  }

  printf("%lld\\n", count);
  return 0;
}