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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include"teatr.h"
#include"message.h"
#include<iostream>
#include<vector>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector< ii > vii;
typedef vector< pair < ii, int > > viii;
typedef vector< vector < ii > > vvii;
typedef pair < pair < int, int >, int >  iii;
typedef pair < pair < int, int >, pair < int, int > > iiii;
typedef unsigned long long ull;
typedef long long ll;
typedef unsigned long long ull;
typedef vector< ll > vll;
typedef long double ld;

#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define st first
#define nd second
#define rep(i,n) for(int i=0; i<(n); ++i)
#define rall(c) (c).rbegin(), (c).rend()
#define FOR(i, a, b)    for (int (i)=(a); (i)<(b); (i)++)
#define FOR2(i, a, b)    for (int (i)=(a); (i)>=(b); (i)--)

template<typename T> inline void setmin(T &x, T y) { if (y < x) x = y; }
template<typename T> inline void setmax(T &x, T y) { if (y > x) x = y; }
template<typename T> inline T gcd(T a, T b) { while (b)swap(a %= b, b); return a; }

const int MAX = 1e6 + 7;
const int T = 1 << 20;
const int INF = 1e9 + 7;
const ll BIG_INF = 1e18 + 5;

ld e = 2.7182818284590452353602874713526624;
//ld PI = acos(-1);
ld eps = 1e-19;

int tree[ T << 1 ];

void udpate( int poz ){
	poz += T;

	while( poz ){
		++tree[poz];
		poz >>= 1;
	}
}

int ask( int pocz , int kon ){
	pocz += T , kon += T;
	int ret = tree[ pocz ] + tree[kon];
	while( pocz >> 1 != kon >> 1 ){
		if( !(pocz & 1) )
			ret += tree[pocz + 1];
		if( kon & 1 )
			ret += tree[kon - 1];
		pocz >>= 1;
		kon >>= 1;
	}
	return ret;
}

int main(){
	boost;

	ll wynik = 0;
	int n = GetN();
	for( int i = 0 ; i < min( MyNodeId() * 1000000 , n ) ; ++i ){
		++tree[ T + GetElement(i) ];
	}
	for( int i = T - 1 ; i > 0 ; --i ){
		tree[i] = tree[i*2] + tree[ i*2 + 1 ];
	}
	for( int i = MyNodeId() * 1000000 ; i < min( n , ( MyNodeId() + 1 ) * 1000000 ) ; ++i ){
		int w = GetElement(i);
		wynik += ask( w + 1 , MAX );
		udpate( w );
	}

	if( MyNodeId() > 0 ){
		PutLL(0,wynik);
		Send(0);
	}
	else{
		for( int i = 1 ; i < NumberOfNodes() ; i++ ){
			Receive(i);
			wynik += GetLL(i);
		}
		cout << wynik << endl;
	}
}