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

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

// ================= 代码实现开始 =================

/* 请在这里定义你需要的全局变量 */

// 这个函数的功能是计算答案（即最少花费的金钱）
// n：表示序列长度
// a：存储整个序列 a
// 返回值：最少花费的金钱（需要注意，返回值的类型为 64 位有符号整数）
long long merge(int start, int mid, int end, vector<int> &a) {
  long long ans = 0;
  vector<int> b;
  int i = start, j = mid, k = 0;
  while (i < mid && j < end) {
    if (a[i] < a[j])
      b.push_back(a[i++]); // 左边的数比较小
    else if (a[i] > a[j]) {
      b.push_back(a[j++]);
      ans += (mid - i);
    } else if (a[i] == a[j]) {
      b.push_back(a[i++]);
      ans += (mid - i);
      b.push_back(a[j++]);
    }
  }
  while (i < mid) {
    b.push_back(a[i++]);
  } // 左边有剩余，左边剩余的都比右边大
  while (j < end) {
    b.push_back(a[j++]);
  } // 右边还有剩余，右边剩余的比左边都大
  for (int m = 0; m < b.size(); m++) {
    a[start + m] = b[m];
    // cout<<"a[ "<<start+m<<"]="<<b[m]<<endl;
  }
  b.clear();
  // cout<<"merge ans= "<<ans<<endl;
  return ans;
}
long long getAnswer(int start, int end, vector<int> &a) { // end不包括
  if (end - start < 2)
    return 0;
  if (end - start == 2) {
    if (a[start] > a[start + 1]) {
      int tmp = a[start];
      a[start] = a[start + 1];
      a[start + 1] = tmp;
      // cout<<"start = "<<start<<"; end= "<<end<<"; " <<"ans ="<<1<<endl;
      return 1;
    } else {
      // cout<<"start = "<<start<<"; end= "<<end<<"; "<<"ans ="<<0 <<endl;
      return 0;
    }
  }
  int mid = (start + end) >> 1;
  long long ans = getAnswer(start, mid, a) + getAnswer(mid, end, a) +
                  merge(start, mid, end, a);
  // cout<<"start = "<<start<< "; mid = "<<mid<<"; end= "<<end<<"; " <<"ans
  // ="<<ans<<endl;
  return ans;
  /* 请在这里设计你的算法 */
}

// ================= 代码实现结束 =================

int main() {
  int n, tmp;
  vector<int> a;
  a.clear();
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &tmp);
    a.push_back(tmp);
  }
  long long ans = getAnswer(0, n, a);
  cout << ans << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<pair<int, int>> array;
  vector<int> b;
  int num, tmp;
  long int count = 0;
  cin >> num;
  for (int i = 0; i < num; i++) {
    cin >> tmp;
    array.emplace_back(tmp, i);
    b.push_back(tmp);
  }
  sort(array.begin(), array.end());
  map<int, int> a;
  for (int i = 0; i < num; i++) {
    count += find(b.begin(), b.end(), array[i].first) - b.begin();
    b.erase(find(b.begin(), b.end(), array[i].first));
  }
  printf("%lu\\n", count);
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Runtime Error

#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 n, int left, int mid, int right) {
  int i, j, k;
  ll 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;
}

ll mergesort(int A[], int n, int left, int right) {
  int mid;
  ll 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[MAX], n, i;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> A[i];
  }
  ll ans = mergesort(A, n, 0, n);
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

#define MAX 19980328
#define SENTINEL 19980328777
typedef long long ll;

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

ll merge(int A[], int n, int left, int mid, int right) {
  int i, j, k;
  ll 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] = L[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;
}

ll mergesort(int A[], int n, int left, int right) {
  int mid;
  ll 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[MAX], n, i;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> A[i];
  }
  ll ans = mergesort(A, n, 0, n);
  cout << ans << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
