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
#include "kollib.h"
#include "message.h"
#define MAX_INST_N 1000006

#include <algorithm>
#include <cstdio>
//#include <unistd.h>

using namespace std;

/* structure must be 8byte-aligned!!!! */
void struct_send( int target, long long *_s, int size )
{
   if ( !size ) return;
   while ( size-- )
     PutLL( target, *(_s++) );
   Send( target );
}

/* structure must be 8byte-aligned!!!! */
void struct_recv( int target, long long *_s, int size )
{
   if ( !size ) return;
   Receive( target );
   while ( size-- )
      *(_s++) = GetLL( target );
}

/* structure must be 8byte-aligned!!!! */
#define send( tt, s ) struct_send( tt, ( long long * )&s, sizeof( s )/8 );
#define recv( tt, s ) struct_recv( tt, ( long long * )&s, sizeof( s )/8 ); 

/* structure must be 8byte-aligned!!!! */
void put_data( int target, long long *_s, int size )
{
   while ( size-- )
     PutLL( target, *(_s++) );
}

/* structure must be 8byte-aligned!!!! */
void get_data( int target, long long *_s, int size )
{
   while ( size-- )
      *(_s++) = GetLL( target );
}

struct elm_descr
{
    int elm;
    int q_num; /* if 0 end of looking */
}T[1003];

int E[1003];

bool sort_func( struct elm_descr e1, struct elm_descr e2 )
{
    if ( e1.elm != e2.elm )
       return e1.elm < e2.elm;
    return e1.q_num < e2.q_num;
}

struct beg_setup
{
    int num_elms;
    int beg_elm;
};

struct ret_setup
{
    int r_num;
    int len0;
    int last0;
    int len1;
    int last1;
    int __a;
};

struct ret_report
{
    int src_elm;
    int dist;
    int side;
    int q_num;
};

struct beg_setup _S[ 102 ];
int t;

void setup_begin()
{
    int n = NumberOfStudents(  );
    int q = NumberOfQueries (  );
    int ilt = 0;
    srand( n*q*349734942+n*893298+q*478378+982987 );
    
    T[ilt++]={0,0};
    
    for ( int i=1; i<=q; i++ )
        {
            T[ilt++]={QueryFrom(i),i*2  };
            T[ilt++]={QueryTo  (i),i*2+1};
        }
    
    for ( int i=0; i<t; i++ )
        {
            _beg:;
            E[i]=((unsigned int)(((unsigned int)rand())*((unsigned int)rand())*((unsigned int)rand())))%n+1;
            for ( int j=0; j<i; j++ ) if ( E[j]==E[i] ) goto _beg;
            T[ilt++]={E[i],0};
            _S[i].beg_elm = E[i];
        }
    //for ( int i=0; i<t; i++ ) printf("%d ",E[i]); printf("\n");
    
    sort( T, T+ilt, sort_func );
    
    int tt=1; while ( tt<=ilt ) tt*=2;
    for ( int i=ilt; i<tt; i++ ) T[i]={1000000009,0};
    ilt=tt;
    
    //for ( int i=0; i<ilt; i++ ) printf("{%d %d} ",T[i].elm,T[i].q_num); printf("\n");
    
    for ( int i=0; i<t; i++ )
        {
            _S[i].num_elms=ilt;
            send( i, _S[i] );
            struct_send( i, (long long*)T, ilt*(sizeof(T[0])/8) );
        }
}

int find_elm_pos( int beg_t, int e )
{
    int t=0;
    while ( beg_t )
          {
              int _t=t+beg_t;
              if ( T[_t].elm<e ) t=_t;
              beg_t>>=1;
          }
    t++;
    return t;
}

