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
#include <cstdio>
#include <message.h>
#include "kollib.h"
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;

#define st first
#define nd second
#define make(a,b) make_pair(a,b)

vector < pair<int,int> > jakie;
int x;

void szukaj( int a, int pre)
{
//	printf("szukaj %d %d\n",a,pre);
	for (int i=1;i<=x;i++)
	{
//		printf("Wrzuc %d %d\n",MyNodeId(), a);
		jakie.push_back( make(a,i) );
		if ( SecondNeighbor(a) == pre )
		{
			pre = a;
			a = FirstNeighbor(a);
		}
		else
		{
			pre = a;
			a = SecondNeighbor(a);
		}
	}
	jakie.push_back( make( NumberOfStudents()+5, 0) );
/*	if ( MyNodeId() + 2 < NumberOfNodes() )
	{
		PutInt( MyNodeId() + 2, a );
		PutInt( MyNodeId() + 2, pre );
		Send( MyNodeId() + 2 );
	}
*/	sort( jakie.begin(), jakie.end() );
}


int main()
{
	int n = NumberOfStudents();
	int k = NumberOfNodes();
	pair<int,int> z;
	pair<int,int> From;
	pair<int,int> To;
	int a,b;
	x = n;
//	if ( MyNodeId() == n-1 ) x = n - (n/k)*(k-1);
//	if ( MyNodeId() / 2 == 0 )
//	{
		if ( MyNodeId() == 0 ) szukaj(1, 0);
//		else szukaj( FirstNeighbor(1), 1 );
//	}
/*/	else
	{
		Receive( MyNodeId() - 2 );
		a = GetInt( MyNodeId() - 2 );
		b = GetInt( MyNodeId() - 2 );
		szukaj( a,b );
	}
*/	if ( MyNodeId() == 0 )
	{
//		for (int i=1;i<k;i++) Receive(i);
		for (int i=1;i<=NumberOfQueries();i++)
		{
			z = *lower_bound( jakie.begin(), jakie.end(), make( QueryFrom(i), 0 ) );
			if (z.st == QueryFrom(i) ) From = make( 0, z.nd );
			z = *lower_bound( jakie.begin(), jakie.end(), make( QueryTo(i), 0 ) );
			if (z.st == QueryTo(i) ) To = make( 0, z.nd );
/*			for (int t = 1; t < k ; t++ )
			{
				
				a = GetInt(t);
				b = GetInt(t);
				if ( a ) From = make( t, a );
				if ( b ) To = make( t, b );
			}
//			printf("From %d %d To %d %d\n", From.st, From.nd, To.st, To.nd );
*/			if ( To.st%2 == From.st%2 )
			{
				a = abs( To.nd - From.nd + ( To.st/2 - From.st/2)*x ) ;
				printf("%d\n", min(a, n-a) );
			}
			else
			{
				a = To.nd + From.nd + (To.st/2 + From.st/2)*x -1;
				printf("%d\n", min(a , n-a) );
			}
		}
	}
/*	else
	{
		for (int i=1;i<=NumberOfQueries();i++)
		{
			z = *lower_bound( jakie.begin(), jakie.end(), make( QueryFrom(i), 0 ) );
//			printf("Wyslij %d %d ", MyNodeId(), i);
			if ( z.st != QueryFrom(i) )
			{
				PutInt(0,0);
//				printf("0 ");
			}
			else
			{
				PutInt(0, z.nd);
//				printf("%d ",z.nd);
			}
			z = *lower_bound( jakie.begin(), jakie.end(), make( QueryTo(i), 0 ) );
			if ( z.st != QueryTo(i) )
			{
				PutInt(0,0);
//				printf("0 ");
			}
			else
			{
				PutInt(0, z.nd);
//				printf("%d ",z.nd);
			}
//			printf("\n");
		}
		Send( 0 );
	}*/
}