#include "kollib.h" #include "message.h" #include<cstdio> #include<cstdlib> #include<vector> #include<map> #include<set> #include<algorithm> #include<time.h> typedef long long int64; using namespace std; int INSTANCES = 0; int STUDENTS = 0; int QUERIES = 0; int ID = 0; int SKIP_RAND_BELOW = 1000; int MAX_INT_QUAN = 10000; int MAX_AVG_INT_LEN = 10; typedef struct { int node, from, to, qfrom, qto; } query; typedef struct { vector<int> data; vector<query> queue; int len, qb, qe; } smt; inline int rson(int n) { return 1 + (n << 1); } inline int lson(int n) { return n << 1; } inline int par(int n) { return n >> 1; } inline void smt_init(smt& m, int len) { int n = 1; while (n < len) { n <<= 1; } m.data.resize(n << 1, 0); query q; m.queue.resize(100, q); m.len = n; } inline void smt_set(smt& m, int pos, int val) { int cur = m.len + pos; m.data[cur] = val; cur = par(cur); while (cur) { m.data[cur] = m.data[lson(cur)] + m.data[rson(cur)]; cur = par(cur); } } inline int smt_sum(smt& m, int from, int to) { if (from > to) { int tmp = from; from = to; to = tmp; } int csum = 0; query q; q.node = 1; q.from = 0; q.to = m.len - 1; q.qfrom = from; q.qto = to; m.qe = 1; m.qb = 0; m.queue[0] = q; while (m.qe != m.qb) { q = m.queue[m.qb]; ++m.qb; if (q.to < q.from) { continue; } if (q.qto < q.from || q.qfrom > q.to) { continue; } if (q.qfrom <= q.from && q.qto >= q.to) { csum += m.data[q.node]; continue; } int mid = q.from + (q.to - q.from) / 2; query q2; if (q.qfrom <= mid) { q2.node = lson(q.node); q2.from = q.from; q2.to = mid; q2.qfrom = q.qfrom; q2.qto = min(mid, q.qto); m.queue[m.qe] = q2; ++m.qe; } if (q.qto >= mid + 1) { q2.node = rson(q.node); q2.from = mid + 1; q2.to = q.to; q2.qfrom = max(mid + 1, q.qfrom); q2.qto = q.qto; m.queue[m.qe] = q2; ++m.qe; } } return csum; } typedef struct { int id; vector<pair<int, int> > adj; } border_t; int main () { INSTANCES = NumberOfNodes(); STUDENTS = NumberOfStudents(); QUERIES = NumberOfQueries(); ID = MyNodeId(); vector<int> b; if (ID == 0) { set<int> borders; for (int i = 0; i < QUERIES; ++i) { borders.insert(QueryFrom(i + 1) - 1); borders.insert(QueryTo(i + 1) - 1); } if (STUDENTS > SKIP_RAND_BELOW || borders.size() < 2) { int req = max(2, min(MAX_INT_QUAN, STUDENTS / MAX_AVG_INT_LEN)); srand(time(NULL)); for (int i = borders.size(); i < req; ++i) { int rnd; do { rnd = rand() % STUDENTS; } while (borders.find(rnd) != borders.end()); borders.insert(rnd); } } b.reserve(borders.size()); for(set<int>::iterator it = borders.begin(); it != borders.end(); it++) { b.push_back(*it); } sort(b.begin(), b.end()); for (int i = 1; i < INSTANCES; ++i) { PutInt(i, b.size()); for (int j = 0; j < b.size(); ++j) { PutInt(i, b[j]); } Send(i); } } else { Receive(0); int n = GetInt(0); b.reserve(n); for (int i = 0; i < n; ++i) { b.push_back(GetInt(0)); } } vector<int> results; int current = ID; while (current < b.size()) { int prev = b[current]; int curr = FirstNeighbor(prev + 1) - 1; int dist = 1; while (!binary_search(b.begin(), b.end(), curr)) { int next = FirstNeighbor(curr + 1) - 1; if (next == prev) { next = SecondNeighbor(curr + 1) - 1; } prev = curr; curr = next; ++dist; } results.push_back(b[current]); results.push_back(curr); results.push_back(dist); prev = b[current]; curr = SecondNeighbor(prev + 1) - 1; dist = 1; while (!binary_search(b.begin(), b.end(), curr)) { int next = FirstNeighbor(curr + 1) - 1; if (next == prev) { next = SecondNeighbor(curr + 1) - 1; } prev = curr; curr = next; ++dist; } results.push_back(b[current]); results.push_back(curr); results.push_back(dist); current += INSTANCES; } if (ID) { PutInt(0, results.size()); for (int i = 0; i < results.size(); ++i) { PutInt(0, results[i]); } Send(0); return 0; } map<int, border_t> bmap; for (int i = 0; i < b.size(); ++i) { border_t bo; bo.id = b[i]; bmap[b[i]] = bo; } int cur = 0; while (cur < results.size()) { bmap[results[cur]].adj.push_back(make_pair(results[cur + 1], results[cur + 2])); cur += 3; } for (int i = 1; i < INSTANCES; ++i) { Receive(i); int n = GetInt(i); int cur = 0; while (cur < n) { int from = GetInt(i); int to = GetInt(i); int cost = GetInt(i); bmap[from].adj.push_back(make_pair(to, cost)); cur += 3; } } map<int, int> positions; smt s; smt_init(s, b.size()); int prev = -1; cur = b[0]; for (int i = 0; i < b.size(); ++i) { positions[cur] = i; border_t bo = bmap[cur]; pair<int, int> next = bo.adj[0]; if (next.first == prev) { next = bo.adj[1]; } prev = cur; cur = next.first; smt_set(s, i, next.second); } for (int i = 0; i < QUERIES; ++i) { int from = QueryFrom(i + 1) - 1; int to = QueryTo(i + 1) - 1; if (from == to) { printf("0\n"); continue; } int p1 = positions[from]; int p2 = positions[to]; int res = smt_sum(s, min(p1, p2), max(p1, p2) - 1); res = min(res, STUDENTS - res); printf("%d\n", res); } return 0; }
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 | #include "kollib.h" #include "message.h" #include<cstdio> #include<cstdlib> #include<vector> #include<map> #include<set> #include<algorithm> #include<time.h> typedef long long int64; using namespace std; int INSTANCES = 0; int STUDENTS = 0; int QUERIES = 0; int ID = 0; int SKIP_RAND_BELOW = 1000; int MAX_INT_QUAN = 10000; int MAX_AVG_INT_LEN = 10; typedef struct { int node, from, to, qfrom, qto; } query; typedef struct { vector<int> data; vector<query> queue; int len, qb, qe; } smt; inline int rson(int n) { return 1 + (n << 1); } inline int lson(int n) { return n << 1; } inline int par(int n) { return n >> 1; } inline void smt_init(smt& m, int len) { int n = 1; while (n < len) { n <<= 1; } m.data.resize(n << 1, 0); query q; m.queue.resize(100, q); m.len = n; } inline void smt_set(smt& m, int pos, int val) { int cur = m.len + pos; m.data[cur] = val; cur = par(cur); while (cur) { m.data[cur] = m.data[lson(cur)] + m.data[rson(cur)]; cur = par(cur); } } inline int smt_sum(smt& m, int from, int to) { if (from > to) { int tmp = from; from = to; to = tmp; } int csum = 0; query q; q.node = 1; q.from = 0; q.to = m.len - 1; q.qfrom = from; q.qto = to; m.qe = 1; m.qb = 0; m.queue[0] = q; while (m.qe != m.qb) { q = m.queue[m.qb]; ++m.qb; if (q.to < q.from) { continue; } if (q.qto < q.from || q.qfrom > q.to) { continue; } if (q.qfrom <= q.from && q.qto >= q.to) { csum += m.data[q.node]; continue; } int mid = q.from + (q.to - q.from) / 2; query q2; if (q.qfrom <= mid) { q2.node = lson(q.node); q2.from = q.from; q2.to = mid; q2.qfrom = q.qfrom; q2.qto = min(mid, q.qto); m.queue[m.qe] = q2; ++m.qe; } if (q.qto >= mid + 1) { q2.node = rson(q.node); q2.from = mid + 1; q2.to = q.to; q2.qfrom = max(mid + 1, q.qfrom); q2.qto = q.qto; m.queue[m.qe] = q2; ++m.qe; } } return csum; } typedef struct { int id; vector<pair<int, int> > adj; } border_t; int main () { INSTANCES = NumberOfNodes(); STUDENTS = NumberOfStudents(); QUERIES = NumberOfQueries(); ID = MyNodeId(); vector<int> b; if (ID == 0) { set<int> borders; for (int i = 0; i < QUERIES; ++i) { borders.insert(QueryFrom(i + 1) - 1); borders.insert(QueryTo(i + 1) - 1); } if (STUDENTS > SKIP_RAND_BELOW || borders.size() < 2) { int req = max(2, min(MAX_INT_QUAN, STUDENTS / MAX_AVG_INT_LEN)); srand(time(NULL)); for (int i = borders.size(); i < req; ++i) { int rnd; do { rnd = rand() % STUDENTS; } while (borders.find(rnd) != borders.end()); borders.insert(rnd); } } b.reserve(borders.size()); for(set<int>::iterator it = borders.begin(); it != borders.end(); it++) { b.push_back(*it); } sort(b.begin(), b.end()); for (int i = 1; i < INSTANCES; ++i) { PutInt(i, b.size()); for (int j = 0; j < b.size(); ++j) { PutInt(i, b[j]); } Send(i); } } else { Receive(0); int n = GetInt(0); b.reserve(n); for (int i = 0; i < n; ++i) { b.push_back(GetInt(0)); } } vector<int> results; int current = ID; while (current < b.size()) { int prev = b[current]; int curr = FirstNeighbor(prev + 1) - 1; int dist = 1; while (!binary_search(b.begin(), b.end(), curr)) { int next = FirstNeighbor(curr + 1) - 1; if (next == prev) { next = SecondNeighbor(curr + 1) - 1; } prev = curr; curr = next; ++dist; } results.push_back(b[current]); results.push_back(curr); results.push_back(dist); prev = b[current]; curr = SecondNeighbor(prev + 1) - 1; dist = 1; while (!binary_search(b.begin(), b.end(), curr)) { int next = FirstNeighbor(curr + 1) - 1; if (next == prev) { next = SecondNeighbor(curr + 1) - 1; } prev = curr; curr = next; ++dist; } results.push_back(b[current]); results.push_back(curr); results.push_back(dist); current += INSTANCES; } if (ID) { PutInt(0, results.size()); for (int i = 0; i < results.size(); ++i) { PutInt(0, results[i]); } Send(0); return 0; } map<int, border_t> bmap; for (int i = 0; i < b.size(); ++i) { border_t bo; bo.id = b[i]; bmap[b[i]] = bo; } int cur = 0; while (cur < results.size()) { bmap[results[cur]].adj.push_back(make_pair(results[cur + 1], results[cur + 2])); cur += 3; } for (int i = 1; i < INSTANCES; ++i) { Receive(i); int n = GetInt(i); int cur = 0; while (cur < n) { int from = GetInt(i); int to = GetInt(i); int cost = GetInt(i); bmap[from].adj.push_back(make_pair(to, cost)); cur += 3; } } map<int, int> positions; smt s; smt_init(s, b.size()); int prev = -1; cur = b[0]; for (int i = 0; i < b.size(); ++i) { positions[cur] = i; border_t bo = bmap[cur]; pair<int, int> next = bo.adj[0]; if (next.first == prev) { next = bo.adj[1]; } prev = cur; cur = next.first; smt_set(s, i, next.second); } for (int i = 0; i < QUERIES; ++i) { int from = QueryFrom(i + 1) - 1; int to = QueryTo(i + 1) - 1; if (from == to) { printf("0\n"); continue; } int p1 = positions[from]; int p2 = positions[to]; int res = smt_sum(s, min(p1, p2), max(p1, p2) - 1); res = min(res, STUDENTS - res); printf("%d\n", res); } return 0; } |