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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "kollib.h"
#include "message.h"

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>


std::vector<int> split;
std::vector<int> students;

struct Part
{
    Part(int _from=0, int _to=0, int _count=0)
        : from(_from), to(_to), count(_count) { }

    friend std::ostream& operator<< (std::ostream& os, const Part& part)
    {
        return os << "Part " << part.from << " " << part.to << ", len=" << part.count;
    }

    bool operator< (const Part& rhs) const
    {
        return (from!=rhs.from) ? (from<rhs.from) : (to<rhs.to);
    }

    int from, to;
    int count;
};

struct Student
{
    Student(int _id=0, int _position=0)
        : id(_id), position(_position) { }

    friend std::ostream& operator<< (std::ostream& os, const Student& student)
    {
        return os << student.id << " student as " << student.position << "th";
    }

    int id;
    int position;
};


struct Proto
{
    typedef enum {END=0, PART=1, STUDENT=2} ID;
    enum {MASTER=0};

    static ID GetID(int source)
    {
        return static_cast<ID>(GetChar(source));
    }

    static void PutPart(const Part& part)
    {
        PutChar(MASTER, PART);
        PutInt(MASTER, part.from);
        PutInt(MASTER, part.to);
        PutInt(MASTER, part.count);
    }
    static Part GetPart(int source)
    {
        int from = GetInt(source);
        int to = GetInt(source);
        int count = GetInt(source);
        return Part(from, to, count);
    }

    static void PutStudent(const Student& student)
    {
        PutChar(MASTER, STUDENT);
        PutInt(MASTER, student.id);
        PutInt(MASTER, student.position);
    }
    static Student GetStudent(int source)
    {
        int id = GetInt(source);
        int position = GetInt(source);
        return Student(id, position);
    }

    static void Send()
    {
        PutChar(MASTER, END);
        ::Send(MASTER);
    }
};


int go(int from, int prev)
{
    std::vector<Student> found;

    int count = 0;
    int to = from;
    while (from==to || !std::binary_search(split.begin(), split.end(), to))
    {
        int next = FirstNeighbor(to);
        if (next==prev)
            next = SecondNeighbor(to);
        if (std::binary_search(students.begin(), students.end(), to))
        {
            //std::cout << "\t\tstudent: " << to << " as " << count << "th from " << from << std::endl;
            found.push_back(Student(to, count));
        }
        prev = to;
        to = next;
        ++count;
    }
    //std::cout << "\tpath: " << from << " " << to << ", len=" << count << std::endl;
    //send
    Proto::PutPart(Part(from, to, count));
    for (int i=0; i<found.size(); ++i)
        Proto::PutStudent(found[i]);
}

void solve()
{
    typedef std::pair<Part, int> StudentPosition;
    typedef std::map<int, StudentPosition> StudentsMap;

    std::vector<Part> parts;
    StudentsMap students;

    for (int i=1; i<NumberOfNodes(); ++i)
    {
        Receive(i);
        for (bool end = false; !end; )
        {
            switch (Proto::GetID(i))
            {
                case Proto::PART: {
                        Part part = Proto::GetPart(i);
                        parts.push_back(part);
                        //std::cout << part << std::endl;
                        break;
                    }
                case Proto::STUDENT: {
                        Student student = Proto::GetStudent(i);
                        // combine student with parts.back()
                        students[student.id] = StudentPosition(parts.back(), student.position);
                        //std::cout << '\t' << student << std::endl;
                        break;
                    }
                default: // Proto::END:
                    end = true;
                    break;
            }
        }
    }

    std::sort(parts.begin(), parts.end());

    std::vector<Part> path;
    path.push_back(parts.front());
    for (int i=1; i<split.size(); ++i)
    {
        std::vector<Part>::iterator it =
            std::lower_bound(parts.begin(), parts.end(), Part(path.back().to, 0));
        int prev = path.back().from;
        if (it->to == prev)
            ++it;
        path.push_back(*it);
    }
    //std::cout << "PATH " << std::endl;
    //for (int i=0; i<path.size(); ++i)
    //    std::cout << '\t' << path[i] << std::endl;
    //std::cout << "STUDENTS " << std::endl;
    //for (StudentsMap::const_iterator it=students.begin(); it!=students.end(); ++it)
    //    std::cout << "student " << it->first << " as " << it->second.second
    //        << " in " << it->second.first << std::endl;

    for (int m=1; m<=NumberOfQueries(); ++m)
    {
        // znajdz studenta
        int ret = 0;
        int i = 0;
        {
            int from = QueryFrom(m);
            const StudentPosition& pos = students[from];
            const Part& part = pos.first;
            for (;;)
            {
                if (path[i].from==part.from && path[i].to==part.to) {
                    ret = part.count - pos.second;
                    //std::cout << part.count - pos.second << " on " << path[i] << std::endl;
                    break;
                }
                else if (path[i].from==part.to && path[i].to==part.from) {
                    ret = pos.second;
                    //std::cout << pos.second << " on " << path[i] << std::endl;
                    break;
                }
                ++i;
            }
        }

        {
            int to = QueryTo(m);
            const StudentPosition& pos = students[to];
            const Part& part = pos.first;
            for (;;)
            {
                if (++i==path.size())
                    i = 0;
                if (path[i].from==part.from && path[i].to==part.to) {
                    ret += pos.second;
                    //std::cout << pos.second << " on " << path[i] << std::endl;
                    break;
                }
                else if (path[i].from==part.to && path[i].to==part.from) {
                    ret += part.count - pos.second;
                    //std::cout << part.count - pos.second << " on " << path[i] << std::endl;
                    break;
                }
                else {
                    ret += path[i].count;
                    //std::cout << path[i].count << " on " << path[i] << std::endl;
                }
            }
        }
        ret %= NumberOfStudents();
        //std::cout << "QUERY("  << m << ")=";
        std::cout << std::min(ret, NumberOfStudents() - ret) << std::endl;
    }
}

int main()
{
    // how data is splitted
    srand(42);
    int parts = NumberOfNodes() * 20;
    for (int i=0; i<parts; ++i)
        split.push_back((rand() % NumberOfStudents()) + 1);
    std::sort(split.begin(), split.end());
    split.erase(std::unique(split.begin(), split.end()), split.end());
    if (split.size() < 3)
        split = {1, 2, 3};

    // prepare queries
    for (int i=1; i<=NumberOfQueries(); ++i) {
        students.push_back(QueryFrom(i));
        students.push_back(QueryTo(i));
    }
    std::sort(students.begin(), students.end());
    students.erase(std::unique(students.begin(), students.end()), students.end());

    if (MyNodeId() == 0)
    {
        solve();
    }
    else
    {
        for (int part=MyNodeId()-1; part<split.size(); part+=NumberOfNodes()-1)
        {
            int from = split[part];
            go(from, FirstNeighbor(from));
            go(from, SecondNeighbor(from));
        }
        Proto::Send();
    }

    return 0;
}