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
#include "kollib.h"
#include <iostream>
#include "message.h"
#include <vector>

using namespace std;

int node;
int nodes;
int n;


inline int count(int from, int to) {
  if (from == to) return 0;
  int half = (n/2) + (n%2);
  
  ///////////////////
  int start = from;
  int cnt = 1;
  int last = start;
  
  start = FirstNeighbor(start);
  
  bool found = false;
  
  while (cnt <= half) {
    //cerr << start << endl;
    if (start == to) {
      found = true;
      break;
    }
    
    if (FirstNeighbor(start) != last) {
      last = start;
      start = FirstNeighbor(start); // move forward
    } else {
      last = start;
      start = SecondNeighbor(start); // move forward
    }
    
    
    
    ++cnt;
  }
  
  if (found) return cnt;
  
  /////////////////////////////
  start = from; // try different way
  cnt = 1;
  last = start;
  
  start = SecondNeighbor(start);
  
  while (cnt <= half) {
   // cerr << start << endl;
    if (start == to) {
      break;
    }
    
    if (FirstNeighbor(start) != last) {
      last = start;
      start = FirstNeighbor(start); // move forward
    } else {
      last = start;
      start = SecondNeighbor(start); // move forward
    }
    
    ++cnt;
  }
  
  return cnt;  
}

int main() {
  cin.sync_with_stdio(false);

  node = MyNodeId();
  nodes = NumberOfNodes();
  
  int m = NumberOfQueries();
  if (m < nodes) nodes = m;
  
  
  n = NumberOfStudents();
  
  
  for (int i = 0; i < m; ++i) {
    if (node == i % nodes) {
      //cout << "Counting for " << i << " on node " << node << endl;
      int result = count(QueryFrom(i+1), QueryTo(i+1));
      //cout << "Result " << result << endl;
      //cout << "On node " << node << " time: " << (clock()/(double)CLOCKS_PER_SEC) << endl;

      PutInt(0, result);
    }
  }
  
  for (int i = 0; i < nodes; ++i) {
    Send(0);
  }
  
  
  if (node == 0) {
    for (int i = 0; i < nodes; ++i) {
      Receive(i);
    }   
    
    vector<int> v(m);
    for (int i = 0; i < m; ++i) {
       v[i] = GetInt(i % nodes);
       //cout << "Recv from " << i % nodes << " node: " << v[i] << endl;
    }
    
    for (int i = 0; i < m; ++i) {
      cout << v[i] << endl;
    }
  }
  
  
  return 0;
}