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

#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define st first
#define nd second
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

//					time	msgc	msgs
//faza losowania:	m		0		0
//faza wyliczania	(n/us)	0		0
//faza przesylu		m		us		m*8
//jedno zapytanie	(n/m)	1		8
//wszystkie			q*n/m/us

struct Hasher{
	inline unsigned operator()(const PII &a) const {
		return a.st ^ ((a.nd << 16) | (a.nd >> 16));
	}	
};

int n, u, us, q, m, solved;
vector<PII> chs;
vector<int> dist;
vector<int> repr;
unordered_map<PII, int, Hasher> id;
vector<int> qs;
int fa, fb;
int res[210];

void choose(){
	mt19937_64 e(n);
	uniform_int_distribution<int> uni(1, n);
	int fx = 1;
	int fy = FirstNeighbor(fx);
	id[PII(fx,fy)] = 0;
	id[PII(fy,fx)] = 0;
	while(id.size() < m){
		int x = uni(e);
		int y = FirstNeighbor(x);
		id[PII(x,y)] = 0;
		id[PII(y,x)] = 0;
	}
	for(pair<const PII, int> &kv : id) chs.push_back(kv.st);
	FWD(i,1,m) swap(chs[i], chs[uniform_int_distribution<int>(0,i)(e)]);
	FWD(i,0,m) id[chs[i]] = i;
	fa = id[PII(fx,fy)];
	fb = id[PII(fy,fx)];
	FWD(i,1,q+1) qs.push_back(i);
	FWD(i,1,q) swap(qs[i], qs[uniform_int_distribution<int>(0,i)(e)]);
}

PII nxt(PII r){
	int f = FirstNeighbor(r.nd);
	return PII(r.nd, f == r.st ? SecondNeighbor(r.nd) : f);
}

void locate(int x, int y, int &xr, int &xd){
	PII xc = PII(x, y);
	xd = 0;
	while(id.find(xc) == id.end()){
		xc = nxt(xc);
		++xd;
	}
	xd += dist[id[xc]];
	xr = repr[id[xc]];
}

int main(){
	u = MyNodeId();
	us = NumberOfNodes();
	n = NumberOfStudents();
	q = NumberOfQueries();
	m = sqrt((LL)q*n/us/2);
	m += (m&1);
	m = max(m, 2);
	m = min(m, (n/2)+((n/2)&1));
	choose();
	int p = m * u / us;
	int k = m * (u + 1) / us;
	dist.resize(m);
	repr.resize(m);
	FWD(i,p,k){
		PII c = chs[i];
		PII d = c;
		dist[i] = 0;
		if(i == fa || i == fb){
			repr[i] = i;
		}else{
			do{
				d = nxt(d);
				++dist[i];
			}while(id.find(d) == id.end());
			repr[i] = id[d];
		}
	}
	FWD(v,0,us)
		if(u != v){
			PutInt(v, p);
			PutInt(v, k);
			FWD(i,p,k){
				PutInt(v, dist[i]);
				PutInt(v, repr[i]);
			}
			Send(v);
		}
	solved = 0;
	FWD(i,0,us-1){
		int src = Receive(-1);
		int a = GetInt(src);
		if(u == 0 && a < 0){
			++solved;
			res[-a-1] = GetInt(src);
			--i;
			continue;
		}
		int b = GetInt(src);
		FWD(j,a,b){
			dist[j] = GetInt(src);
			repr[j] = GetInt(src);
		}
	}
	for(int z = 1; z < m + 3; z *= 2){
		FWD(i,0,m)
			if(repr[i] != fa && repr[i] != fb){
				dist[i] = dist[i] + dist[repr[i]];
				repr[i] = repr[repr[i]];
			}
	}
	p = q * u / us;
	k = q * (u + 1) / us;
	FWD(_i,p,k){
		int i = qs[_i];
		int x = QueryFrom(i), xr, xd;
		locate(x, FirstNeighbor(x), xr, xd);
		int y = QueryTo(i), yr, yd;
		locate(y, FirstNeighbor(y), yr, yd);
		if(xr != yr){
			xr = yr;
			xd = n - 1 - xd;
		}
		int d = abs(xd-yd);
		d = min(d, n-d);
		PutInt(0, -i);
		PutInt(0, d);
		Send(0);
	}
	if(u == 0){
		while(solved < q){
			int src = Receive(-1);
			++solved;
			int i = -GetInt(src)-1;
			int r = GetInt(src);
			res[i] = r;
		}
		FWD(i,0,q)
			printf("%d\n", res[i]);
	}
	return 0;
}