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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include<bits/stdc++.h>
#include "message.h"
#include "teatr.h"
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 1e18L + 1;
const int IINF = 1e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

//#define LOCAL
#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

const ll lim = 1ll << 20, H = 1e6;

ll is, nds;

struct seg_tree{
	vector<ll>tab;
	seg_tree()
		:tab(2 * lim){}
	void add(ll pos){
		pos += lim;
		while(pos >= 1){
			tab[pos]++;
			pos >>= 1;
		}
	}
	ll sm(ll st, ll fn){
		st += lim; fn += lim;
		ll res = tab[st] + tab[fn];
		while(fn - st > 1){
			if((st & 1) == 0){
				res += tab[st + 1];
			}
			if((fn & 1) == 1){
				res += tab[fn - 1];
			}
			st >>= 1; fn >>= 1;
		}
		return res;
	}
};

ll do_small(ll st, ll fn){
	seg_tree segt;
	ll res = 0;
	rep(i, st, fn + 1){
		ll el = GetElement(i);
		res += segt.sm(el + 1, H);
		segt.add(el);
	}
	if(res){
		DBG(st, fn, res);
	}
	return res;
}

ll do_cross(ll st1, ll fn1, ll st2, ll fn2){
	vector<ll>tab(H + 1, 0);
	rep(i, st1, fn1 + 1){
		ll el = GetElement(i);
		tab[el]++;
	}
	for(ll i = H - 1; i >= 0; i--){
		tab[i] += tab[i + 1];
	}
	ll res = 0;
	rep(i, st2, fn2 + 1){
		ll el = GetElement(i);
		res += tab[el + 1];
	}
	if(res){
		DBG(st1, fn1, st2, fn2, res);
	}
	return res;
}

const ll LB = 14, SB = 5;

int main()
{
#ifndef LOCAL
	ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
	vector<pair<pair<ll, ll>, pair<ll, ll> > >tasks;
	is = MyNodeId(); nds = NumberOfNodes();
	ll n = GetN(), ls = n / LB, ss = n / LB / SB;
	vector<pair<ll, ll> >large_blocks;
	ll prev_end = -1;
	rep(i, 0, LB - 1){
		large_blocks.pb({prev_end + 1, prev_end + ls});
		prev_end += ls;
	}
	large_blocks.pb({prev_end + 1, n - 1});
	rep(i, 0, LB){
		rep(j, i + 1, LB){
			tasks.pb({large_blocks[i], large_blocks[j]});
		}
	}
	rep(i, 0, LB){
		vector<pair<ll, ll> >small_blocks;
		ll prev_end = (i == 0) ? -1 : large_blocks[i - 1].sd;
		rep(j, 0, SB - 1){
			small_blocks.pb({prev_end + 1, prev_end + ss});
			prev_end += ss;
		}
		small_blocks.pb({prev_end + 1, large_blocks[i].sd});
		rep(j, 0, SB){
			tasks.pb({{-1, -1}, small_blocks[j]});
			rep(k, j + 1, SB){
				tasks.pb({small_blocks[j], small_blocks[k]});
			}
		}
	}
	ll res = 0;
	for(ll i = is; i < SZ(tasks); i += nds){
		if(tasks[i].ft == make_pair(-1ll, -1ll)){
			res += do_small(tasks[i].sd.ft, tasks[i].sd.sd);
		} else{
			res += do_cross(tasks[i].ft.ft, tasks[i].ft.sd, tasks[i].sd.ft, tasks[i].sd.sd);
		}
	}
	DBG(is, res);
	if(is != 0){
		PutLL(0, res);
		Send(0);
	} else{
		rep(i, 1, nds){
			Receive(i);
			res += GetLL(i);
		}
		cout << res << "\n";
	}
    return 0;
}