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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

#define MAX_NON 100
#define SLEEP_DELAY 10


//#define DEBMSG
//#define DEBKOL
//#define DEBSINGLE
//#define DEBPAUSE
//#define deb fprintf

typedef long long ll;
typedef pair<int,int> pii;
#define mp make_pair
typedef vector<int> vi;

#if defined(DEBMSG)
#include <Windows.h>
int debnon, debmni;
int NumberOfNodes() { return debnon; }
int MyNodeId() { return debmni; }
queue<ll> sendQueue[MAX_NON];
int sendId[MAX_NON];
queue<ll> recvQueue[MAX_NON];
int recvId[MAX_NON];
void PutChar(int t, char c) { sendQueue[t].push(c); }
void PutInt(int t, int i) { sendQueue[t].push(i); }
void PutLL(int t, ll l) { sendQueue[t].push(l); }
void Send(int t) {
    char fname1[256], fname2[256];
    int id1 = sendId[t]++;
    sprintf(fname1, "pipe_%d_%d_%d", debmni, t, id1);
    int id2 = sendId[t]++;
    sprintf(fname2, "pipe_%d_%d_%d", debmni, t, id2);
    FILE *f2 = fopen(fname2, "w");
    fprintf(f2, "%d", sendQueue[t].size());
    while (!sendQueue[t].empty()) {
        fprintf(f2, " %lld", sendQueue[t].front());
        sendQueue[t].pop();
    }
    fclose(f2);
    FILE *f1 = fopen(fname1, "w");
    fclose(f1);
}
int Receive(int s) {
    vector<string> fname1(debnon), fname2(debnon);
    bool cont = true;
    while (cont) for (int i=0; i<debnon; ++i) if (s < 0 || i == s) {
        Sleep(SLEEP_DELAY);
        int id1 = recvId[i];
        char buf[256];
        sprintf(buf, "pipe_%d_%d_%d", i, debmni, id1);
        FILE *f1 = fopen(buf, "r");
        if (f1 == NULL)
            continue;
        fclose(f1);
        remove(buf);
        s = i;
        cont = false;
        break;
    }
    int id2 = ++recvId[s];
    ++recvId[s];
    char buf[256];
    sprintf(buf, "pipe_%d_%d_%d", s, debmni, id2);
    FILE *f2 = fopen(buf, "r");
    int n;
    fscanf(f2, "%d", &n);
    while (n--) {
        ll x;
        fscanf(f2, "%lld", &x);
        recvQueue[s].push(x);
    }
    fclose(f2);
    remove(buf);
    return s;
}
char GetChar(int s) { char c = recvQueue[s].front(); recvQueue[s].pop(); return c; }
int GetInt(int s) { int i = recvQueue[s].front(); recvQueue[s].pop(); return i; }
ll GetLL(int s) { ll l = recvQueue[s].front(); recvQueue[s].pop(); return l; }
#else // DEBMSG
#include "message.h"
#endif // DEBMSG

#ifndef deb
template<class T1, class T2> void deb(T1, T2) {}
template<class T1, class T2, class T3> void deb(T1, T2, T3) {}
template<class T1, class T2, class T3, class T4> void deb(T1, T2, T3, T4) {}
template<class T1, class T2, class T3, class T4, class T5> void deb(T1, T2, T3, T4, T5) {}
#endif // deb

#if defined (DEBKOL)

int NumberOfStudents() {
    return 200000000;
}

int FirstNeighbor(int i) {
    int nos = NumberOfStudents();
    int a = i+1; if (a > nos) a = 1;
    int b = i-1; if (b < 1) b = nos;
    return a < b ? a : b;
}

int SecondNeighbor(int i) {
    int nos = NumberOfStudents();
    int a = i+1; if (a > nos) a = 1;
    int b = i-1; if (b < 1) b = nos;
    return a > b ? a : b;
}

int NumberOfQueries() {
    return 200;
}

int QueryFrom(int i) {
    int nos = NumberOfStudents();
    return (100000 * i) % nos + 1;
}

int QueryTo(int i) {
    int nos = NumberOfStudents();
    return (200000 * i) % nos + 1;
}
#else // DEBKOL
#include "kollib.h"
#endif // DEBKOL

inline int Other(int x, int a) {
    return FirstNeighbor(x) + SecondNeighbor(x) - a;
}

struct node {
    int a, b;
    node() : a(-1), b(-1) {}
    node(const node &oth) : a(oth.a), b(oth.b) {}
    node(int _a, int _b) : a(_a), b(_b) {}
    node rev() const { return node(b, a); }
    bool operator==(const node &oth) const { return a == oth.a && b == oth.b; }
    bool operator!=(const node &oth) const { return !(this->operator==(oth)); }
    bool operator<(const node &oth) const { return a < oth.a || (a == oth.a && b < oth.b); }
};


