#include "kollib.h" #include "message.h" #include <algorithm> #include <vector> #include <cstdlib> #include <unordered_map> using namespace std; unordered_map<int, int> map; int n, m; int getNumber(int k) { if(map.count(k)) return map[k]; int s = k, prev = FirstNeighbor(k), i1, i2; for(i1 = 0; ; i1++) { int neigh = FirstNeighbor(k); if(neigh == prev) neigh = SecondNeighbor(k); prev = k; k = neigh; if(map.count(k)) break; } swap(s, k); prev = SecondNeighbor(k); for(i2 = 0; ; i2++) { int neigh = FirstNeighbor(k); if(neigh == prev) neigh = SecondNeighbor(k); prev = k; k = neigh; if(map.count(k)) break; } int v1 = map[s], v2 = map[k]; if(v1 > v2) return v1 - i1; else return v2 - i2; } int main() { if(MyNodeId()) return 0; n = NumberOfStudents(); m = NumberOfQueries(); int step = max(1, n / 1000); int k = 1, prev = FirstNeighbor(k), c = 0, p = step; while(true) { if(p == step) { p = 0; map.insert(make_pair(k, c)); } p++; int neigh = FirstNeighbor(k); if(neigh == prev) neigh = SecondNeighbor(k); if(neigh == 1) { map.insert(make_pair(k, c)); break; } prev = k; k = neigh; c++; } for(int i = 1; i <= m; i++) { int ans = abs(getNumber(QueryFrom(i)) - getNumber(QueryTo(i))); printf("%d\n", min(ans, n - ans)); } }
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 | #include "kollib.h" #include "message.h" #include <algorithm> #include <vector> #include <cstdlib> #include <unordered_map> using namespace std; unordered_map<int, int> map; int n, m; int getNumber(int k) { if(map.count(k)) return map[k]; int s = k, prev = FirstNeighbor(k), i1, i2; for(i1 = 0; ; i1++) { int neigh = FirstNeighbor(k); if(neigh == prev) neigh = SecondNeighbor(k); prev = k; k = neigh; if(map.count(k)) break; } swap(s, k); prev = SecondNeighbor(k); for(i2 = 0; ; i2++) { int neigh = FirstNeighbor(k); if(neigh == prev) neigh = SecondNeighbor(k); prev = k; k = neigh; if(map.count(k)) break; } int v1 = map[s], v2 = map[k]; if(v1 > v2) return v1 - i1; else return v2 - i2; } int main() { if(MyNodeId()) return 0; n = NumberOfStudents(); m = NumberOfQueries(); int step = max(1, n / 1000); int k = 1, prev = FirstNeighbor(k), c = 0, p = step; while(true) { if(p == step) { p = 0; map.insert(make_pair(k, c)); } p++; int neigh = FirstNeighbor(k); if(neigh == prev) neigh = SecondNeighbor(k); if(neigh == 1) { map.insert(make_pair(k, c)); break; } prev = k; k = neigh; c++; } for(int i = 1; i <= m; i++) { int ans = abs(getNumber(QueryFrom(i)) - getNumber(QueryTo(i))); printf("%d\n", min(ans, n - ans)); } } |