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
#include<stdio.h>
#include "message.h"
#include "kollib.h"
#include <set>
#include <unordered_map>
using namespace std;


set<int> P;
set<int> R;
unordered_map<int, int> M;
int Q[300][2];

int main() {
    int n = NumberOfStudents();
    int mm = NumberOfQueries();

    if(MyNodeId() > 0) return 0;

    for(int i=0;i<mm;i++) {
        Q[i][0] = QueryFrom(i+1);
        Q[i][1] = QueryTo(i+1);
        P.insert(Q[i][0]);
        P.insert(Q[i][1]);
    }

    int id = n/7 + 1;
    int id2 = -1;
    int lastid = -1;
    int k = 0;

    while(R.size() < P.size()) {
        if(P.find(id) != P.end()) {
            R.insert(id);
            M[id] = k;
        }
        k++;
        id2 = FirstNeighbor(id);
        if(id2 == lastid)
            id2 = SecondNeighbor(id);
        lastid = id;
        id = id2;
    }

    for(int i=0;i<mm;i++) {
        int x = ((M[Q[i][0]] - M[Q[i][1]]) + n) % n;
        printf("%d\n", min(x, n-x));
    }
    
    return 0;
}