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
#include <bits/stdc++.h>
#include "message.h"
#include "teatr.h"

using namespace std;

const int MAXEL = 1000010;
int tree[MAXEL];
long long int sum[6], other[6];
long long int wyn = 0;

int check_if_under_5(int _begin, int _end)
{
    for (int i = _begin; i < _end; i++)
    {
        int x = GetElement(i);
        if (x > 5)
            return 0;
    }

    return 1;
}

long long int highsum(int x)
{
    long long int out = 0;
    for (int i = 5; i > x; i--)
        out += sum[i];
    return out;
}

long long int lowsum(int x)
{
    long long int out = 0;
    for (int i = 1; i < x; i++)
        out += other[i];
    return out;
}

void calculate5(int _begin, int _end)
{
    for (int i = _begin; i < _end; i++)
    {
        int x = GetElement(i);
        wyn += highsum(x);
        sum[x]++;
    }
}

void calculateothers(int _begin,int _end)
{
    for (int i = 0; i <= 5; i++)
        sum[i] += other[i];

    for (int i = _begin; i < _end; i++)
    {
        int x = GetElement(i);
        wyn += lowsum(x);
    }
}

int t_pref(int w)
{
    int out = 0;
    while (w > 0)
    {
        out += tree[w];
        w -= (w & (-w));
    }

    return out;
}

void t_add(int w)
{
    while (w < MAXEL)
    {
        tree[w]++;
        w += (w & (-w));
    }
}

void calculate(int _begin, int _end)
{
    for (int i = _end - 1; i >= _begin; i--)
    {
        int x = GetElement(i);
        wyn += (long long int) t_pref(x - 1);
        t_add(x);
    }
}

void ssend(int cel)
{
    PutLL(cel, wyn);
    for (int i = 1; i <= 5; i++)
        PutInt(cel, sum[i]);
    Send(cel);
}

void rreceive (int cel)
{
    Receive(cel);
    wyn += GetLL(cel);
    for (int i = 1; i <= 5; i++)
        other[i] = GetInt(cel);
}

int main(){

    int id = MyNodeId();
    int n = GetN();
    int interval = ceil(n/100);

    if (id == 0)
    {
        if (n >= 100)
        {
            int under_5 = check_if_under_5(0, interval);
            int iss = under_5;
            for (int i = 1; i < 100; i++)
            {
                Receive(i);
                iss = min(iss, GetInt(i));
            }


            if (iss == 1)
            {
                for (int i = 1; i < 100; i++)
                {
                    PutInt(i, 1);
                    Send(i);
                }

                calculate5(0, interval);
                rreceive(id + 1);
                calculateothers(0, interval);

                printf("%lld", wyn);
            }

            else
            {
                for (int i = 1; i < 100; i++)
                {
                    PutInt(i, 0);
                    Send(i);
                }

                calculate(0, n);
                printf("%lld", wyn);
            }

            return 0;
        }

        else

        {
            calculate(0, n);
            printf("%lld", wyn);
            return 0;
        }
    }

    else
    {
            if (n >= 100)
            {
                int under_5 = check_if_under_5(id * interval, min ((id + 1) * interval, n));
                PutInt(0, under_5);
                Send(0);
                Receive(0);
                int need = GetInt(0);

                if(need == 0)
                    return 0;

                else
                {
                    calculate5(id * interval, min ((id + 1) * interval, n));

                    if (id == 99)
                    {
                        ssend(id - 1);
                    }

                    if (id != 99)
                    {
                        rreceive(id + 1);
                        calculateothers(id * interval, min ((id + 1) * interval, n));
                        ssend(id - 1);
                    }

                    return 0;
                }

                return 0;
            }

            else
                return 0;
    }
}