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
#include "message.h"
#include "kollib.h"
#include <cstdio>
#include <algorithm>
//#include <ctime>
#include <cstdlib>
#include <ext/hash_map>
using namespace std;
using namespace __gnu_cxx;

#define MAX_POINTS 300000

struct CP{
	int nextDist, prevDist, next, prev, sum;
	
	CP() : sum(-1) {}
};

vector<int> points;
hash_map<int, CP> hmap;

int main(){
	int id = MyNodeId();
	int nodes = NumberOfNodes();
	int n = NumberOfStudents();
	int q = NumberOfQueries();
	int nPoints = max(min(MAX_POINTS, n / 3), 2);
	
	hmap.resize(nPoints + 1000);
	points.reserve(nPoints);
	
	long long x = 3571;
	long long plus = 40591;
	
	while((int)points.size() < nPoints){
		//int r = (rand() * RAND_MAX + rand()) % n + 1;
		int r = x % n + 1;
		x += plus;
		
		if(hmap.find(r) == hmap.end()){
			points.push_back(r);
			hmap[r] = CP();
		}
	}
	
	for(int i=1; i<=q; i++){
		int from = QueryFrom(i);
		int to = QueryTo(i);
		
		if(hmap.find(from) == hmap.end()){
			points.push_back(from);
			hmap[from] = CP();
		}
		
		if(hmap.find(to) == hmap.end()){
			points.push_back(to);
			hmap[to] = CP();
		}
	}
	
	for(int i=id; i<(int)points.size(); i+=nodes){
		int start = points[i];
		int prev = start;
		int cur = SecondNeighbor(start);
		int dist = 1;
	
		while(hmap.find(cur) == hmap.end()){
			int next = SecondNeighbor(cur);
			
			if(next == prev)
				next = FirstNeighbor(cur);
				
			++dist;
			prev = cur;
			cur = next;
		}
		
		CP &cp = hmap[start];
		cp.nextDist = dist;
		cp.next = cur;
		
		// ---- second direction
		
		start = points[i];
		prev = start;
		cur = FirstNeighbor(start);
		dist = 1;
	
		while(hmap.find(cur) == hmap.end()){
			int next = FirstNeighbor(cur);
			
			if(next == prev)
				next = SecondNeighbor(cur);
				
			++dist;
			prev = cur;
			cur = next;
		}
		
		cp.prevDist = dist;
		cp.prev = cur;
	}
	
	if(id == 0){
		for(int node = 1; node < nodes; node++){
			if(node < (int)points.size())
				Receive(node);
			
			for(int i=node; i<(int)points.size(); i+=nodes){
				CP cp;
				cp.next = GetInt(node);
				cp.nextDist = GetInt(node);
				cp.prev = GetInt(node);
				cp.prevDist = GetInt(node);
				hmap[points[i]] = cp;
			}	
		}
	}else{
		bool ok = false;
	
		for(int i=id; i<(int)points.size(); i+=nodes){
			CP &cp = hmap[points[i]];
			PutInt(0, cp.next);
			PutInt(0, cp.nextDist);
			PutInt(0, cp.prev);
			PutInt(0, cp.prevDist);
			ok = true;
		}
		
		if(ok) Send(0);
	}
	/*
	if(id == 0)
	for(int i=0; i<(int)points.size(); i++){
		printf("%d:  next=%d dist=%d  prev=%d dist=%d    sum=%d\n", 
		points[i], hmap[points[i]].next, hmap[points[i]].nextDist, hmap[points[i]].prev, hmap[points[i]].prevDist, hmap[points[i]].sum);
	}
	*/
	if(id == 0){
		hash_map<int, CP>::iterator it = hmap.begin();
		it->second.sum = 0;
		int sum = it->second.nextDist;
		int prev = it->first;
		it = hmap.find(it->second.next);
	
		while(it->second.sum < 0){
			it->second.sum = sum;
		
			if(it->second.next != prev){
				sum += it->second.nextDist;
				prev = it->first;
				it = hmap.find(it->second.next);
			}else{
				sum += it->second.prevDist;
				prev = it->first;
				it = hmap.find(it->second.prev);
			}
		}
	}
	
	if(id == 0){
		for(int i=1; i<=q; i++){
			int from = QueryFrom(i);
			int to = QueryTo(i);
			int dist = abs(hmap[from].sum - hmap[to].sum);
			int minDist = min(dist, n - dist);
			
			printf("%d\n", minDist);
		}
	}
	
	return 0;
}