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
#include <iostream>
#include <set>
#include "message.h"
#include "kollib.h"

using namespace std;

int qp[400];

struct db { int x, p; };

bool operator< (db a, db b) {
	return a.x < b.x;
}

set<db> s;

int nextStudent(int x, int pr) {
	int y = FirstNeighbor(x);
	if (pr == y) {
		return SecondNeighbor(x);
	} else {
		return y;
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	int id    = MyNodeId();
	int n     = NumberOfStudents();
	int m     = NumberOfQueries();

	if (id == 0) {
		db tmp;
		pair<set<db>::iterator, bool> insr;
		for (int i = 0; i < m; i++) {
			tmp.x = QueryFrom(i+1);
			tmp.p = i*2;
			insr = s.insert(tmp);
			if (!insr.second) {
				qp[i*2] = -(insr.first->p) - 1;
			}
			tmp.x = QueryTo(i+1);
			tmp.p = i*2+1;
			insr = s.insert(tmp);
			if (!insr.second) {
				qp[i*2+1] = -(insr.first->p) - 1;
			}
		}

		int cs = 1, ps = SecondNeighbor(1);
		set<db>::iterator it;
		for (int i = 0; i < n; i++) {
			tmp.x = nextStudent(cs, ps);
			ps = cs;
			cs = tmp.x;
			it = s.find(tmp);
			if (it != s.end()) {
				qp[it->p] = i;
			}
		}

		int diff, cqa, cqb;
		for (int i = 0; i < m; i++) {
			cqa = qp[i*2];
			if (cqa < 0) { cqa = qp[-cqa - 1]; }

			cqb = qp[i*2+1];
			if (cqb < 0) { cqb = qp[-cqb - 1]; }

			diff = cqa - cqb;
			if (diff < 0)
				diff = -diff;
			cout << min(diff, n - diff) << endl;
		}
	}

	return 0;
}