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

#define PB push_back
#define MP make_pair
#define st first
#define nd second
#define umap unordered_map
#define uset unordered_set
#define watch(x) cerr << (#x) << " is " << (x) << endl
#define all(a) begin(a),end(a)

using namespace std;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const int inf=1000000;
const long long llinf=0x3f3f3f3f3f3f3f3f;
const double oo=1e15;
const double eps=1e-8;
const double pi=acos(-1.0);

template<typename T> T sqr(T a_) { return a_*a_; }
template<typename T> T cub(T a_) { return a_*a_*a_; }

// inline void readI(int*n){register char c=0,sign=1;while(c<33)c=getchar_unlocked();if(c=='-'){sign=-1;c=getchar_unlocked();}*n=0;while(c>33){*n*=10;*n+=c-'0';c=getchar_unlocked();}*n*=sign;}
// inline void printI(int n){if(!n){putchar_unlocked('0');return;}if(n<0){putchar_unlocked('-');n*=-1;}char digits[12];int i=0;while(n){digits[i++]=(n%10)+'0';n/=10;}while(i--)putchar_unlocked(digits[i]);}
// inline void readLL(long long*n){register char c=0,sign=1;while(c<33)c=getchar_unlocked();if(c=='-'){sign=-1;c=getchar_unlocked();}*n=0;while(c>33){*n*=10;*n+=c-'0';c=getchar_unlocked();}*n*=sign;}
// inline void printLL(long long n){if(!n){putchar_unlocked('0');return;}if(n<0){putchar_unlocked('-');n*=-1;}char digits[24];int i=0;while(n){digits[i++]=(n%10)+'0';n/=10;}while(i--)putchar_unlocked(digits[i]);}
// inline void readS(string*s){register char c=0;while(c<33)c=getchar_unlocked();while(c>=33){*s+=c;c=getchar_unlocked();}}
// inline void printS(string s){for(auto c:s)putchar_unlocked(c);}

typedef long double ld;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

//-----------------------------------------------//

const int N=1e6+10;
const int M=1e6+1;
const int mod=1000005997;

// int GetN() { return 100 * 1000 * 1000; }
// int GetElement(int i) {
// if (i < 100 * 1000 * 1000 - 3) return 1 + i / 100;
// else return 100 * 1000 * 1000 - i;
// }

ll n,res;
int cnt,a,b,lb,le,rb,re,x,k;
int t[N];

int tree[N];

///////////////////
//https://www.topcoder.com/community/competitive-programming/tutorials/binary-indexed-trees/
int query(int x){
    int sum = 0;
    while (x > 0){
        sum += tree[x];
        x -= (x & -x);
    }
    return sum;
}
void insert(int x){
    while (x <= M){
        tree[x] += 1;
        x += (x & -x);
    }
}
///////////////////


int GetElem(int k){
    return (k>n ? inf : GetElement(k-1));
}

void brute(int n){
    ll res=0;
    for(int i=0; i<n; i++){
        t[i]=GetElement(i);
    }
    for(int i=1; i<n; i++){
        for(int j=0; j<i; j++){
            if(t[j]>t[i]) res++;
        }
    }
    cout<<res<<'\n';
}

void same(){
    lb=a*k+1;
    le=(a+1)*k;

    for(int i=le; i>=lb; i--){
        x=GetElem(i);
        res+=query(x-1);
        insert(x);
    }
}

void diff(){
    lb=a*k+1;
    le=(a+1)*k;
    rb=b*k+1;
    re=(b+1)*k;

    for(int i=rb; i<=re; i++) t[GetElem(i)]++;
    for(int i=1; i<=1000000; i++) t[i]+=t[i-1];
    for(int i=lb; i<=le; i++) res+=t[GetElem(i)-1];
}

bool ok=1;

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    n=GetN();
    if(MyNodeId()==0){

        if(n<13){
            brute(n);
            ok=0;
        }

        k=n/13;
        if(n%13) k++;

        for(int i=0; i<13; i++){
            for(int j=i; j<13; j++){
                PutInt(++cnt, i);
                PutInt(cnt, j);
                PutInt(cnt, (ok? k : -1));
                Send(cnt);
            }
        }

        for(int ins = 1; ins <= 91; ++ins){
            Receive(ins);
            res += GetLL(ins);
        }
        if(ok)
        cout<<res<<'\n';

        return 0;
    }
    else if(MyNodeId()<=91){
        Receive(0);
        a=GetInt(0);
        b=GetInt(0);
        k=GetInt(0);

        if(k!=-1) {
            a==b ? same() : diff();
        }

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

    return 0;
}

//-----------------------------------------------//