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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#ifndef MW_HEADER
#define MW_HEADER
#include <bits/stdc++.h>
#include "message.h"
using namespace std;

#define FOR(i,a,b) for (LL i = (a); i < (b); ++i)
#define FORD(i,b,a) for (LL i = (LL)(b)-1; i >= (a); --i)
#define REP(i,N) FOR(i,0,N)
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define st first
#define nd second
#define pb push_back

typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;
#endif

LL NODES = NumberOfNodes(), ME = MyNodeId();
LL __N, START, END, NEXT, PREV;
LL get_start(int node) { return __N/NODES * node + min(__N%NODES, (LL)node); }
LL get_end(int node) { return get_start(node+1); }
/** Every node has at least factor items to process */
void reduce_nodes(LL N, int factor = 1) {
  NODES = min(NODES, max(N/factor, 1LL)); if (ME >= NODES) exit(0);
  NEXT = (ME + 1)%NODES, PREV=(ME+NODES-1)%NODES;
  __N = N;  START = get_start(ME), END = get_end(ME);
}

template <typename Container> struct is_container : false_type { };
template <typename... Ts> struct is_container<list<Ts...> > : true_type { };
template <typename... Ts> struct is_container<vector<Ts...> > : true_type { };
// template <typename... Ts> struct is_container<set<Ts...> > : true_type { };
// template <typename... Ts> struct is_container<map<Ts...> > : true_type { };
template <typename Container> struct is_pair : false_type { };
template <typename... Ts> struct is_pair<pair<Ts...>> : true_type { };
template <typename Container> struct is_triple : false_type { };
template <typename T1, typename T2, typename T3> struct is_triple<tuple<T1,T2,T3>> : true_type { };

template<typename T> void Put(int target, typename enable_if<!is_class<T>::value, const T&>::type value) { T::not_implemented; }
template<typename T> void Put(int target, typename enable_if<is_container<T>::value, const T&>::type vec);
template<> void Put<bool>(int target, const bool& value) { PutChar(target, value); }
template<> void Put<char>(int target, const char& value) { PutChar(target, value); }
template<> void Put<int>(int target, const int& value) { PutInt(target, value); }
template<> void Put<unsigned int>(int target, const unsigned int& value) { PutInt(target, value); }
template<> void Put<long long>(int target, const long long& value) { PutLL(target, value); }
template<> void Put<unsigned long long>(int target, const unsigned long long& value) { PutLL(target, value); }
// template<typename T> void Put(int target, typename enable_if<is_class<T>::value && !is_container<T>::value && !is_pair<T>::value, const T&>::type value){
//   char data[sizeof(T)]; memcpy(data, &value, sizeof(T));
//   REP(i, (int)sizeof(T)) PutChar(target, data[i]);
// }
template<typename T> void Put(int target, typename enable_if<is_pair<T>::value, const T&>::type pair) {
  Put<typename T::first_type>(target, pair.first);
  Put<typename T::second_type>(target, pair.second);
}
template<typename T> void Put(int target, typename enable_if<is_triple<T>::value, const T&>::type triple) {
  Put<typename tuple_element<0, T>::type>(target, get<0>(triple));
  Put<typename tuple_element<1, T>::type>(target, get<1>(triple));
  Put<typename tuple_element<2, T>::type>(target, get<2>(triple));
}
template<typename T> void Put(int target, typename enable_if<is_container<T>::value, const T&>::type vec) {
  Put<int>(target, vec.size());
  for (auto v: vec) { Put<typename T::value_type>(target, v); }
}

