1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "message.h"
#include "teatr.h"
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;

template <typename T>
struct fenwick {
	vector<T> fen;
	int n;//size

	fenwick(int _n) : n(_n) {
		fen.resize(n);
	}

	inline int lsb(int x) {
		return x & -x;
	}

	inline void modify(int x, T val) {
		for (int i = x; i < n; i += lsb(i)) {
			fen[i] += val; // main operation
		}
	}

	inline T get(int v) {
		T res{};
		for (int i = v; i > 0; i -= lsb(i)) {
			res += fen[i]; // main operation
		}
		return res;
	}
};

int main()
{
	if (MyNodeId() != 0) return 0;

	fenwick<int> fen(1 << 20);

	int n = GetN();

	ll res = 0;
	for (int i = n - 1; i >= 0; i--)
	{
		int x = GetElement(i);

		res += fen.get(x - 1);
		fen.modify(x, 1);
	}
	cout << res;

	return 0;
}