const int maxn = 200000;
const long long big = 2000000000;
int L[maxn / 2 + 2], R[maxn / 2 + 2];
int A[maxn + 10];
long long zhenghe(int A[], int left, int mid, int right) {
  long long 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] = big;
  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;
    }
  }
  return cnt;
}
long long guibing(int A[], int left, int right) {
  int mid;
  long long v1, v2, v3;
  if (left + 1 < right) {
    mid = (left + right) / 2;
    v1 = guibing(A, left, mid);
    v2 = guibing(A, mid, right);
    v3 = zhenghe(A, left, mid, right);
    return v1 + v2 + v3;
  } else
    return 0;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);
  long long ans = guibing(A, 0, n);
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 100000
#define SENTINEL 2000000000
typedef long long llong;
int L[MAX / 2 + 2], 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 += mid + j - k - 1;
      ;
    }
  }
  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 n, i, A[MAX];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> A[i];
  }
  llong ans = mergeSort(A, n, 0, n);
  cout << ans << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define eps 1e-10;
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 2e5 + 100;

int n, a[maxn], tree[maxn];
vector<int> v;
LL ans = 0;
int getid(int x) { return lower_bound(v.begin(), v.end(), x) - v.begin() + 1; }
int lowbit(int x) { return x & (-x); }
void add(int x, int y) {
  for (int i = x; i <= n + 2; i += lowbit(i))
    tree[i] += y;
}
LL getsum(int x) {
  LL ret = 0;
  for (int i = x; i; i -= lowbit(i))
    ret += tree[i];
  return ret;
}

int main() {
#ifdef ac
  freopen("in.txt", "r", stdin);
  // freopen("out.txt","w",stdout);
#endif // ac
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i], v.push_back(a[i]);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  for (int i = n; i; --i) {
    int id = getid(a[i]);
    ans += getsum(id);
    add(id + 1, 1);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

typedef long long ll;

const long maxsize = 200005;

typedef struct {
  ll a[maxsize];
  long length;
} number;

void s(number *A, long j) {
  ll t;
  t = A->a[j];
  A->a[j] = A->a[j - 1];
  A->a[j - 1] = t;
}

ll bubbleSort(number *A) {
  ll cnt = 0;
  for (ll i = 0; i < A->length; i++)
    for (ll j = A->length - 1; j > i; j--) {
      if (A->a[j] < A->a[j - 1]) {
        s(A, j);
        cnt++;
      }
    }
  return cnt;
}

int main() {
  long n;
  number A;
  cin >> n;
  A.length = n;
  for (int i = 0; i < n; i++)
    cin >> A.a[i];
  cout << bubbleSort(&A) << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
long long a[200001], b[200001];
long long x = 0;
void Merge(long long A[], long long B[], int left, int mid, int right) {
  int n1, n2;
  n1 = left;
  n2 = mid + 1;
  int k = left;
  while (n1 != mid + 1 && n2 != right + 1) {
    if (A[n1] > A[n2]) {
      B[k++] = A[n2++];
      x = x + mid + 1 - n1;
    } else
      B[k++] = A[n1++];
  }
  while (n1 != mid + 1)
    B[k++] = A[n1++];
  while (n2 != right + 1)
    B[k++] = A[n2++];
  for (int i = left; i <= right; i++)
    A[i] = B[i];
  return;
}
void MergeSort(long long A[], long long B[], int left, int right) {
  int mid;
  if (left < right) {
    mid = (left + right) / 2;
    MergeSort(A, B, left, mid);
    MergeSort(A, B, mid + 1, right);
    Merge(A, B, left, mid, right);
  }
  return;
}
int main() {
  long long n;
  scanf("%d", &n);
  for (long long i = 0; i < n; i++)
    scanf("%d", &a[i]);
  MergeSort(a, b, 0, n - 1);
  printf("%ld\\n", x);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int main() {
  int n, a[100001], j, k, num, i, t;
  while (scanf("%d", &n) != EOF) {
    num = 0;
    for (i = 1; i <= n; i++) {
      scanf("%d", &a[0]);
      k = j = i;
      if (i == 1) {
        a[1] = a[0];
      } else {
        j--;
        while (j) {
          if (a[0] > a[j] || a[0] == a[j])
            break;
          j--;
        }
        a[i] = a[0];
        num = num + i - j - 1;
        sort(a + 1, a + i + 1);
      }
    }
    printf("%d\\n", num);
  }
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

long long count = 0;
int arr[500005];
int tempArr[500005];
void mergeSort(int l, int r) {
  if (l == r)
    return;

  int mid = (l + r) / 2;

  mergeSort(l, mid);
  mergeSort(mid + 1, r);

  int idx = l;
  int leftIdx = l;
  int toLeft = mid;
  int rightIdx = mid + 1;
  int toRight = r;

  while (leftIdx <= toLeft && rightIdx <= toRight) {
    if (arr[leftIdx] < arr[rightIdx]) {
      tempArr[idx++] = arr[leftIdx++];
    } else {
      tempArr[idx++] = arr[rightIdx++];
      count += (mid + 1) - leftIdx;
    }
  }

  while (leftIdx <= toLeft) {
    tempArr[idx++] = arr[leftIdx++];
  }
  while (rightIdx <= toRight) {
    tempArr[idx++] = arr[rightIdx++];
  }

  for (int i = l; i <= r; i++) {
    arr[i] = tempArr[i];
  }
}

int main() {

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

  mergeSort(0, n - 1);

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

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF 0x3f3f3f3f
#define MAXM 5000 + 10
#define MAXN 500000 + 10

using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;

int n;
int ans = 0;
int num[MAXN];

void bubbleSort(int a[]) {
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j >= i + 1; j--) {
      if (a[j] < a[j - 1]) {
        swap(a[j], a[j - 1]);
        ans++;
      }
    }
  }
}

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

  bubbleSort(num);

  //    for(int i = 0; i < n; i ++)
  //        printf("%d%c", num[i], i == n - 1 ? '\\n' : ' ');
  printf("%d\\n", ans);
}

/*

The WAM is F**KING interesting .

*/
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define i_7 (ll)(1E9 + 7)
#define i_5 (ll)(1E9 + 5)
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  else
    return c + i_7;
}
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
ll inf = (ll)1E12; /*10^12*/
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}