template<typename T> typename enable_if<!is_class<T>::value, T>::type Get(int source) { T::not_implemented; }
template<typename T> typename enable_if<is_container<T>::value, T>::type Get(int source);
template<> bool Get<bool>(int source) { return GetChar(source); }
template<> char Get<char>(int source) { return GetChar(source); }
template<> int Get<int>(int source) { return GetInt(source); }
template<> unsigned int Get<unsigned int>(int source) { return GetInt(source); }
template<> long long Get<long long>(int source) { return GetLL(source); }
template<> unsigned long long Get<unsigned long long>(int source) { return GetLL(source); }
// template<typename T> typename enable_if<is_class<T>::value && !is_container<T>::value && !is_pair<T>::value, T>::type Get(int source) {
//   char data[sizeof(T)]; REP(i, (int)sizeof(T)) data[i] = GetChar(source);
//   T value; memcpy(&value, data, sizeof(T));
//   return value;
// }
template<typename T> typename enable_if<is_pair<T>::value, T>::type Get(int source) {
  auto f = Get<typename T::first_type>(source);
  auto s = Get<typename T::second_type>(source);
  return T(f, s);
}
template<typename T> typename enable_if<is_triple<T>::value, T>::type Get(int source) {
  auto f = Get<typename tuple_element<0, T>::type>(source);
  auto s = Get<typename tuple_element<1, T>::type>(source);
  auto t = Get<typename tuple_element<2, T>::type>(source);
  return T(f, s, t);
}
template<typename T> typename enable_if<is_container<T>::value, T>::type Get(int source) {
  int size = GetInt(source);
  vector<typename T::value_type> result; result.reserve(size);
  REP(i,size) result.push_back(Get<typename T::value_type>(source));
  return T(result.begin(), result.end());
}

template<typename T> void Broadcast(int source, T& value) {
  if (ME == source) REP(i,NODES) { Put<T>(i, value); Send(i); }
  Receive(source); value = Get<T>(source);
}

template<typename T> void BroadcastTree(int source, T& value) {
  int relative = (ME - source + NODES) % NODES;
  if (relative) {
    int from = (source + (relative-1) / 2) % NODES;
    Receive(from); value = Get<T>(from);
  }
  FOR(i,1,3) if (2*relative + i < NODES) {
    int to = ((source + 2*relative + i) % NODES);
    Put<T>(to, value); Send(to);
  }
}

template<typename T, typename Fn> void Accumulate(int target, const T& value, Fn fn) {
  Put<T>(target, value); Send(target);
  if (ME == target) REP(i, NODES) { Receive(i); fn(Get<T>(i)); }
}

/** ~5ms for adding ints */
template<typename T, typename Fn> T AccumulateTree(int target, T value, Fn fn) {
  int relative = (ME - target + NODES) % NODES;
  for (int b = 1; b < NODES; b <<= 1) {
    if (relative&b) {
      int to = (target + relative - b + NODES) % NODES;
      Put<T>(to, value); Send(to); break;
    } else if (relative + b < NODES) {
      int from = (target + relative + b) % NODES;
      Receive(from); value = fn(value, Get<T>(from));
    }
  }

  return value;
}

template<typename T, typename Compute, typename Acc> T AccumulateValues(int target, Compute fn, Acc acc) {
  T result = fn(START);
  FOR(i,START+1,END) result = acc(result, fn(i));
  return AccumulateTree(target, result, acc);
}

template<typename T, typename Compute> vector<T> AccumulateVector(int target, Compute fn) {
  vector<T> local; FOR(i,START, END) local.pb(fn(i));
  vector<T> acc;
  Accumulate(target, local, [&](const vector<T>& vec) {acc.insert(acc.end(), vec.begin(), vec.end());});
  return acc;
}

template<typename T> vector<T> Collect(int target, const T& value) {
  vector<T> acc;
  Accumulate(target, value, [&](const T& val) { acc.pb(val); });
  return acc;
}
// ^^^ CUT HERE ^^^
#include "teatr.h"

int N = GetN();
const int MAX_ELEMENT = 1000001;
int counts[MAX_ELEMENT];

LL data[MAX_ELEMENT];
// value[n] += x
void add(int n, int x) { for (; n < MAX_ELEMENT; n |= n + 1) data[n] += x; }
// Returns value[0] + value[1] + ... + value[n]
LL sum(int n) { LL s=0; while (n>=0) { s+=data[n]; n=(n&(n+1))-1; } return s; }

int main() {
  reduce_nodes(N);
  FORD(i,N,END) ++counts[GetElement(i)];
  REP(i,MAX_ELEMENT) add(i, counts[i]);

  LL result = 0;
  FORD(i,END,START) {
    int k = GetElement(i);
    result += sum(k-1);
    add(k, 1);
  }
  if (ME != 0) {
    PutLL(0, result);
    Send(0);
    return 0;
  }
  FOR(i,1,NODES) {
    Receive(i);
    result += GetLL(i);
  }
  printf("%lld\n", result);
}