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 <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <algorithm>


#include "message.h"
#include "kollib.h"


using namespace std;

void recPut(int target, int x) {
	PutInt(target, x);
}

template<class T, class U>
void recPut(int target, const pair<T, U>& x) {
	recPut(target, x.first);
	recPut(target, x.second);
}

template<class T>
void recPut(int target, const vector<T>& V) {
	PutInt(target, V.size());
	for (auto& x:V) recPut(target, x);
}

void recGet(int target, int& x) {
	x=GetInt(target);
}

template<class T, class U>
void recGet(int target, pair<T, U>& x) {
	recGet(target, x.first);
	recGet(target, x.second);
}

template<class T>
void recGet(int target, vector<T>& V) {
	V.resize(GetInt(target));
	for (auto& x:V) recGet(target, x);
}


vector<pair<pair<int, int>, pair<int, int> > > GO(const vector<int> &PV, const vector<int> &task) {
	vector<pair<pair<int, int>, pair<int, int> > > taskRes;
	for (auto t:task) {
		int d1=1;
		int d2=1;
		int act1=FirstNeighbor(t);
		int act2=SecondNeighbor(t);
		
		for (int prev=t; !binary_search(PV.begin(), PV.end(), act1); d1++) {
			int next=FirstNeighbor(act1);
			if (next==prev) next=SecondNeighbor(act1);
			prev=act1;
			act1=next;
		}
		for (int prev=t; !binary_search(PV.begin(), PV.end(), act2); d2++) {
			int next=SecondNeighbor(act2);
			if (next==prev) next=FirstNeighbor(act2);
			prev=act2;
			act2=next;
		}
		taskRes.push_back(make_pair(make_pair(act1, d1),make_pair(act2, d2)));
	}
	return taskRes;
}


void goCykl(map<int, pair<int, pair<pair<int, int>, pair<int, int> > > > &P) {
	int act=P.begin()->first;
	int last=act;
	int prev=P.begin()->second.second.first.first;
	int d=0;
	do {
		P[act].first=d;
		auto p=P[act];
		int next=p.second.second.first;
		int nextD=d+p.second.second.second;
		if (next==prev) {
			next=P[act].second.first.first;
			nextD=d+p.second.first.second;
		}
		d=nextD;
		prev=act;
		act=next;
	} while (act!=last);
}



void wypisz(map<int, pair<int, pair<pair<int, int>, pair<int, int> > > > &P) {
	int N=NumberOfStudents();
	int Q=NumberOfQueries();
	for (int q=1; q<=Q; q++) {
		int a=QueryFrom(q);
		int b=QueryTo(q);
		printf("%d\n", min((P[a].first-P[b].first+N)%N, (P[b].first-P[a].first+N)%N));
	}
}


int main() {
	int N=NumberOfStudents();
	int M=NumberOfQueries();
	int nodeCnt=NumberOfNodes();
	int nodeId=MyNodeId();
	

	
	if(nodeId==0) {
		map<int, pair<int, pair<pair<int, int>, pair<int, int> > > > P;
		vector<vector<int> > tasks(nodeCnt);
		vector<int> PV;
		for (int i=1; i<=M; i++) {
			P[QueryFrom(i)]=make_pair(-1, make_pair(make_pair(0, 0), make_pair(0, 0) ) );
			P[QueryTo(i)]=make_pair(-1, make_pair(make_pair(0, 0), make_pair(0, 0) ) );
		}
		int SPN=5*max(nodeCnt, int(P.size()));
		
		
		if (N<10000) {
			for (auto p:P) PV.push_back(p.first);
			tasks[0]=PV;
		}
		else {
			while (int(P.size())<SPN && int(P.size())%nodeCnt)
				P[1+rand()%N]=make_pair(-1, make_pair(make_pair(0, 0), make_pair(0, 0) ) );
		
			for (auto p:P) PV.push_back(p.first);
			random_shuffle(PV.begin(), PV.end());
			int taskSize=PV.size()/nodeCnt;
			for (int node=0; node<nodeCnt; node++)
				for (int i=node*taskSize; i<(node+1)*taskSize; i++)
					tasks[node].push_back(PV[i]);
			sort(PV.begin(), PV.end());
		}
		
		
		for (int node=1; node<nodeCnt; node++) {
			recPut(node, PV);
			recPut(node, tasks[node]);
			Send(node);
		}
		
		{
			auto taskRes=GO(PV, tasks[0]);
			for (int i=0; i<taskRes.size(); i++) {
				P[tasks[0][i]]=make_pair(-1, taskRes[i]);
			}
		}
		
		
//		for (int node=1; node<nodeCnt; node++) {
//			Receive(node);
		for (int n=1; n<nodeCnt; n++) {
			int node=Receive(-1);
			vector<pair<pair<int, int>, pair<int, int> > > taskRes;
			recGet(node, taskRes);
			
			for (int i=0; i<taskRes.size(); i++) {
				P[tasks[node][i]]=make_pair(-1, taskRes[i]);
			}
		}
		
//		printf("tutaj\n");
		
		goCykl(P);
		
		wypisz(P);
		
	} else {
		vector<int> PV;
		vector<int> task;
		Receive(0);
		recGet(0, PV);
		recGet(0, task);
		recPut(0, GO(PV, task));
		Send(0);
	}
	return 0;
}