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
#include <cstdio>
#include <iostream>
#include <cassert>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k));
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);

#include "message.h"
#include "kollib.h"

const int D = 0;

const ll random3500[] = {19828997816361LL, 20583072294788LL, 31523795971286LL, 6126619385744LL, 18068180097777LL, 3216248078827LL, 5837921948379LL, 4189745625356LL, 13295642116388LL, 21172693223708LL, 23328598889799LL, 2007475957010LL, 28238894457923LL, 30820391823014LL, 32570389887578LL, 16257777635896LL, 7375295999581LL, 35071628864508LL, 13807531970117LL, 29559064176141LL, 3259533570076LL, 310224160562LL, 9602030642000LL, 29470698166392LL, 7226007305746LL, 16113588943862LL, 3810279219017LL, 25860651954511LL, 12713265940804LL, 14959159418064LL, 34831917990448LL, 5946014247995LL, 2236281831158LL, 5190413828253LL, 31845778985503LL, 15007765046986LL, 24036949031865LL, 28910049849417LL, 6259718527102LL, 5473403157717LL, 14270329052594LL, 24004310059212LL, 16722587278439LL, 29366876581652LL, 23326425039405LL, 33401876221286LL, 12173517571671LL, 29799897083903LL, 9567021349473LL, 26007709820446LL, 26786464864534LL, 17624280347366LL, 34994934580100LL, 18694053529207LL, 22052141951976LL, 29630038055251LL, 26145950633490LL, 10065616992372LL, 25793248498164LL, 21119156509343LL, 5083095539801LL, 28367026980815LL, 4067928021748LL, 4003397534163LL, 24141520953416LL, 15365462329717LL, 10216048613661LL, 20331873279424LL, 5636612149368LL, 24480159103812LL, 11819941501008LL, 16095387811681LL, 21995702619259LL, 32773873711111LL, 10204012638850LL, 14450634351216LL, 21256278069348LL, 21034210114481LL, 29160277568873LL, 19905428741190LL, 25962599618454LL, 8545040331737LL, 24576430591335LL, 17396851354871LL, 21534558412496LL, 28306142091382LL, 30530680048059LL, 25602346471086LL, 34357929137529LL, 10760153581787LL, 30576786148837LL, 5449590792005LL, 2695780207144LL, 19178584041717LL, 10511358604377LL, 5371203469351LL, 11044322303606LL, 30728048278210LL, 33213118719592LL, 27618276706410LL, 33639335218696LL, 23005264474132LL, 17769597275498LL, 32368539084072LL, 4030281929942LL, 17480295002247LL, 13453650215215LL, 21331379405853LL, 24905549643969LL, 30372388982720LL, 17483072528296LL};

const int HASH = 1 << 18;
int getHash (int x) {
   return (1LL * 1000000007 * x) % HASH;
}
bool possiblyInterestingHash[HASH];

vector<pii> toReturn;
vi startPoints;
vi queryPoints;
int n;
void go (int gdzie, int skad, const int kier) {
   int x = kier;
   while (1) {
      if (possiblyInterestingHash[getHash(gdzie)]) {
         const bool isStartPoint = binary_search(ALL(startPoints), gdzie);
         const bool isQueryPoint = binary_search(ALL(queryPoints), gdzie);
         if (isStartPoint || isQueryPoint) toReturn.pb(mp(x,gdzie));
         if (isStartPoint) return;
      }

      int nxt;
      if (gdzie > n/2) {
         // moze jest drugim?
         nxt = FirstNeighbor(gdzie);
         if (nxt == skad) nxt = SecondNeighbor(gdzie);
      } else {
         // gdzie pewnie jest pierwszym
         nxt = SecondNeighbor(gdzie);
         if (nxt == skad) nxt = FirstNeighbor(gdzie);
      }

      skad = gdzie;
      gdzie = nxt;
      x += kier;
   }
}
map<int, vector<pii> > mapa;
map<int, int> koord;
void buduj (const int gdzie, const int skad, const int poczatek, const int X) {
   if (gdzie == poczatek) {
      if (skad != -1) {
         if (D) assert(X == n);
         return;
      }
   }
   const vector<pii> &v = mapa[gdzie];
   if (D) assert(!v.empty());
   const int koniec1 = v[0].se, koniec2 = v.back().se;
   int dokad = koniec1;
   if (dokad == skad) dokad = koniec2;

   // zbuduj kawalek [gdzie, dokad)
   if (dokad == koniec1) {
      // interesujace sa ujemne
      for (int i = 1; v[i].fi <= 0; ++i) {
         koord[v[i].se] = X + (- v[i].fi);
      }
      buduj(dokad, gdzie, poczatek, X + (-v[0].fi));
   } else {
      // interesujace sa dodatnie
      int i = 0;
      while (v[i].fi < 0) ++i;
      // v[i] == (0, gdzie)
      for (; i < SZ(v) - 1; ++i) {
         koord[v[i].se] = X + v[i].fi;
      }
      buduj(dokad, gdzie, poczatek, X + v.back().fi);
   }
}


int start[1111];

int main () {
   n = NumberOfStudents();
   const int my = MyNodeId(), k = NumberOfNodes();

   start[1] = random3500[1] % n + 1;
   start[2] = random3500[2] % n + 1;
   if (start[2] == start[1]) start[2] = 1 + start[2] % n;
   start[3] = random3500[3] % n + 1;
   while (start[3] == start[1] || start[3] == start[2]) start[3] = 1 + start[3] % n;
   REP(i,4,k-1) {
      start[i] = random3500[i] % n + 1;
   }
   REP(i,1,k-1) {
      startPoints.pb(start[i]);
   }
   sort(ALL(startPoints));
   startPoints.erase(unique(ALL(startPoints)), startPoints.end());

   const int q = NumberOfQueries();
   FORI(i,q) {
      queryPoints.pb(QueryFrom(i));
      queryPoints.pb(QueryTo(i));
   }
   sort(ALL(queryPoints));
   queryPoints.erase(unique(ALL(queryPoints)), queryPoints.end());

   FOREACH(it,startPoints) {
      possiblyInterestingHash[getHash(*it)] = 1;
   }
   FOREACH(it,queryPoints) {
      possiblyInterestingHash[getHash(*it)] = 1;
   }

   if (my > 0) {
      const int myStart = start[my];
      const int le = FirstNeighbor(myStart), ri = SecondNeighbor(myStart);
      toReturn.pb(mp(0, myStart));
      go(le, myStart, +1);
      go(ri, myStart, -1);
      sort(ALL(toReturn));
      PutInt(0, SZ(toReturn));
      FOREACH(it,toReturn) {
         PutInt(0, it->fi);
         PutInt(0, it->se);
      }
      Send(0);
   } else { // my == 0
      FOR(lol,k-1) {
         const int inst = Receive(-1);
         int sz = GetInt(inst);
         vector<pii> v;
         int startingAt = -1;
         while (sz--) {
            const int x = GetInt(inst);
            const int gdzie = GetInt(inst);
            v.pb(mp(x,gdzie));
            if (x == 0) startingAt = gdzie;
         }
         if (D) assert(startingAt != -1);
         mapa[startingAt] = v; // najwyzej sie nadpisze tym samym
      }

      buduj(start[1], -1, start[1], 0);

      FORI(i,q) {
         int pos1 = koord[QueryFrom(i)], pos2 = koord[QueryTo(i)];
         if (pos1 > pos2) swap(pos1, pos2);
         printf("%d\n", min(pos2 - pos1, n - pos2 + pos1));
      }
   }
   return 0;
}