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
#include <cstdio>
#include "teatr.h"
#include "message.h"
#include <algorithm>

int count[1000005];
int wval[1000005];
int intervals[105];
int inval;
int n, k;
int me;

constexpr const int mheight = 1000000;

int tree[2500005];
void add(int pos, int s, int cs, int ce){
    if ((s == cs) && (s == ce)){
        tree[pos]++;
        return;
    }
    else if (s <= (cs + ce)/2){
        add(pos * 2, s, cs, (cs + ce)/2);
    }
    else {
        add(pos * 2 + 1, s, (cs + ce)/2 + 1, ce);
    }
    tree[pos] = tree[pos * 2] + tree[pos * 2 + 1];
}

int get(int pos, int s, int e, int cs, int ce){
    //printf("%d %d %d %d %d\n", pos, s, e, cs, ce);
    if ((s == cs) && (e == ce)){
        return tree[pos];
    }
    else if ((s <= (cs + ce)/2) && (e <= (cs + ce)/2)){
        return get(pos * 2, s, e, cs, (cs + ce)/2);
    }
    else if ((s > (cs + ce)/2) && (e > (cs + ce)/2)){
        return get(pos * 2 + 1, s, e, (cs + ce)/2 + 1, ce);
    }
    else {
        return get(pos * 2, s, (cs + ce)/2, cs, (cs + ce)/2) + get(pos * 2 + 1, (cs + ce)/2 + 1, e, (cs + ce)/2 + 1, ce);
    }
}

int main(){
    n = GetN();
    k = NumberOfNodes();
    me = MyNodeId();
    if (me == 0){
        //printf("a\n");
        for (int i = 0; i < n; i++){
            count[GetElement(i)]++;
        }
        int sum = 0;
        for (int i = 1; i <= mheight; i++){
            sum += count[i];
            if (sum > 2 * n / k){
                intervals[inval] = i;
                inval++;
                sum = 0;
            }
        }
        if (intervals[inval - 1] != mheight){
            intervals[inval] = mheight;
            inval++;
        }
        for (int i = 1; i < k; i++){
            PutInt(i, inval);
            for (int j = 0; j < inval; j++){
                PutInt(i, intervals[j]);
            }
            Send(i);
        }
        //printf("b %d\n", inval);
        /*for (int i = 0; i < inval; i++){
            printf("bb %d\n", intervals[i]);
        }*/
    }
    else {
        //printf("c\n");
        Receive(0);
        inval = GetInt(0);
        for (int i = 0; i < inval; i++){
            intervals[i] = GetInt(0);
        }
        //printf("d\n");
    }
    k--;
    if (me == 0){
        long long sum = 0;
        long long inwer[105];
        long long temp[105];
        for (int i = 0; i <= inval; i++){
            inwer[i] = 0;
        }
        //printf("h1 %d %d\n", inval, intervals[0]);
        for (int i = 0; i < k / 2; i++){
            long long _sum = 0;
            Receive(i + 1);
            sum += GetLL(i + 1);
            //printf("received %d %lld\n", i + 1, sum);
            for (int j = 0; j < inval; j++){
                int v = GetInt(i + 1);
                _sum += v;
                temp[j] = _sum;
                sum += inwer[j + 1] * v;
            //    printf("adding %lld %d\n", inwer[j + 1], v);
            }
            long long _sum2 = 0;
            for (int j = 0; j < inval; j++){
                inwer[j + 1] += _sum - temp[j];
            }
        }
        //printf("h2 %lld\n", sum);
        for (int i = k / 2; i < k; i++){
            Receive(i + 1);
            sum += GetLL(i + 1);
        }
        printf("%lld\n", sum);
    }
    else {
        //printf("entered %d\n", me);
        me--;
        int w1 = 0;
        for (int i = 1; i <= mheight; i++){
            wval[i] = w1;
            if (intervals[w1] == i){
                w1++;
            }
        }
        if (me < k / 2){
            int len = n / (k / 2) + 1;
            int e = len * me;
            int f = std::min(n, len * (me + 1));
            //printf("ent1 %d %d %d %d\n", me + 1, inval, e, f);
            long long sum = 0;
            for (int i = e; i < f; i++){
                int val = GetElement(i);
                sum += get(1, val + 1, mheight + 1, 1, mheight + 1);
                add(1, val, 1, mheight + 1);
                count[wval[val]]++;
            }
            //printf("end1 %d %lld\n", me + 1, sum);
            PutLL(0, sum);
            for (int i = 0; i < inval; i++){
                //printf("homhom %d\n", count[i]);
                PutInt(0, count[i]);
            }
            Send(0);
        }
        else {
            me -= k / 2;
            long long sum = 0;
            int len = n / (k / 2) + 1;
            //printf("ent2 %d %d %d\n", me + 1, inval, len);
            for (int i = 0; i < n; i += len){
                for (int j = 0; j < len; j++){
                    if (i + j >= n)
                        break;
                    //printf("z %d %d\n", i, j);
                    int val = GetElement(i + j);
                    if (wval[val] == me){
                    //    printf("w1\n");
                        sum += get(1, val + 1, mheight + 1, 1, mheight + 1);
                    //    printf("w2\n");
                    }
                }
                for (int j = 0; j < len; j++){
                    if (i + j >= n){
                        break;
                    }
                    int val = GetElement(i + j);
                    if (wval[val] == me){
                        //printf("w3 %d\n", val);
                        add(1, val, 1, mheight + 1);
                        //printf("w4\n");
                    }
                }
            }
            //printf("end2 %d\n", me + 1);
            PutLL(0, sum);
            Send(0);
        }
    }
}