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
// Mateusz Kurek, PA 2014
// 4B KOL

#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<string>
#include<list>
#include<deque>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<utility>
#include<sstream>
#include<cstring>
#include <string>
#include <cstdarg>

//#include <iostream>

// #include <unordered_map>
#include "message.h"
#include "kollib.h"

using namespace std;

#define REP(i,v)for(int i=0;i<(v);++i)
#define FOR(i,x,v)for(int i=x;i<=(v);++i)
#define FORD(i,x,v)for(int i=x;i>=(v);--i)
#define VAR(v,n) __typeof(n) v = (n)
#define FOREACH(i,c) for(VAR(i,(c).begin()); i != (c).end(); ++i)
#define ALL(c) (c).begin(), c.end()
#define PB push_back
#define SZ size
#define MP make_pair
#define FI first
#define SE second
#define CL clear()
#define RS resize
#define INFTY 1000000001
#define EPS 10e-9
#define SIZE(x) ((int)(x).size())

bool debug = true;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<bool> VB;
typedef pair<int,int> PII;
typedef long long LL;
typedef vector<string> VS;

void show(PII p)
{printf("(%d %d)\n",p.FI,p.SE);}

void show(VI e)
{REP(i,SIZE(e)) printf("%d ",e[i]); printf("\n");}

void show(vector<PII> e)
{REP(i,SIZE(e)) printf("(%d %d) ",e[i].FI,e[i].SE); printf("\n");}

void show(VS e)
{REP(i,SIZE(e)) printf("%s\n",e[i].c_str());}

void show(VVI e)
{REP(i,SIZE(e)) show(e[i]);}

#define MAX_STUDENTS 10000001
#define MAX_TESTS 201

map<int, int> students;
map<int, int>::iterator it;

int log(const char *fmt, ...)
{
    if(debug){
        va_list args;
        va_start(args, fmt);
        return printf(fmt, args);
    }
    return 0;
}

int get_dir(int id, int nodes){
    return (id + 1) > nodes / 2 ? -1 : 1;
}

void read_students(int id2, int count, int nodes, int start, int n){
    int fn, sn, tmp, dir = get_dir(id2, nodes), end = start + count -1, curr, prev;
    fn = FirstNeighbor(1);
    sn = SecondNeighbor(1);
    int from, to;
    // read from end
    if(dir == -1)
    {
        from = n;
        to = start;
        curr = sn;
        prev = 1;
    }
    else{
        curr = 1;
        prev = sn;
        from = 1;
        to = end;
    }
    // printf("Instance %d: reading from %d to %d (getting from %d to %d) with dir %d\n", id2, from, to, start, end, dir);
    if(dir == 1){
        for(int i=from;i<=to;i+=dir){
            fn = FirstNeighbor(curr);
            sn = SecondNeighbor(curr);
            if(i >= min(start, end) && i <= max(start, end)){
                students[curr] = i;
            }
            tmp = curr;
            if(fn == prev){
                curr = sn;
            }
            else{
                curr = fn;
            }
            prev = tmp;
            // pos_start += dir;
        }
    }
    else{
        for(int i=from;i>=to;i+=dir){

            fn = FirstNeighbor(curr);
            sn = SecondNeighbor(curr);
            if(i >= min(start, end) && i <= max(start, end)){
                students[curr] = i;
            }
            tmp = curr;
            if(fn == prev){
                curr = sn;
            }
            else{
                curr = fn;
            }
            prev = tmp;
            // pos_start += dir;
        }
    }
    // printf("Instance %d: end of reading\n", id2);
    // return PII(curr, prev);
}



void answer(int i){
    int query_from, query_to, tmp;
    query_from = QueryFrom(i);
    query_to = QueryTo(i);
    it = students.find(query_from);
    // printf("Instance %d, query %d, looking for %d and %d\n", MyNodeId(), i, query_from, query_to);
    // Receive(0);
    // tmp = GetInt(0);
    // log("Instance %d - turn received\n", id);
    if(it != students.end()){
        PutInt(0, i);
        PutInt(0, it->second);
        Send(0);
    }

    it = students.find(query_to);
    if(it != students.end()){
        PutInt(0, i);
        PutInt(0, it->second);
        Send(0);
    }
}

bool first_received[MAX_TESTS];
int ans[MAX_TESTS];


int main()
{
    // printf("Instance %d: Calling read\n", MyNodeId());
    int nodes = NumberOfNodes(), id=MyNodeId(), n, my_start_with, my_dir, curr, prev, fn, sn, my_students_count, query_from, query_to, query, answ, source, tmp;
    PII read_end;

    n = NumberOfStudents();
    my_students_count = n / nodes;
    my_start_with = id * my_students_count;
    if(n % nodes >= (id + 1)){
        my_students_count += 1;

    }
    my_start_with += min(n % nodes, id) + 1; // +- 1
    my_dir = get_dir(id, nodes);
    int receive_from = (id - get_dir(id, nodes) + nodes) % nodes, send_to = (id + get_dir(id, nodes) + nodes) % nodes;

    read_students(id, my_students_count, nodes, my_start_with, n);

    // if(id == 0){
    //     log("Students: %d, nodes: %d, nodes/2: %d\n", n, nodes, nodes/2);
    // }
    // log("Instance %d, my_start_with: %d, my_students: %d, my_dir: %d, receiving from %d, sending to %d\n",
    //     id, my_start_with, my_students_count, my_dir, receive_from, send_to);

    // start if id=0 - read first student

    // if(id == 0){
    //     fn = FirstNeighbor(1);
    //     if(nodes > 1){
    //         PutInt(nodes-1, fn); // his current
    //         PutInt(nodes-1, 1); // my last
    //         Send(nodes-1);
    //     }
    //     read_end = read_students(my_students_count, 1, fn, 1, 1); // pos_start = 0?
    // }
    // else{
    //     Receive(receive_from);
    //     curr = GetInt(receive_from);
    //     prev = GetInt(receive_from);

    //     read_end = read_students(my_students_count, curr, prev, my_start_with, my_dir);
    // }

    // if((my_dir == 1 && (id + 1) < nodes/2) || (my_dir == -1 && (id + 1) > (nodes/2) + 1)){
    //     PutInt(send_to, read_end.first);
    //     PutInt(send_to, read_end.second);
    //     Send(send_to);
    //     // log("Instance %d: Sending to: %d\n", id, send_to);
    // }

    // printf("Instance %d: my students: ", id);
    // for (auto& x: students) {
    //     printf(" %d -> %d ", x.second, x.first);
    // }
    // printf("\n");
    // // queries

    FOR(i, 1, NumberOfQueries()){
        answer(i);
    }

    if(id == 0){


        FOR(j, 1, NumberOfQueries()*2){
            source = Receive(-1);
            query = GetInt(source);
            answ = GetInt(source);
            if(first_received[query]){
                tmp = abs(ans[query] - answ);
                ans[query] = min(tmp, n - tmp);
            }
            else{
                first_received[query] = true;
                ans[query] = answ;
            }
        }

        FOR(j, 1, NumberOfQueries()){
            printf("%d\n", ans[j]);
        }

    }

    return 0;
}