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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include<algorithm>
#include<iostream>
#include<list>
#include<set>
#include<map>
#include<queue>
#define FOR(i,n) for(int i = 0;i<n;++i)
#define FORI(i,b,n) for(int i = b;i<n;++i)
#define FORD(i,n) for(int i = n;i>=0;--i)
#define ZERO 0.000001
#define MAX ((1<<31)-1)
#define qprintf debug && printf
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define ull long long int
#include "kollib.h"
#include "message.h"
#include <iostream>
using namespace std;
//int debug=1;
//#include <unistd.h>
//#define dcout usleep(myId * 1000);debug && cerr<<"["<<myId<<"] "
class N
{
	public:
		int first;
		int toFirst;
		int second;
		int toSecond;
		int me;
};
class Path
{
	public:
	int distance;
	int finish;
};
Path go(int currentPoint, int previousPoint, set<int> &searchFor){
	int distance=0;
	do{
		//cout<<"current = "<<currentPoint;
		++distance;
		int nextPoint = FirstNeighbor(currentPoint);
		if(nextPoint==previousPoint)
			nextPoint = SecondNeighbor(currentPoint);
		previousPoint=currentPoint;
		currentPoint=nextPoint;
		//cout<<" -> next = "<<currentPoint<<endl;
	}while(!searchFor.count(currentPoint));
	//cout<<"I got to "<<currentPoint<<endl;

	Path path;
	path.distance=distance;
	path.finish = currentPoint;
	return path;
}
Path go(int currentPoi, int previousPoi, map<int,N> &listNodes, set<int> &poi){
	int distance=0;
	do{
		//cout<<"current = "<<currentPoi;
		int nextPoi = listNodes[currentPoi].first;
		int distDiff=listNodes[currentPoi].toFirst;
		if(nextPoi==previousPoi){
			nextPoi = listNodes[currentPoi].second;
			distDiff=listNodes[currentPoi].toSecond;
		}
		previousPoi=currentPoi;
		currentPoi=nextPoi;
		distance+=distDiff;
		//cout<<" -> next = "<<currentPoi<<endl;
	}while(!poi.count(currentPoi));
	//cout<<"I got to "<<currentPoi<<endl;

	Path path;
	path.distance=distance;
	path.finish = currentPoi;
	return path;
}
Path go(int from, int to, int previous, map<int,N> &listNodes){
	int distance=0;
	Path zero;
	zero.distance=0;
	zero.finish=from;
	if(from==to)return zero;
	do{
		int next = listNodes[from].first;
		int distDiff=listNodes[from].toFirst;
		if(next==previous){
			next = listNodes[from].second;
			distDiff=listNodes[from].toSecond;
		}
		distance+=distDiff;
		previous=from;
		from=next;
		//cout<<" -> next = "<<currentPoi<<endl;
	}while(from!=to);
	//cout<<"I got to "<<currentPoi<<endl;

	Path path;
	path.distance=distance;
	path.finish = from;
	return path;
}

