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
#include <stdio.h>
#include <algorithm>
#include <map>
#include "kollib.h"
#include "message.h"

using namespace std;

const int ALL_DONE = 1;
const int QUERY_LOOKUP = 2;
const int COLLECT_RESULT = 3;

struct query_lookup_t {
	int query, value, pos;
};

struct collect_result_t {
	int query, result;
};

void broadcastAllDone(int myId, int nodes) {
	for(int i = 0; i < nodes; ++i) {
		if(i != myId) {
			PutInt(i, ALL_DONE);
			Send(i);
		}
	}
}

void broadcastLookups(int myId, int nodes, vector<query_lookup_t> &queries) {
	for(int i = 0; i < nodes; ++i) {
		if(i != myId) {
			PutInt(i, QUERY_LOOKUP);
			PutInt(i, queries.size());
			for(vector<query_lookup_t>::iterator it = queries.begin(); it != queries.end(); ++it) {
				PutInt(i, it->query);
				PutInt(i, it->value);
				PutInt(i, it->pos);
			}
			Send(i);
		}
	}
}

vector<query_lookup_t> receiveLookups(int from) {
	vector<query_lookup_t> result;
	int count = GetInt(from);
	for(int i = 0; i < count; ++i) {
		query_lookup_t query;
		query.query = GetInt(from);
		query.value = GetInt(from);
		query.pos = GetInt(from);
		result.push_back(query);
	}
	return result;
}

void sendCollectResult(int to, collect_result_t result) {
	PutInt(to, COLLECT_RESULT);
	PutInt(to, result.query);
	PutInt(to, result.result);
	Send(to);
}

collect_result_t receiveCollectResult(int from) {
	collect_result_t result;
	result.query = GetInt(from);
	result.result = GetInt(from);
	return result;
}

int main() {
	int n = NumberOfStudents();
	int nodes = NumberOfNodes();
	int queries = NumberOfQueries();
	int groupSize = max(100, 1 + (n - 1) / nodes);
	int groupsCount = 1 + (n - 1) / groupSize;
	int lastGroupSize = n % groupSize;
	if(lastGroupSize == 0)
		lastGroupSize = groupSize;
	int myId = MyNodeId();
	if(myId >= groupsCount)
		return 0;
	int myGroupSize = myId == groupsCount - 1? lastGroupSize : groupSize;
	int myStart = myId * groupSize;
	int students[myGroupSize];
	bool reverse;
	int before;

	if(myId == 0) {
		students[0] = 1;
		before = SecondNeighbor(1);
		reverse = false;
		if(groupsCount > 1) {
			PutInt(groupsCount - 1, 1);
			PutInt(groupsCount - 1, FirstNeighbor(1));
			Send(groupsCount - 1);
		}
	} else if(myId == groupsCount - 1) {
		Receive(0);
		before = GetInt(0);
		students[myGroupSize - 1] = GetInt(0);
		reverse = true;
	} else if(myId >= (groupsCount + 1) / 2) {
		Receive(myId + 1);
		before = GetInt(myId + 1);
		students[myGroupSize - 1] = GetInt(myId + 1);
		reverse = true;
	} else { // myId < (groupsCount + 1) / 2
		Receive(myId - 1);
		before = GetInt(myId - 1);
		students[0] = GetInt(myId - 1);
		reverse = false;
	}

	for(int i = reverse? myGroupSize - 2 : 1; reverse? i >= 0 : i < myGroupSize; i = reverse? i - 1 : i + 1) {
		int previous = reverse? i + 1 : i - 1;
		int neighbor = FirstNeighbor(students[previous]);
		if(neighbor == before)
			neighbor = SecondNeighbor(students[previous]);
		students[i] = neighbor;
		before = students[previous];
	}
	map<int, int> studentsReverse;
	for(int i = 0; i < myGroupSize; ++i)
		studentsReverse[students[i]] = i;
	if(myId == (groupsCount - 1) / 2 || myId == 1 + (groupsCount - 1) / 2) {}
	else if(myId < (groupsCount - 1) / 2) {
		PutInt(myId + 1, students[myGroupSize - 2]);
		PutInt(myId + 1, students[myGroupSize - 1]);
		Send(myId + 1);
	} else {
		PutInt(myId - 1, students[1]);
		PutInt(myId - 1, students[0]);
		Send(myId - 1);
	}

	int queriesDone = 0;
	map<int, int> results;
	vector<query_lookup_t> lookups;
	for(int i = 1; i <= queries; ++i) {
		if(studentsReverse.find(QueryFrom(i)) != studentsReverse.end()) {
			int pos = studentsReverse[QueryFrom(i)];
			if(studentsReverse.find(QueryTo(i)) != studentsReverse.end()) {
				++queriesDone;
				int result = abs(studentsReverse[QueryTo(i)] - pos);
				if(groupsCount == 1)
					result = min(result, n - result);
				if(myId == 0)
					results[i] = result;
				else {
					collect_result_t collect = {
						query: i,
						result: result,
					};
					sendCollectResult(0, collect);
				}
			} else {
				query_lookup_t lookup = {
					query: i,
					value: QueryTo(i),
					pos: myStart + i,
				};
				lookups.push_back(lookup);
			}
		}
	}
	broadcastLookups(myId, groupsCount, lookups);

	while(queriesDone < queries) {
		int from = Receive(-1);
		int type = GetInt(from);
		vector<query_lookup_t> lookups2;
		collect_result_t collect;
		switch(type) {
			case QUERY_LOOKUP:
				lookups2 = receiveLookups(from);
				for(vector<query_lookup_t>::iterator it = lookups2.begin(); it != lookups2.end(); ++it) {
					if(studentsReverse.find(it->value) != studentsReverse.end()) {
						int ourPos = studentsReverse[it->value];
						int result = min(abs(ourPos - it->pos), min(it->pos + 1 + n - ourPos, ourPos + 1 + n - it->pos));
						if(myId == 0)
							results[it->query] = result;
						else {
							collect = {
								query: it->query,
								result: result,
							};
							sendCollectResult(0, collect);
						}
						++queriesDone;
					}
				}
				break;
			case COLLECT_RESULT:
				collect = receiveCollectResult(from);
				results[collect.query] = collect.result;
				++queriesDone;
				break;
			case ALL_DONE:
				queriesDone = queries;
				break;
		}
	}

	if(myId == 0) {
		broadcastAllDone(0, nodes);
		for(int i = 1; i <= queries; ++i)
			printf("%d\n", results[i]);
	}

	return 0;
}