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

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;

#define FOR(i, b, e) for(int i=b; i<=e; i++)
#define FORD(i, b, e) for(int i=b; i>=e; i--)
#define SIZE(x) ((int)(x).size())
#define pb push_back
#define st first
#define nd second
#define sp ' '
#define ent '\n'
#define WATCH(x) cout<<(#x)<<" is "<<(x)<<ent

//END OF TEMPLATE

const int N=1e6;

int zlicz[N+5];
int node, n, a, b;
ll odp;
vector<pair<ii, ii> > pod;

// int GetN() { return int(1e8); }
// int GetElement(int i) { return (i * 1ll * i) % int(1e6) + 1; }

// Copied from Wikipedia - https://en.wikipedia.org/wiki/Fenwick_tree
#define LSB(i) ((i) & -(i)) // zeroes all the bits except the least significant one

int A[N+5];

int sum(int i) // Returns the sum from index 1 to i
{
    int sum = 0;
    while (i > 0)
        sum += A[i], i -= LSB(i);
    return sum;
}

void add(int i, int k){ // Adds k to element with index i
    while (i < N+2)
        A[i] += k, i += LSB(i);
}

void liczsam(int p, int k){
    if(p>k || p>n || k>n){
        return;
    }
	int el;
	FOR(i, p, k){
		el=GetElement(i-1);
		odp+=sum(N+1)-sum(el);
		add(el, 1);
	}
}

void licz(int p1, int k1, int p2, int k2){
    if(p2<=k1 || p1>k1 || p2>k2 || p2>n || k2>n || p1>n || k1>n){
        return;
    }
	FOR(i, p1, k1){
		zlicz[GetElement(i-1)]++;
	}
	FORD(i, N, 1){
		zlicz[i]+=zlicz[i+1];
	}
	FOR(i, p2, k2){
		odp+=zlicz[GetElement(i-1)+1];
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int p1, k1, p2, k2, ile;
	node=MyNodeId();
	n=GetN();
    // cin>>n;
    ile=n/13;
	if(node==0){
        p1=1;
        p2=1;
        FOR(i, 0, 12){
            FOR(j, i, 12){
                if(i==12 && j==12){
                    pod.pb({{p1, n}, {p2, n}});
                }
                else if(i==12){
                    pod.pb({{p1, n}, {p2, p2+ile}});
                    p2+=ile+1;
                }
                else if(j==12){
                    pod.pb({{p1, p1+ile}, {p2, n}});
                    p1+=ile+1;
                    p2=p1;
                }
                else{
                    pod.pb({{p1, p1+ile}, {p2, p2+ile}});
                    p2+=ile+1;
                }
            }
        }
        FOR(i, 91, 99){
            pod.pb({{n+1, n+1}, {n+1, n+1}});
        }
        // cout<<SIZE(pod)<<ent;
        // for(auto x: pod){
        //     cout<<x.st.st<<sp<<x.st.nd<<sp<<x.nd.st<<sp<<x.nd.nd<<ent;
        // }
        FOR(i, 1, 99){
            PutInt(i, pod[i].st.st);
            PutInt(i, pod[i].st.nd);
            PutInt(i, pod[i].nd.st);
            PutInt(i, pod[i].nd.nd);
            Send(i);
        }
        liczsam(pod[0].st.st, pod[0].st.nd);
		FOR(i, 1, 99){
			Receive(i);
			odp+=GetLL(i);
		}
		cout<<odp<<ent;
	}
	else{
		Receive(0);
        p1=GetInt(0);
        k1=GetInt(0);
        p2=GetInt(0);
        k2=GetInt(0);
        if(p1==p2){
            liczsam(p1, k1);
        }
        else if(k1<p2){
            licz(p1, k1, p2, k2);
        }
		PutLL(0, odp);
		Send(0);
	}
	return 0;
}