void run_thread()
{
    int th_num=MyNodeId();
    struct ret_report _R[502];
    struct ret_setup  _r;
    int ilr=0;
    
    struct beg_setup setup_data;
    recv( 0, setup_data );
    struct_recv( 0, (long long*)T, setup_data.num_elms*(sizeof(T[0])/8) );
    int pos_look_dist = setup_data.num_elms/2;
    
    int pos_prev=-1;
    int pos = setup_data.beg_elm;
    int l=0;
    int _beg=1;
    while ( 1 )
          {
             int _e = find_elm_pos( pos_look_dist, pos );
             //printf("%d %d %d\n",setup_data.beg_elm,pos,T[_e].elm);
             while ( T[_e].elm == pos )
                {
                    if ( T[_e].q_num )
                       {
                           _R[ilr++]={th_num,l,0,T[_e].q_num};
                       } else
                       {
                           if ( !_beg )
                              {
                                _r.last0=pos;
                                _r.len0=l;
                                goto _next0;
                              }
                           _beg=0;
                       }
                    _e++;
                }
             
             int _p = SecondNeighbor( pos );
             if ( _p == pos_prev )
                 _p = FirstNeighbor( pos );
             pos_prev=pos;
             pos = _p;
             l++;
          }
    _next0:;
    
    pos_prev=-1;
    pos = setup_data.beg_elm;
    l=0;
    _beg=1;
    while ( 1 )
          {
             int _e = find_elm_pos( pos_look_dist, pos );
             while ( T[_e].elm == pos )
                {
                    if ( T[_e].q_num )
                       {
                           _R[ilr++]={th_num,l,1,T[_e].q_num};
                       } else
                       {
                           if ( !_beg )
                              {
                                _r.last1=pos;
                                _r.len1=l;
                                goto _next1;
                              }
                           _beg=0;
                       }
                    _e++;
                }
             
             int _p = FirstNeighbor( pos );
             if ( _p == pos_prev )
                 _p = SecondNeighbor( pos );
             pos_prev=pos;
             pos = _p;
             l++;
          }
    _next1:;
    
    _r.r_num=ilr;
    send( 0, _r );
    struct_send( 0, (long long *)_R, _r.r_num*sizeof(_R[0])/8 );
    //printf("%d %d\n",_r.r_num,setup_data.beg_elm);
    //for ( int i=0; i<_r.r_num; i++ ) printf("{%d %d %d %d} ",_R[i].src_elm,_R[i].side,_R[i].dist,_R[i].q_num); printf("\n");
}

void calculate_end()
{
    int n = NumberOfStudents(  );
    int q = NumberOfQueries (  );
    
    struct ret_report  R[1003];
    struct ret_setup  RS[1003];
    int ilr=0;
    
    for ( int i=0; i<t; i++ )
        {
            recv( i, RS[i] );
            struct_recv( i, (long long *)(R+ilr), RS[i].r_num*(sizeof(R[0])/8) );
            ilr+=RS[i].r_num;
        }
    
    //for ( int i=0; i<t; i++) printf("%d ",E[i]); printf("\n");
    //for ( int i=0; i<t; i++) printf("{%d %d %d %d} ",RS[i].last0,RS[i].len0,RS[i].len1,RS[i].last1); printf("\n");
    for ( int i=0; i<t; i++)
        {
            int _e = RS[i].last0;
            int _w=-1;
            for ( int j=0; j<t&&_w==-1; j++ ) if ( E[j]==_e ) _w=j;
            RS[i].last0=_w;
            
            _e = RS[i].last1;
            _w=-1;
            for ( int j=0; j<t&&_w==-1; j++ ) if ( E[j]==_e ) _w=j;
            RS[i].last1=_w;
            //printf("{%d %d} ",RS[i].last0,RS[i].last1);
        }
    //printf("\n");
    
    struct ret_report pos[202][2];
    for ( int i=0; i<ilr; i++ )
        pos[(R[i].q_num/2)-1][R[i].q_num%2]=R[i];
        
    for ( int i=0; i<q; i++ )
        {
            //printf("{%d %d %d} {%d %d %d}\n",pos[i][0].src_elm,pos[i][0].dist,pos[i][0].side,pos[i][1].src_elm,pos[i][1].dist,pos[i][1].side);
            int p = pos[i][0].src_elm;
            int k = pos[i][1].src_elm;
            
            int s         =  0;
            int prev      = -1;
            int last_side =  0;
            
            if ( pos[i][0].side )
                s += pos[i][0].dist;
               else
                s -= pos[i][0].dist;
            //printf("111 %d\n",s);
            
            while ( p != k )
               {
                   //printf("  %d\n",p);
                   if ( RS[ p ].last0 != prev )
                      {
                          s += RS[ p ].len0;
                          prev=p;
                          p  = RS[ p ].last0;
                          last_side=0;
                      } else
                      {
                          s += RS[ p ].len1;
                          prev=p;
                          p  = RS[ p ].last1;
                          last_side=1;
                      }
               }
            //printf("  %d\n",p);
            //printf("222 %d\n",s);
            
            if ( last_side ^ pos[i][1].side )
                s -= pos[i][1].dist;
               else
                s += pos[i][1].dist;
            //printf("333 %d\n",s);
            
            s = (s+n)%n;
            printf( "%d\n", min( s, n-s ) );
        }
}

int main()
{
    t = NumberOfNodes();
    int n = NumberOfStudents();
    int tnum = MyNodeId();
    if ( n<150 )
       {
           t = 1;
           if (tnum) return 0;
       }
    
    if ( !tnum )
        setup_begin();
    run_thread();
    if ( !tnum )
        calculate_end();
    return 0;
}