/////////////////////////////////////////////////////
//[1,n]
#define max_n                                                                  \\
  300000 // 調節！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
int bit[max_n + 1], n;
int sum(int i) { // a[1]~a[i]の和を求める
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}
void add(int i, int x) { // a[i]にxを加える
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}
////////////////////////////////////////
int main() {
  cin >> n;
  int a[n];
  rep(i, 0, n - 1) cin >> a[i];
  vector<int> v;
  rep(i, 0, n - 1) v.pb(a[i]);
  sort(v.begin(), v.end());
  // v.erase(unique(v.begin(),v.end()),v.end());
  rep(i, 0, n - 1) {
    a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1;
  }
  // rep(i,0,n-1)cout<<a[i]<<" ";
  ll ans = 0;
  rep(i, 0, n - 1) {
    ans += i - sum(a[i]);
    add(a[i], 1);
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define i_7 (ll)(1E9 + 7)
#define i_5 (ll)(1E9 + 5)
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  else
    return c + i_7;
}
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
ll inf = (ll)1E12; /*10^12*/
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}

/////////////////////////////////////////////////////
//[1,n]
#define max_n                                                                  \\
  300000 // 調節！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
int bit[max_n + 1], n;
int sum(int i) { // a[1]~a[i]の和を求める
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}
void add(int i, int x) { // a[i]にxを加える
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}
////////////////////////////////////////
int main() {
  n = max_n;
  int N;
  cin >> N;
  int a[N];
  rep(i, 0, N - 1) cin >> a[i];
  ll ans = 0;
  rep(i, 0, N - 1) {
    ans += sum(max_n) - sum(a[i]);
    add(a[i], 1);
    // cout<<ans<<endl;
  }
  cout << ans << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void merge(vector<int> &A, int left, int mid, int right, long long int &count) {
  int n1 = mid - left;
  int n2 = right - mid;
  vector<int> L, R;
  L.resize(n1 + 1);
  R.resize(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] = numeric_limits<int>::max();
  R[n2] = numeric_limits<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 {
      count += n1 - i;
      A[k] = R[j];
      j = j + 1;
    }
  }
}

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

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

