p180.ans1
======================
3

=================
p180.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

class Fenwick {
  public:
    int n;
    vector<int> tr;

    Fenwick(int _n = 0) { init(_n); }

    void init(int _n) {
        n = _n;
        tr.assign(n + 1, 0);
    }

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

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

        return ret;
    }
};

int n;
vector<int> a;

void read() {
    cin >> n;
    a.resize(n);
    cin >> a;
}

void solve() {
    // Count inversions with a Fenwick tree over compressed values. We sort and
    // dedupe the values, map each element to its 1-based rank, then sweep left
    // to right inserting ranks. After inserting the i-th element (i + 1 seen so
    // far), the number of already-inserted values that are <= a[i] is
    // query(a[i]); the rest, i + 1 - query(a[i]), are strictly greater values
    // sitting to its left, i.e. inversions ending at position i.

    vector<int> li = a;
    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;
    }

    Fenwick fen(li.size() + 1);

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

    cout << answer << '\n';
}

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

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
p180.in1
======================
5
2 3 1 5 4

=================
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

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