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
#include "kollib.h"
#include "message.h"
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

vector<int> students;
int positions[200][2][100];

int studentFind(int s) {
    for (int i = 1; i <= students.size(); ++i) {
        if (students[i] == s) return i;
    }
    return -students.size();
}

int main() {
    int myNodeId = MyNodeId();
    int numberOfNodes = NumberOfNodes();
    int numberOfStudents = NumberOfStudents();
    int numberOfQueries = NumberOfQueries();
    int studentsPerNode = numberOfStudents / numberOfNodes + 1;

    if (MyNodeId() == 0) {
        //printf("[0] numberOfNodes: %d\n", numberOfNodes);
        //printf("[0] numberOfStudents: %d\n", numberOfStudents);
        //printf("[0] numberOfQueries: %d\n", numberOfQueries);
        //printf("[0] studentsPerNode: %d\n", studentsPerNode);

        int limitL = numberOfStudents / 2;
        int limitR = numberOfStudents - limitL;
        //printf("[0] limitL: %d\n", limitL);
        //printf("[0] limitR: %d\n", limitR);

        int startL = FirstNeighbor(1);
        int startR = SecondNeighbor(1);

        PutChar(numberOfNodes - 1, 'R'); // dir
        PutInt(numberOfNodes - 1, 1);  // prev
        PutInt(numberOfNodes - 1, startR);  // next
        PutInt(numberOfNodes - 1, 0);  // read
        PutInt(numberOfNodes - 1, limitR); // limit
        Send(numberOfNodes - 1);

        //printf("[0] Reading students\n");
        students.push_back(1);
        char dir = 'L';
        int prev = 1;
        int next = startL;
        int read = 1;
        int limit = limitL;
        int nodeStudents = 1;
        int nodeId;
        while ((nodeStudents < studentsPerNode) && (read < limit)) {
            //printf("[%d] %c %d\n", myNodeId, dir, next);
            students.push_back(next);
            nodeStudents++;
            read++;
            int newNext = FirstNeighbor(next);
            if (newNext == prev) newNext = SecondNeighbor(next);
            prev = next;
            next = newNext;
        }
        if (read < limit) {
            if (dir == 'L') nodeId = myNodeId + 1;
            else nodeId = myNodeId - 1;
            PutChar(nodeId, dir);
            PutInt(nodeId, prev);
            PutInt(nodeId, next);
            PutInt(nodeId, read);
            PutInt(nodeId, limit);
            Send(nodeId);
        }

        int queryFrom, queryTo, position;
        for (int i = 1; i <= numberOfQueries; ++i) {
            queryFrom = QueryFrom(i);
            queryTo = QueryTo(i);
            position = studentFind(queryFrom);
            positions[i-1][0][0] = position;
            position = studentFind(queryTo);
            positions[i-1][1][0] = position;
        }

        for (int j = 1; j < numberOfNodes; j++) {
            nodeId = Receive(-1);
            for (int i = 1; i <= numberOfQueries; ++i) {
                position = GetInt(nodeId);
                positions[i-1][0][nodeId] = position;
                position = GetInt(nodeId);
                positions[i-1][1][nodeId] = position;
            }
        }

        //printf("----------\n");
        int posFrom, posTo;
        for (int i = 1; i <= numberOfQueries; ++i) {
            posFrom = 0;
            for (int j = 0; j < numberOfNodes; j++) {
                //printf("%d ", positions[i-1][0][j]);
                if (positions[i-1][0][j] > 0) {
                    posFrom += positions[i-1][0][j];
                    break;
                }
                else posFrom += -positions[i-1][0][j];
            }
            //printf("\n");
            posTo = 0;
            for (int j = 0; j < numberOfNodes; j++) {
                //printf("%d ", positions[i-1][1][j]);
                if (positions[i-1][1][j] > 0) {
                    posTo += positions[i-1][1][j];
                    break;
                }
                else posTo += -positions[i-1][1][j];
            }
            //printf("\n");
            //printf("%d %d\n", posFrom, posTo);
            printf("%d\n", min(abs(posFrom-posTo), numberOfStudents-abs(posFrom-posTo)));
            //printf("\n\n");
        }
    }
    else {
        int nodeId = Receive(-1);
        int dir = GetChar(nodeId);
        int prev = GetInt(nodeId);
        int next = GetInt(nodeId);
        int read = GetInt(nodeId);
        int limit = GetInt(nodeId);
        //printf("%d -> [%d] Reading students (dir: %c, prev: %d, next: %d, read: %d, limit: %d)\n", nodeId, myNodeId, dir, prev, next, read, limit);
        int nodeStudents = 0;
        while ((nodeStudents < studentsPerNode) && (read < limit)) {
            //printf("[%d] %c %d\n", myNodeId, dir, next);
            students.push_back(next);
            nodeStudents++;
            read++;
            int newNext = FirstNeighbor(next);
            if (newNext == prev) newNext = SecondNeighbor(next);
            prev = next;
            next = newNext;
        }
        if (dir == 'R') reverse(students.begin(), students.end());
        if (dir == 'L') nodeId = myNodeId + 1;
        else nodeId = myNodeId - 1;
        if ((nodeId > 0) && (nodeId < numberOfNodes - 1)) {
            PutChar(nodeId, dir);
            PutInt(nodeId, prev);
            PutInt(nodeId, next);
            PutInt(nodeId, read);
            PutInt(nodeId, limit);
            Send(nodeId);
        }

        int queryFrom, queryTo, position;
        for (int i = 1; i <= numberOfQueries; ++i) {
            queryFrom = QueryFrom(i);
            queryTo = QueryTo(i);
            position = studentFind(queryFrom);
            PutInt(0, position);
            position = studentFind(queryTo);
            PutInt(0, position);
        }
        Send(0);
    }

    return 0;
}