Error: TLE

Fixed submission:

#include <iostream>
#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() {
  int n;
  cin >> n;
  vector<int> A;
  A.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  int cnt = bubbleSort(A);
  cout << cnt << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <assert.h>
#include <iostream>
using namespace std;
#define int long long
int tempmarge[1000100];
int N, A[10000000];

int MargeSort(int x[], int left, int right) {
  int mid, i, j, k, cl, cr, c;
  if (left >= right)
    return 0;
  mid = (left + right) / 2;
  cl = MargeSort(x, left, mid);
  cr = MargeSort(x, mid + 1, right);

  for (i = left; i <= mid; i++)
    tempmarge[i] = x[i];
  for (i = mid + 1, j = right; i <= right; i++, j--)
    tempmarge[i] = x[j];
  i = left;
  j = right;
  c = 0;
  for (k = left; k <= right; k++)
    if (tempmarge[i] <= tempmarge[j])
      x[k] = tempmarge[i++];
    else {
      c += mid + 1 - i;
      x[k] = tempmarge[j--];
    }
  return c + cl + cr;
}

signed main() {

  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  cout << MargeSort(A, 0, N - 1) << endl;
  return 0;
}

Error: 0
1
2
3
4
5


Fixed submission:

#include <algorithm>
#include <assert.h>
#include <iostream>
using namespace std;
#define int long long
int tempmarge[1000100];
int N, A[10000000];

int MargeSort(int x[], int left, int right) {
  int mid, i, j, k, cl, cr, c;
  if (left >= right)
    return 0;
  mid = (left + right) / 2;
  cl = MargeSort(x, left, mid);
  cr = MargeSort(x, mid + 1, right);

  for (i = left; i <= mid; i++)
    tempmarge[i] = x[i];
  for (i = mid + 1, j = right; i <= right; i++, j--)
    tempmarge[i] = x[j];
  i = left;
  j = right;
  c = 0;
  for (k = left; k <= right; k++)
    if (tempmarge[i] <= tempmarge[j])
      x[k] = tempmarge[i++];
    else {
      c += mid + 1 - i;
      x[k] = tempmarge[j--];
    }
  return c + cl + cr;
}

