<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

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

<|response|>
1. Abridged Problem Statement  
Given an array A of N integers (1 ≤ N ≤ 65 537, 0 ≤ Ai ≤ 10^9), count how many index pairs (i, j) satisfy i < j and A[i] > A[j].

2. Key Observations  
- A brute-force double loop is O(N²), which is too slow for N up to ~65 000.  
- We need an O(N log N) method. Two classic approaches:  
  a) Merge-sort-based inversion counting.  
  b) Fenwick Tree (Binary Indexed Tree, BIT) with coordinate compression.  
- Fenwick Tree approach outline:  
  • Sweep the array from left to right.  
  • Maintain a data structure that counts how many previously seen elements exceed the current one.  
  • Since Ai can be as large as 10^9, first compress values into the range [1..N].

3. Full Solution Approach  
Step 1: Coordinate Compression  
- Copy all A[i] into a new list “vals.”  
- Sort “vals” and remove duplicates.  
- Map each original A[i] to its 1-based rank in “vals.” This rank lies in [1..M], where M ≤ N.

Step 2: Fenwick Tree (BIT)  
- Create a BIT of size M, initialized to zero.  
- BIT supports two operations in O(log M):  
  • add(pos, 1): increment the count at index pos.  
  • sum(pos): return the total count in [1..pos].

Step 3: Count Inversions  
- Initialize inversions = 0.  
- For i from 0 to N−1:  
   1. Let r = rank(A[i]).  
   2. smaller_or_equal = BIT.sum(r) = number of seen elements ≤ A[i].  
   3. seen_so_far = i.  
   4. So elements > A[i] among seen = i − smaller_or_equal. Add that to inversions.  
   5. BIT.add(r, 1) to mark A[i] as seen.

Overall complexity: O(N log N) time, O(N) extra space.

4. C++ Implementation with Detailed Comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

// Fenwick Tree (BIT) for point updates and prefix sums
struct Fenwick {
    int n;
    vector<int> f;
    Fenwick(int _n): n(_n), f(n+1, 0) {}
    // add v at position i
    void add(int i, int v) {
        for (; i <= n; i += i & -i) {
            f[i] += v;
        }
    }
    // sum of [1..i]
    int sum(int i) const {
        int s = 0;
        for (; i > 0; i -= i & -i) {
            s += f[i];
        }
        return s;
    }
};

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

    int N;
    cin >> N;
    vector<long long> A(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }

    // Step 1: Coordinate compression
    vector<long long> vals = A;
    sort(vals.begin(), vals.end());
    vals.erase(unique(vals.begin(), vals.end()), vals.end());
    // Map each A[i] to its rank in [1..M]
    int M = vals.size();
    vector<int> rankA(N);
    for (int i = 0; i < N; i++) {
        // lower_bound returns iterator to A[i]'s position in vals
        rankA[i] = int(lower_bound(vals.begin(), vals.end(), A[i]) - vals.begin()) + 1;
    }

    // Step 2: Initialize Fenwick tree of size M
    Fenwick bit(M);

    // Step 3: Sweep and count inversions
    long long inversions = 0;
    for (int i = 0; i < N; i++) {
        int r = rankA[i];
        // number of seen elements ≤ A[i]
        int cnt_le = bit.sum(r);
        // seen so far = i, so count of seen > A[i] is i - cnt_le
        inversions += (i - cnt_le);
        // mark A[i] as seen
        bit.add(r, 1);
    }

    cout << inversions << "\n";
    return 0;
}
```

5. Python Implementation with Detailed Comments  
```python
import sys

def main():
    data = sys.stdin.read().split()
    n = int(data[0])
    A = list(map(int, data[1:]))

    # 1. Coordinate compression
    vals = sorted(set(A))              # sorted unique values
    rank = {v: i+1 for i, v in enumerate(vals)}  # map value -> rank
    M = len(vals)

    # 2. Fenwick Tree implementation
    class Fenwick:
        def __init__(self, n):
            self.n = n
            self.fw = [0] * (n + 1)
        # point update: add v at index i
        def add(self, i, v=1):
            while i <= self.n:
                self.fw[i] += v
                i += i & -i
        # prefix sum [1..i]
        def sum(self, i):
            s = 0
            while i > 0:
                s += self.fw[i]
                i -= i & -i
            return s

    bit = Fenwick(M)
    inversions = 0

    # 3. Sweep through A
    for i, x in enumerate(A):
        r = rank[x]
        # how many seen elements ≤ x
        cnt_le = bit.sum(r)
        # seen so far = i, so seen > x = i - cnt_le
        inversions += (i - cnt_le)
        # mark this value
        bit.add(r, 1)

    # 4. Output result
    print(inversions)

if __name__ == "__main__":
    main()
```