int main() {
	int nodes=NumberOfNodes();
	int myId = MyNodeId();

	int queries = NumberOfQueries();
	int students = NumberOfStudents();
	if(queries==0)return 0;

	set<int> poi;
	FOR(i,queries){
		poi.insert(QueryFrom(i+1));
		poi.insert(QueryTo(i+1));
	}
	int queriedPoints = poi.size();
	set<int> m;
	{
		int i=0;
		set<int>::iterator it = poi.begin();
		set<int>::iterator ite = poi.end();
		for(;it!=ite;++it){
			m.insert(*it);
			++i;
		}
	}
	//queriedPoints=400;students=200000000;nodes=20;
	//dcout<<"Queried points="<<queriedPoints<<"="<<m.size()<<endl;return 0;

	int workingNodes = nodes-1;

	int maxTargetPoints = 1000000;//nie generujemy więcej niż milion
	maxTargetPoints = min(students/10, maxTargetPoints);//nie generujemy więcej niż jedna dziesiąta
	//if(maxTargetPoints)
	int maxPerNode = maxTargetPoints/workingNodes;//czyli per node
	int perNode = min(20000, maxPerNode);


	int targetPoints = perNode * workingNodes;
	int toGenerate = targetPoints - queriedPoints;

	if(perNode<10 || toGenerate<0){
		//fuck it, i'll do it on my own!
		perNode = queriedPoints;
		targetPoints = queriedPoints;
		workingNodes=1;
		if(myId!=0 && myId!=1){
			//actually with node no 1
			return 0;
		}
	}

	//if(myId==0)cout<<"perNode="<<perNode<<", targetPoints="<<targetPoints<<", workingNodes="<<workingNodes<<", toGenerate="<<toGenerate<<", students="<<students<<", queries="<<queries<<", queriedPoints="<<queriedPoints<<endl;return 0;

	int*generatedPoints=new int[targetPoints+1000];
	{
		int i=0;
		set<int>::iterator it = m.begin();
		set<int>::iterator ite = m.end();
		for(;it!=ite;++it){
			generatedPoints[i]=*it;
			//if(*it<1 || *it>students){cout<<"FFF"<<endl;return 0;}
			++i;
		}
	}
	//ull ccc=0;
	FORI(i,queriedPoints,targetPoints){
		int next=7;
		do{
			next=((((ull)i)*104729+next)%students+students)%students+1;//++ccc;
			//cout<<m.size()<<" ";
		}while(m.count(next));
		//if(next<1 || next>students){cout<<"FFF"<<endl;return 0;}
		m.insert(next);
		generatedPoints[i]=next;
	}//cout<<ccc<<endl;return 0;
	if(myId){//working node
		//FOR(i,targetPoints)//cout<<generatedPoints[i]<<" ";
		//cout<<endl;

		N*mine=new N[perNode];

		int myStart = myId-1;
		FOR(i,perNode){
			int myNthPoint = i*workingNodes+myStart;
			int currentPoint=generatedPoints[myNthPoint];
			//dcout<<"Now point i="<<i<<", so generated_i="<<myNthPoint<<", so I start from "<<currentPoint<<endl;
			int previousPoint = FirstNeighbor(currentPoint);
			Path towardsSecond = go(currentPoint, previousPoint, m);


			previousPoint = SecondNeighbor(currentPoint);
			Path towardsFirst = go(currentPoint, previousPoint, m);

			mine[i].me=currentPoint;
			mine[i].first=towardsSecond.finish;
			mine[i].toFirst=min(towardsSecond.distance,students-towardsSecond.distance);
			mine[i].second=towardsFirst.finish;
			mine[i].toSecond=min(towardsFirst.distance,students-towardsFirst.distance);
		}
		FOR(i,perNode){
			PutInt(0,mine[i].me);
			PutInt(0,mine[i].first);
			PutInt(0,mine[i].toFirst);
			PutInt(0,mine[i].second);
			PutInt(0,mine[i].toSecond);
		}
		Send(0);
		return 0;
	}

	// myId==0 -> centerNode
	map<int,N> listNodes;
	FOR(i,workingNodes){
		int receiveFrom = i+1;
		Receive(receiveFrom);
		FOR(j, perNode){
			N listNode;
			listNode.me = GetInt(receiveFrom);
			listNode.first = GetInt(receiveFrom);
			listNode.toFirst = GetInt(receiveFrom);
			listNode.second = GetInt(receiveFrom);
			listNode.toSecond = GetInt(receiveFrom);
			listNodes[listNode.me]=listNode;
		}
	}
	map<int,N> finalQueryPoints;
	int currentPoi = *(poi.begin());
	{
		int i=0;
		set<int>::iterator it = poi.begin();
		set<int>::iterator ite = poi.end();
		for(;it!=ite;++it){
			int currentPoi = *it;
			int previousPoint = listNodes[currentPoi].first;
			Path towardsSecond = go(currentPoi, previousPoint, listNodes, poi);

			previousPoint = listNodes[currentPoi].second;
			Path towardsFirst = go(currentPoi, previousPoint, listNodes, poi);

			finalQueryPoints[currentPoi].me=currentPoi;
			finalQueryPoints[currentPoi].first=towardsSecond.finish;
			finalQueryPoints[currentPoi].toFirst=min(towardsSecond.distance,students-towardsSecond.distance);
			finalQueryPoints[currentPoi].second=towardsFirst.finish;
			finalQueryPoints[currentPoi].toSecond=min(towardsFirst.distance,students-towardsFirst.distance);
		}
	}
	//cout<<finalQueryPoints.size()<<endl;
	FOR(i,queries){
		int from = QueryFrom(i+1);
		int to = QueryTo(i+1);
		Path left = go(from, to, finalQueryPoints[from].first, finalQueryPoints);
		Path right = go(from, to, finalQueryPoints[from].second, finalQueryPoints);
		int dist = min(left.distance,right.distance);
		cout<<dist<<endl;
	}
	return 0;
}