signed main() {

  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  cout << MargeSort(A, 0, N - 1) << endl;
  for (int i = 0; i < N; i++)
    cerr << A[i] << endl;
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define MAX_N 200001

using namespace std;

typedef long long int LL;

struct fenwick_tree {
  int N;
  vector<int> bit;
  fenwick_tree(int num) : bit(num + 1, 0) { N = num; }
  int Sum(int a) {
    int ret = 0;
    for (int x = a; x > 0; x -= x & -x) {
      ret += bit[x];
    }
    return ret;
  }
  void Add(int a, int w) {
    for (int x = a; x <= N; x += x & -x) {
      bit[x] += w;
    }
  }
};

int main() {
  int n, a[MAX_N], b[MAX_N];
  LL ans = 0;
  map<int, int> mp;

  cin >> n;
  fenwick_tree bit(n);

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

  sort(b, b + n);

  for (int i = 0; i < n; i++) {
    mp[b[i]] = i + 1;
  }
  for (int i = 0; i < n; i++) {
    ans += i - bit.Sum(mp[a[i]]);
    bit.Add(mp[a[i]], 1);
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

#define MAX_N 200001

using namespace std;

typedef long long int LL;

struct fenwick_tree {
  int N;
  vector<int> bit;
  fenwick_tree(int num) : bit(num + 1, 0) { N = num; }
  int Sum(int a) {
    int ret = 0;
    for (int x = a; x > 0; x -= x & -x) {
      ret += bit[x];
    }
    return ret;
  }
  void Add(int a, int w) {
    for (int x = a; x <= N; x += x & -x) {
      bit[x] += w;
    }
  }
};

int main() {
  int n, a[MAX_N];
  LL ans = 0;

  cin >> n;
  fenwick_tree bit(n);

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans += i - bit.Sum(a[i]);
    bit.Add(a[i], 1);
  }

  cout << ans << endl;

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

#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>

using namespace std;

using i64 = long long;

template <typename T> struct BinaryIndexedTree {
  size_t n;
  vector<T> tree;
  BinaryIndexedTree(size_t n, T e = 0) : n(n) { tree = vector<T>(n + 1, e); }
  void add(size_t i, T w) {
    // v[i] += w;
    // i in [1, n]
    for (size_t j = i; j <= n; j += j & -j)
      tree[j] += w;
  }
  T sum(T m) {
    // returns sum of v[i] for i in [1, m]
    T sum_ = 0;
    for (size_t i = m; i > 0; i -= i & -i)
      sum_ += tree[i];

    return sum_;
  }
};

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

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

  map<int, size_t> m;
  for (size_t i = 0; i < n; ++i)
    m[a[i]] = i;

  vector<int> b(n);
  int rank_ = 1;
  for (pair<const int, size_t> p : m)
    b[p.second] = rank_++;

  BinaryIndexedTree<i64> tree(n + 1);
  i64 res = 0;
  for (size_t i = 0; i < n; ++i) {
    res += i - tree.sum(b[i]);
    tree.add(b[i], 1);
  }

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

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

using i64 = long long;

template <typename T> struct BinaryIndexedTree {
  size_t n;
  vector<T> tree;
  BinaryIndexedTree(size_t n, T e = 0) : n(n) { tree = vector<T>(n + 1, e); }
  void add(size_t i, T w) {
    // v[i] += w;
    // i in [1, n]
    for (size_t j = i; j <= n; j += j & -j)
      tree[j] += w;
  }
  T sum(T m) {
    // returns sum of v[i] for i in [1, m]
    T sum_ = 0;
    for (size_t i = m; i > 0; i -= i & -i)
      sum_ += tree[i];

    return sum_;
  }
};

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

  vector<int> a(n);
  int max_ = 0;
  for (size_t i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    ++a[i];
    if (max_ < a[i])
      max_ = a[i];
  }

  BinaryIndexedTree<i64> tree(max_ + 1);
  i64 res = 0;
  for (size_t i = 0; i < n; ++i) {
    res += i - tree.sum(a[i]);
    tree.add(a[i], 1);
  }

  printf("%lld\\n", res);
  return 0;
}
Problem p02274 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 200005;

int a[MAX_N];
int k[MAX_N];
int bit[MAX_N + 1], n;
map<int, int> mp;

// i???????????§??????????¨??????????(i???1????????????)
int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}
// i???????????????x????????????(i???1????????????)
void add(int i, int x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

int main() {
  cin >> n;
  rep(i, n) {
    scanf("%d", &a[i]);
    k[i] = a[i];
    mp[a[i]] = i;
  }
  sort(a, a + n);
  rep(i, n) { k[mp[a[i]]] = i + 1; }
  ll ans = 0;
  rep(j, n) {
    ans += j - sum(k[j]);
    add(k[j], 1);
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 100005;

int a[MAX_N];
int bit[MAX_N + 1], n;

// i???????????§??????????¨??????????(i???1????????????)
int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}
// i???????????????x????????????(i???1????????????)
void add(int i, int x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

int main() {
  cin >> n;
  rep(i, n) { scanf("%d", &a[i]); }
  ll ans = 0;
  rep(j, n) {
    ans += j - sum(a[j]);
    add(a[j], 1);
  }
  printf("%lld\\n", ans);
  return 0;
}