#include "message.h"
#include "kollib.h"
#include <iostream>
using namespace std;
inline int abs(int x)
{
return (x < 0 ? -x : x);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
long long nodes = NumberOfNodes();
int node = MyNodeId();
long long n = NumberOfStudents();
if (nodes > n) nodes = n;
int p = (node * n) / nodes;
int k = ((node + 1) * n) / nodes;
int ln = k - p;
int *lp = new int[ln];
//int lp[10000000];
//int *lp = new int[n];
int last = 0;
int cur = 1;
int next;
for (int i = 0; i < n; ++i)
{
if (p < cur && cur <= k) lp[cur-1-p] = i;
//cout << cur << endl;
//lp[cur-1] = i;
next = FirstNeighbor(cur);
if (last == next) next = SecondNeighbor(cur);
last = cur;
cur = next;
}
/*
for (int i = 0; i < n; ++i)
{
cout << lp[i] << ' ';
}
cout << endl;
*/
if (node > 0)
{
int m = NumberOfQueries();
int f, t;
for (int j = 1; j <= m; ++j)
{
f = QueryFrom(j);
t = QueryTo(j);
if (p < f && f <= k)
{
PutInt(0, lp[f-1-p]);
Send(0);
}
if (p < t && t <= k)
{
PutInt(0, lp[t-1-p]);
Send(0);
}
}
}
else
{
int m = NumberOfQueries();
int f, t, w;
int fw, tw, fn, tn;
for (int j = 1; j <= m; ++j)
{
f = QueryFrom(j);
t = QueryTo(j);
fn = (f-1) * nodes / n;
tn = (t-1) * nodes / n;
if (fn > 0)
{
Receive(fn);
fw = GetInt(fn);
}
else
{
fw = lp[f-1];
}
//cout << j << ": " << fw;
if (tn > 0)
{
Receive(tn);
tw = GetInt(tn);
}
else
{
tw = lp[t-1];
}
//cout << ", " << tw << endl;
w = abs(fw - tw);
//w = abs(lp[f-1] - lp[t-1]);
if (n - w < w) w = n - w;
cout << w << '\n';
}
}
}
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 | #include "message.h" #include "kollib.h" #include <iostream> using namespace std; inline int abs(int x) { return (x < 0 ? -x : x); } int main() { ios::sync_with_stdio(false); cin.tie(NULL); long long nodes = NumberOfNodes(); int node = MyNodeId(); long long n = NumberOfStudents(); if (nodes > n) nodes = n; int p = (node * n) / nodes; int k = ((node + 1) * n) / nodes; int ln = k - p; int *lp = new int[ln]; //int lp[10000000]; //int *lp = new int[n]; int last = 0; int cur = 1; int next; for (int i = 0; i < n; ++i) { if (p < cur && cur <= k) lp[cur-1-p] = i; //cout << cur << endl; //lp[cur-1] = i; next = FirstNeighbor(cur); if (last == next) next = SecondNeighbor(cur); last = cur; cur = next; } /* for (int i = 0; i < n; ++i) { cout << lp[i] << ' '; } cout << endl; */ if (node > 0) { int m = NumberOfQueries(); int f, t; for (int j = 1; j <= m; ++j) { f = QueryFrom(j); t = QueryTo(j); if (p < f && f <= k) { PutInt(0, lp[f-1-p]); Send(0); } if (p < t && t <= k) { PutInt(0, lp[t-1-p]); Send(0); } } } else { int m = NumberOfQueries(); int f, t, w; int fw, tw, fn, tn; for (int j = 1; j <= m; ++j) { f = QueryFrom(j); t = QueryTo(j); fn = (f-1) * nodes / n; tn = (t-1) * nodes / n; if (fn > 0) { Receive(fn); fw = GetInt(fn); } else { fw = lp[f-1]; } //cout << j << ": " << fw; if (tn > 0) { Receive(tn); tw = GetInt(tn); } else { tw = lp[t-1]; } //cout << ", " << tw << endl; w = abs(fw - tw); //w = abs(lp[f-1] - lp[t-1]); if (n - w < w) w = n - w; cout << w << '\n'; } } } |
English