p180.cpp
======================
#include <bits/stdc++.h>
#define endl '\n'

// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")

using namespace std;
template<class T, class T2>
inline void chkmax(T& x, const T2& y) {
    if(x < y) {
        x = y;
    }
}
template<class T, class T2>
inline void chkmin(T& x, const T2& y) {
    if(x > y) {
        x = y;
    }
}
const int MAXN = (1 << 16);

int n;
int a[MAXN + 42];

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

vector<int> li;

int sz;
int tr[MAXN + 42];

void init() {
    sz = li.size() + 1;
    memset(tr, 0, sizeof(tr));
}

void add(int x) {
    for(; x <= sz; x += (x & -x)) {
        tr[x]++;
    }
}

int query(int x) {
    int ret = 0;
    for(; x > 0; x -= (x & -x)) {
        ret += tr[x];
    }

    return ret;
}

void solve() {
    for(int i = 0; i < n; i++) {
        li.push_back(a[i]);
    }

    sort(li.begin(), li.end());
    li.erase(unique(li.begin(), li.end()), li.end());

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

    init();

    uint32_t answer = 0;
    for(int i = 0; i < n; i++) {
        add(a[i]);
        answer += i + 1 - query(a[i]);
    }

    cout << answer << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    read();
    solve();
    return 0;
}

=================
statement.txt
======================
180. Inversions
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard
output: standard



There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount of such pairs (i, j) that 1<=i<j<=N and A[i]>A[j].

Input
The first line of the input contains the number N. The second line contains N numbers A1...AN.

Output
Write amount of such pairs.

Sample test(s)

Input
5
2 3 1 5 4

Output
3
Author:	Stanislav Angelyuk
Resource:	Saratov ST team Spring Contest #1
Date:	18.05.2003

=================
