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
/*{{{*/
#pragma GCC optimize("O3")
#include "teatr.h"
#include "message.h"
#include <bits/stdc++.h>
#ifdef LOCAL
int c_;
#define cout (c_?cerr:cout)
#define debug(...) {cerr<<(c_?"":"\033[96;1m");\
++c_;__VA_ARGS__;--c_;cerr<<(c_?"":"\033[0m");}
#else
#define debug(...) {}
#endif
#define st first
#define nd second
#define all(...) begin(__VA_ARGS__), end(__VA_ARGS__)
#define dump(...) debug(print(#__VA_ARGS__" =", __VA_ARGS__))
#define dump_range(...) debug(cerr<<#__VA_ARGS__": ";print_range(__VA_ARGS__);)
using namespace std;
template< typename t >
using V = vector< t >;
using ll = long long;
using ld = long double;
void print(){cout<<'\n';}
template< typename t, typename... v >
void print(const t& a, const v&... b)
{cout<<a<<(sizeof...(b)?" ":"");print(b...);}
template< typename t >
void print_range(t a, const t& b)
{while (a!=b) cout<<(*a++)<<' ';cout<<'\n';}
/*}}}*/

constexpr int maxk = 1e6, logg = 20, rozm = (1 << logg);

inline ll solve(int lo1, int hi1, int lo2, int hi2)
{
    ll res = 0;
    int zl[maxk + 2];
    fill(zl, zl + maxk + 2, 0);

    for (int i = lo1; i <= hi1; ++i)
        ++zl[GetElement(i)];
    
    for (int i = maxk; i >= 1; --i)
        zl[i] += zl[i + 1];

    for (int i = lo2; i <= hi2; ++i)
        res += zl[GetElement(i) + 1];

    return res;
}


inline ll solve(int lo, int hi)
{
    ll res = 0;
    int tree[rozm << 1];
    fill(tree, tree + (rozm << 1), 0);

    for (int i = lo; i <= hi; ++i)
    {
        int v = GetElement(i);
    
        for (int r = v + rozm; r >= 1; r >>= 1)
            res += tree[r];

        int l = rozm;
        int r = v + rozm - 1;

        if (l > r) continue;

        ++tree[l];
        tree[r] += (l != r);
        
        for (; (l >> 1) != (r >> 1); l >>= 1, r >>= 1)
            tree[l + 1] += ((l & 1) == 0), tree[r - 1] += ((r & 1) == 1);
    }

    return res;
}


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n = GetN();
    int h = NumberOfNodes();
    int d = MyNodeId();
    int sh = 1;

    while (sh * (sh - 1) / 2 + sh <= h)
        ++sh;
    --sh;

    int len = ((n + sh - 1) / sh);

    int p = 0, q = 0;

    for (int i = 0; i < d; ++i)
    {
        ++q; 
        if (q == sh)
            ++p, q = p;
    }

    int lo1 = p * len;
    int hi1 = p * len + (len - 1);

    int lo2 = q * len;
    int hi2 = q * len + (len - 1);

    hi2 = min(hi2, n - 1);
    hi1 = min(hi1, n - 1);

    ll res = 0;
    if (p == q) res = solve(lo1, hi1);
    else res = solve(lo1, hi1, lo2, hi2);

    if (d + 1 < h)
    {
        Receive(d + 1);
        res += GetLL(d + 1);
    }

    dump(d, p, q, lo1, hi1, lo2, hi2, res);

    if (d - 1 >= 0)
    {
        PutLL(d - 1, res);
        Send(d - 1);
    }

    if (d == 0)
        print(res);
}