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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <algorithm>
#include <cstdio>
#include <utility>
#include <vector>

#include "teatr.h"
#include "message.h"


namespace pk
{


template<class ValueType, int Range>
class binary_indexed_tree
{
public:
    typedef ValueType value_type;

    binary_indexed_tree()
    {
        std::fill(data, data + Range, value_type());
    }

    void increase(const int index, const value_type& value)
    {
        for(int i = index; i < Range; i |= i + 1)
            data[i] += value;
    }

    void set(const int index, const value_type& value)
    {
        const value_type old_value = count_in_range(index, index);
        increase(index, value - old_value);
    }

    value_type count_less_equal(int index) const
    {
        value_type sum = value_type();

        while(index >= 0)
        {
            sum += data[index];
            index &= index + 1;
            --index;
        }

        return sum;
    }

    value_type count_in_range(const int from, const int to) const
    {
        return (count_less_equal(to) - count_less_equal(from - 1));
    }

private:
    value_type data[Range];
};


} // namespace pk


const int MAX_ELEMENT = 1000009;
const int PARTITION = 5000000;


class TeaSolutionSingle
{
public:
    TeaSolutionSingle(const int n_) : n(n_) {}

    void solve()
    {
        pk::binary_indexed_tree<int, MAX_ELEMENT> bit;
        long long result = 0LL;

        for(int i = 0; i < n; ++i)
        {
            const int v = GetElement(i);

            result += bit.count_in_range(v+1, MAX_ELEMENT-1);
            bit.increase(v, 1);
        }

        printf("%lld\n", result);
    }

private:
    const int n;
};


std::vector<std::pair<int, int>> get_ranges_for_regular_check(const int node_id)
{
    const int n = GetN();

    std::vector<std::pair<int, int>> ranges;

    for(int i = 0; i < n; i += PARTITION)
        ranges.emplace_back(i, std::min(i + PARTITION - 1, n - 1));

    return ranges;
}


struct ProblemPart
{
    ProblemPart() {}
    ProblemPart(
            const int from1_,
            const int to1_,
            const int from2_,
            const int to2_)
            : from1(from1_),
              to1(to1_),
              from2(from2_),
              to2(to2_)
    {}

    int from1, to1;
    int from2, to2;
};


std::vector<ProblemPart> get_problem_parts()
{
    const int n = GetN();

    std::vector<std::pair<int, int>> ranges;

    for(int i = 0; i < n; i += PARTITION)
        ranges.emplace_back(i, std::min(i + PARTITION - 1, n - 1));

    std::vector<ProblemPart> result;
    for(int i = 0; i < ranges.size(); ++i)
        for(int j = i+1; j < ranges.size(); ++j)
            result.emplace_back(ranges[i].first, ranges[i].second, ranges[j].first, ranges[j].second);

    return result;
}


long long solve_regular(const int from, const int to)
{
    pk::binary_indexed_tree<int, MAX_ELEMENT> bit;
    long long result = 0LL;

    for(int i = from; i <= to; ++i)
    {
        const int v = GetElement(i);

        result += bit.count_in_range(v+1, MAX_ELEMENT-1);
        bit.increase(v, 1);
    }

    return result;
}


long long solve_part(const int from1, const int to1, const int from2, const int to2)
{
    long long t[MAX_ELEMENT];
    std::fill(t, t + MAX_ELEMENT, 0LL);

    for(int i = from1; i <= to1; ++i)
        ++t[GetElement(i)];

    for(int i = MAX_ELEMENT-2; i >= 0; i--)
        t[i] += t[i+1];

    long long result = 0LL;
    for(int i = from2; i <= to2; ++i)
    {
        const int v = GetElement(i);
        result += t[v+1];
    }

    return result;
}


class TeaSolutionMaster
{
public:
    TeaSolutionMaster(const int n_) : n(n_), result(0LL) {}

    void solve()
    {
        solve_regular_range();
        solve_problem_parts();

        printf("%lld\n", result);
    }

private:
    void solve_regular_range()
    {
        const int my_node_id = MyNodeId();
        const int number_of_nodes = NumberOfNodes();

        const auto ranges = get_ranges_for_regular_check(my_node_id);

        for(int i = 0; i < ranges.size(); ++i)
        {
            const int node_id = i % number_of_nodes;

            if(node_id == my_node_id)
            {
                result += solve_regular(ranges[i].first, ranges[i].second);
            }
            else
            {
                Receive(node_id);
                result += GetLL(node_id);
            }
        }
    }

    void solve_problem_parts()
    {
        const int my_node_id = MyNodeId();
        const int number_of_nodes = NumberOfNodes();

        const auto problem_parts = get_problem_parts();

        for(int i = 0; i < problem_parts.size(); ++i)
        {
            const int node_id = i % number_of_nodes;

            if(node_id == my_node_id)
            {
                result += solve_part(
                        problem_parts[i].from1,
                        problem_parts[i].to1,
                        problem_parts[i].from2,
                        problem_parts[i].to2);
            }
            else
            {
                Receive(node_id);
                result += GetLL(node_id);
            }
        }
    }

    const int n;
    long long result;
};


class TeaSolutionSlave
{
public:
    TeaSolutionSlave(const int n_) : n(n_) {}

    void solve()
    {
        solve_regular_range();
        solve_problem_parts();
    }

private:
    void solve_regular_range()
    {
        const int my_node_id = MyNodeId();
        const int number_of_nodes = NumberOfNodes();

        const auto ranges = get_ranges_for_regular_check(my_node_id);

        for(int i = my_node_id; i < ranges.size(); i += number_of_nodes)
        {
            const long long result = solve_regular(ranges[i].first, ranges[i].second);

            PutLL(0, result);
            Send(0);
        }
    }

    void solve_problem_parts()
    {
        const int my_node_id = MyNodeId();
        const int number_of_nodes = NumberOfNodes();

        const auto problem_parts = get_problem_parts();

        for(int i = my_node_id; i < problem_parts.size(); i += number_of_nodes)
        {
            const long long result = solve_part(
                    problem_parts[i].from1,
                    problem_parts[i].to1,
                    problem_parts[i].from2,
                    problem_parts[i].to2);

            PutLL(0, result);
            Send(0);
        }
    }

    const int n;
};


int main()
{
    const int n = GetN();
    if(n <= 1000000)
    {
        if(MyNodeId() == 0)
        {
            TeaSolutionSingle solution(n);
            solution.solve();
        }
    }
    else
    {
        if(MyNodeId() == 0)
        {
            TeaSolutionMaster solution(n);
            solution.solve();
        }
        else
        {
            TeaSolutionSlave solution(n);
            solution.solve();
        }
    }
}