set<node> nodes;
map<node, pair<node, int> > ranges;
vector<pair<node, pair<node, int> > > synch;

int pos;
node walk(node ab, int &steps) {
    if (ranges.count(ab) > 0) {
        steps--;
        pos += ranges[ab].second;
        return ranges[ab].first;
    }
    int len = 0;
    node cd(ab);
    for (; steps > 0; steps--) {
        int c = Other(cd.b, cd.a);
        cd.a = cd.b;
        cd.b = c;
        len++;
        if (nodes.count(cd) != 0)
            break;
    }
    nodes.insert(cd);
    nodes.insert(cd.rev());
    ranges[ab] = mp(cd, len);
    ranges[cd.rev()] = mp(ab.rev(), len);
    synch.push_back(mp(ab, mp(cd, len)));
    pos += len;
    return cd;
}

int stepsForRound(int nr, int nos, int non) {
    return 2000000;
}

const char MYRANGES = 0;
const char OTHRANGES = 1;
const char GOTSOLUTION = 2;
const char QUIT = 3;
const char WRITESOLUTION = 4;

int realmain(int argc, char **argv) {
#if defined(DEBMSG)
#if defined(DEBSINGLE)
    debnon = 1;
    debmni = 0;
#else // DEBSINGLE
    debnon = atoi(argv[1]);
    debmni = atoi(argv[2]);
#endif // DEBSINGLE
#endif // DEBMSG

    int non = NumberOfNodes();
    int mni = MyNodeId();
    int n = NumberOfStudents();

    unsigned long seed = time(NULL) - mni * 1000;
    fprintf(stderr, "seed = %d\n", seed);
    srand(seed);

    for (int i=NumberOfQueries(); i>0; --i) {
        int x = QueryFrom(i);
        int a = FirstNeighbor(x);
        int b = SecondNeighbor(x);
        nodes.insert(node(a, x));
        nodes.insert(node(x, a));
        nodes.insert(node(b, x));
        nodes.insert(node(x, b));
        ranges[node(a, x)] = mp(node(x, b), 1);
        ranges[node(b, x)] = mp(node(x, a), 1);

        deb(stderr, "INITIAL %d %d %d\n", a, x, b);

        x = QueryTo(i);
        a = FirstNeighbor(x);
        b = SecondNeighbor(x);
        nodes.insert(node(a, x));
        nodes.insert(node(x, a));
        nodes.insert(node(b, x));
        nodes.insert(node(x, b));
        ranges[node(a, x)] = mp(node(x, b), 1);
        ranges[node(b, x)] = mp(node(x, a), 1);

        deb(stderr, "INITIAL %d %d %d\n", a, x, b);
    }

    int rnd = rand();
    for (int i=0; i<100; ++i) {
        rnd <<= 5;
        rnd ^= rand();
    }
    while (rnd < 0) rnd += 1000000000;
    rnd %= NumberOfStudents();
    rnd++;
    fprintf(stderr, "rnd = %d\n", rnd);

    int a = rnd, b = FirstNeighbor(rnd);
    node start(a, b);
    nodes.insert(start);
    nodes.insert(start.rev());

    pos = 0;
    node ab(start);
    for (int nr=0; ; ++nr) {
        int cent = nr % non;
        // local computations
        int steps = stepsForRound(nr, n, non);
        while (steps > 0) {
            ab = walk(ab, steps);
            if (pos >= n)
                break;
        }
        if (pos >= n) {
            // got solution
            if (mni == cent) {
                deb(stderr, "I got solution, I'm central node\n");
                for (int i=0; i<non; ++i) if (i != mni) {
                    deb(stderr, "Send quit to %d\n", i);
                    PutChar(i, QUIT);
                    Send(i);
                }
            } else {
                deb(stderr, "I got solution, send info to central %d\n", cent);
                PutChar(cent, GOTSOLUTION);
                Send(cent);
                Receive(cent);
                if (GetChar(cent) == QUIT) {
                    deb(stderr, "QUIT received\n");
                    deb(stderr, "return 0;\n");
                    return 0;
                }
                deb(stderr, "WRITESOLUTION received\n");
            }
            // write solution here
            int noq = NumberOfQueries();
            map<int,int> mm;
            vector<pii> qq(noq);
            for (int i=0; i<noq; ++i) {
                int a = QueryFrom(i+1);
                int b = QueryTo(i+1);
                deb(stderr, "QUERY %d: %d %d\n", i, a, b);
                qq[i] = mp(a, b);
                mm[a] = -1;
                mm[b] = -1;
            }
            int cur = 0;
            node ab = start;
            do {
                deb(stderr, "I'm here: %d %d\n", ab.a, cur);
                if (mm.count(ab.a) > 0) mm[ab.a] = cur;
                if (mm.count(ab.b) > 0) mm[ab.b] = cur+1;
                pair<node, int> &p = ranges[ab];
                cur += p.second;
                deb(stderr, "%d %d  %d\n", ab.a, ab.b, p.second);
                ab = p.first;
            } while (cur <= n);
            for (int i=0; i<noq; ++i) {
                int a = mm[qq[i].first];
                int b = mm[qq[i].second];
                deb(stderr, "QUERY %d %d\n", qq[i].first, qq[i].second);
                deb(stderr, "RESUL %d %d\n", a, b);
                int res = std::abs(a - b);
                if (n - res < res)
                    res = n - res;
                printf("%d\n", res);
            }
            return 0;
        }
        // synchronization
        if (mni == cent) {
            int sum = synch.size();
            vector<vector<pair<node, pair<node, int> > > > v(non);
            std::swap(synch, v[mni]);
            int finish = -1;
            for (int i=1; i<non; ++i) {
                int s = Receive(-1);
                char cmd = GetChar(s);
                if (cmd == GOTSOLUTION) {
                    deb(stderr, "Received GOTSOLUTION from %d\n", s);
                    finish = s;
                    break;
                }
                deb(stderr, "Received MYRANGES from %d\n", s);
                int rs = GetInt(s);
                sum += rs;
                for (int i=0; i<rs; ++i) {
                    int a = GetInt(s);
                    int b = GetInt(s);
                    int c = GetInt(s);
                    int d = GetInt(s);
                    int len = GetInt(s);
                    v[s].push_back(mp(node(a, b), mp(node(c, d), len)));
                }
            }
            if (finish >= 0) {
                PutChar(finish, WRITESOLUTION);
                Send(finish);
                deb(stderr, "Send WRITESOLUTION to %d\n", finish);
                for (int i=0; i<non; ++i) if (i != mni && i != finish) {
                    PutChar(i, QUIT);
                    Send(i);
                    deb(stderr, "Send QUIT to %d\n", i);
                }
                deb(stderr, "return 0;\n");
                return 0;
            }
            for (int t=0; t<non; ++t) if (t != mni) {
                PutChar(t, OTHRANGES);
                PutInt(t, sum - v[t].size());
                for (int i=0; i<non; ++i) if (i != t) {
                    for (int j=0; j<v[i].size(); ++j) {
                        PutInt(t, v[i][j].first.a);
                        PutInt(t, v[i][j].first.b);
                        PutInt(t, v[i][j].second.first.a);
                        PutInt(t, v[i][j].second.first.b);
                        PutInt(t, v[i][j].second.second);
                    }
                }
                Send(t);
                deb(stderr, "Send OTHRANGES to %d\n", t);
            }
            std::swap(synch, v[0]);
            for (int i=1; i<v.size(); ++i) {
                for (int j=0; j<v[i].size(); ++j)
                    synch.push_back(v[i][j]);
            }
        } else {
            PutChar(cent, MYRANGES);
            PutInt(cent, synch.size());
            for (int i=0; i<synch.size(); ++i) {
                PutInt(cent, synch[i].first.a);
                PutInt(cent, synch[i].first.b);
                PutInt(cent, synch[i].second.first.a);
                PutInt(cent, synch[i].second.first.b);
                PutInt(cent, synch[i].second.second);
            }
            Send(cent);
            deb(stderr, "Send MYRANGES to cent %d\n", cent);
            Receive(cent);
            if (GetChar(cent) == QUIT) {
                deb(stderr, "Got QUIT from cent %d\n", cent);
                return 0;
            }
            deb(stderr, "Got OTHRANGES from cent %d\n", cent);
            int rs = GetInt(cent);
            for (int i=0; i<rs; ++i) {
                int a = GetInt(cent);
                int b = GetInt(cent);
                int c = GetInt(cent);
                int d = GetInt(cent);
                int len = GetInt(cent);
                synch.push_back(mp(node(a, b), mp(node(c, d), len)));
            }
        }
        // local update
        for (int i=0; i<synch.size(); ++i) {
            //TODO need sth more here?
            node &ab = synch[i].first;
            node &cd = synch[i].second.first;
            int &len = synch[i].second.second;
            nodes.insert(ab);
            nodes.insert(ab.rev());
            nodes.insert(cd);
            nodes.insert(cd.rev());
            ranges[ab] = mp(cd, len);
            ranges[cd.rev()] = mp(ab.rev(), len);
        }
        synch.clear();
    }

    return 0;
}

int main(int argc, char **argv) {
    int res = realmain(argc, argv);
#if defined(DEBPAUSE)
    system("PAUSE");
#endif
    return res;
}