#include <algorithm> #include <cstdio> #include <cstdlib> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <cmath> #include <cstring> #include <string> #include <iostream> #include <complex> #include <sstream> #include <cassert> using namespace std; #include "message.h" #include "palindromy.h" typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define SIZE(c) ((int)((c).size())) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second int NODES; int ME; int ORIGINAL_LENGTH; // ...ABABABAB[oorriiggiinnaallABABABAB]ABABAB... inline char get(int k) { if (k<0 || k >= 2 * ORIGINAL_LENGTH) return (k%2 ? 'A' : 'B'); return GetLetter(k / 2); } struct Incomplete { int first; int second; int last; Incomplete(): first(-1), second(-1), last(-1) {} }; void send(int target, const Incomplete& incomplete) { PutInt(target, incomplete.first); PutInt(target, incomplete.second); PutInt(target, incomplete.last); Send(target); } Incomplete receive(int source) { Incomplete incomplete; Receive(source); incomplete.first = GetInt(source); incomplete.second = GetInt(source); incomplete.last = GetInt(source); return incomplete; } Incomplete merge(const Incomplete& left, const Incomplete& right) { if (left.first == -1) return right; if (right.first == -1) return left; Incomplete result; result.first = left.first; result.last = right.last; result.second = (left.second != -1 ? left.second : right.first); return result; } int T[100000000]; int main() { NODES = NumberOfNodes(); ME = MyNodeId(); ORIGINAL_LENGTH = GetLength(); int interval_length = (2 * ORIGINAL_LENGTH + NODES - 1) / NODES; int margin = 4 * interval_length; int start = ME * interval_length - margin; int g = 0; FOR(i, 1, 5 * interval_length) { if (2*g-i >= 0) T[i] = max(min(T[2*g-i], T[g]+g-i), 0); while (T[i] < 4 * interval_length && i-T[i]-1 >= 0 && get(start+i+T[i]) == get(start+i-T[i]-1)) ++T[i]; if (T[i]+i > T[g]+g) g=i; } LL result = 0; Incomplete incomplete; FOR(i,4*interval_length,5*interval_length) { if (T[i] >= 4*interval_length) { if (incomplete.first == -1) incomplete.first = i + start; else if (incomplete.second == -1) incomplete.second = i + start; incomplete.last = i + start; } else { result += (T[i]+1)/2; //printf("(1)[%d %d]\n", i+start, T[i]); } } int master = ME, nodes_per_interval = 1; while (interval_length < 2*ORIGINAL_LENGTH) { interval_length <<= 1; nodes_per_interval <<= 1; master -= master % nodes_per_interval; int previous_master = master + nodes_per_interval / 2; if (ME == previous_master) { send(master, incomplete); } if (ME == master) { if (previous_master < NODES) { Incomplete second_part = receive(previous_master); incomplete = merge(incomplete, second_part); } FOR(i,1,nodes_per_interval) if (ME + i < NODES) send(ME+i, incomplete); } else { incomplete = receive(master); } if (incomplete.first == -1) continue; assert(master + nodes_per_interval <= NODES); bool need_to_check_single = true; int single_radius = 2*interval_length; if (incomplete.first < incomplete.last) { need_to_check_single = false; int interval_start = interval_length * (master / nodes_per_interval); int interval_end = interval_start + interval_length; int diff = (incomplete.second - incomplete.first); int cyc = 2 * diff; int L = incomplete.first - 2 * interval_length; L += (nodes_per_interval - L%nodes_per_interval) % nodes_per_interval; L += (ME - master); int R = incomplete.last + 2 * interval_length; R -= (R % nodes_per_interval); R += -nodes_per_interval + (ME - master); int startL = L, startR = R; do { L -= nodes_per_interval; } while (L >= interval_start - 4 * interval_length - 5 && get(L + cyc) == get(L)); do { R += nodes_per_interval; } while (R <= interval_end + 4 * interval_length + 5 && get(R - cyc) == get(R)); cyc = -1; // don't use if (ME != master) { PutInt(master, L); PutInt(master, R); Send(master); } if (ME == master) { FOR(i,1,nodes_per_interval) { Receive(ME+i); L = max(L, GetInt(ME+i)); R = min(R, GetInt(ME+i)); } FOR(i,1,nodes_per_interval) { PutInt(ME+i, L); PutInt(ME+i, R); Send(ME+i); } } if (ME != master) { Receive(master); L = GetInt(master); R = GetInt(master); } int new_first = 1e9 + 500; int new_last = -1; for (int i = incomplete.first + (ME - master) * diff; i <= incomplete.last; i += nodes_per_interval * diff) { int radius = min(R - i, i-1 - L); if (radius >= 4 * interval_length || get(i - radius - 1) == get(i + radius)) { new_first = min(new_first, i); new_last = max(new_last, i); if (radius < 4 * interval_length) { need_to_check_single = true; single_radius = max(single_radius, radius); } } else { result += (radius+1)/2; } } if (ME != master) { PutInt(master, new_first); PutInt(master, new_last); PutInt(master, need_to_check_single); PutInt(master, single_radius); Send(master); } if (ME == master) { FOR(i,1,nodes_per_interval) { Receive(ME + i); new_first = min(new_first, GetInt(ME + i)); new_last = max(new_last, GetInt(ME + i)); need_to_check_single = GetInt(ME + i) || need_to_check_single; single_radius = max(single_radius, GetInt(ME+i)); } FOR(i,1,nodes_per_interval) { PutInt(ME+i, new_first); PutInt(ME+i, new_last); PutInt(ME+i, need_to_check_single); PutInt(ME+i, single_radius); Send(ME+i); } } if (ME != master) { Receive(master); new_first = GetInt(master); new_last = GetInt(master); need_to_check_single = GetInt(master) || need_to_check_single; single_radius = max(single_radius, GetInt(master)); } if (new_first > new_last) incomplete = Incomplete(); else { incomplete.first = new_first; incomplete.last = new_last; incomplete.second = (incomplete.first == incomplete.last ? -1 : incomplete.first + diff); } } if (need_to_check_single && incomplete.first != -1 && incomplete.first == incomplete.last) { int c = incomplete.first; int radius = single_radius - (single_radius%nodes_per_interval) - nodes_per_interval + (ME - master); do { radius += nodes_per_interval; } while (radius <= 4 * interval_length + 5 && get(c + radius - 1) == get(c - radius)); if (ME != master) { PutInt(master, radius); Send(master); } else { FOR(i,1,nodes_per_interval) { Receive(ME + i); radius = min(radius, GetInt(ME + i)); } } --radius; if (ME == master) { if (radius < 4 * interval_length) { //printf("(2)[%d %d]\n", c, radius); result += (radius+1)/2; incomplete.first = incomplete.second = incomplete.last = -1; } } } } if (ME > 0) { PutLL(0, result); Send(0); } else { //REP(i, LENGTH) printf("[%d]%c", i, get(i)); printf("\n"); FOR(i,1,NODES) { Receive(i); result += GetLL(i); } printf("%lld\n", result); } }
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 | #include <algorithm> #include <cstdio> #include <cstdlib> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <cmath> #include <cstring> #include <string> #include <iostream> #include <complex> #include <sstream> #include <cassert> using namespace std; #include "message.h" #include "palindromy.h" typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define SIZE(c) ((int)((c).size())) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second int NODES; int ME; int ORIGINAL_LENGTH; // ...ABABABAB[oorriiggiinnaallABABABAB]ABABAB... inline char get(int k) { if (k<0 || k >= 2 * ORIGINAL_LENGTH) return (k%2 ? 'A' : 'B'); return GetLetter(k / 2); } struct Incomplete { int first; int second; int last; Incomplete(): first(-1), second(-1), last(-1) {} }; void send(int target, const Incomplete& incomplete) { PutInt(target, incomplete.first); PutInt(target, incomplete.second); PutInt(target, incomplete.last); Send(target); } Incomplete receive(int source) { Incomplete incomplete; Receive(source); incomplete.first = GetInt(source); incomplete.second = GetInt(source); incomplete.last = GetInt(source); return incomplete; } Incomplete merge(const Incomplete& left, const Incomplete& right) { if (left.first == -1) return right; if (right.first == -1) return left; Incomplete result; result.first = left.first; result.last = right.last; result.second = (left.second != -1 ? left.second : right.first); return result; } int T[100000000]; int main() { NODES = NumberOfNodes(); ME = MyNodeId(); ORIGINAL_LENGTH = GetLength(); int interval_length = (2 * ORIGINAL_LENGTH + NODES - 1) / NODES; int margin = 4 * interval_length; int start = ME * interval_length - margin; int g = 0; FOR(i, 1, 5 * interval_length) { if (2*g-i >= 0) T[i] = max(min(T[2*g-i], T[g]+g-i), 0); while (T[i] < 4 * interval_length && i-T[i]-1 >= 0 && get(start+i+T[i]) == get(start+i-T[i]-1)) ++T[i]; if (T[i]+i > T[g]+g) g=i; } LL result = 0; Incomplete incomplete; FOR(i,4*interval_length,5*interval_length) { if (T[i] >= 4*interval_length) { if (incomplete.first == -1) incomplete.first = i + start; else if (incomplete.second == -1) incomplete.second = i + start; incomplete.last = i + start; } else { result += (T[i]+1)/2; //printf("(1)[%d %d]\n", i+start, T[i]); } } int master = ME, nodes_per_interval = 1; while (interval_length < 2*ORIGINAL_LENGTH) { interval_length <<= 1; nodes_per_interval <<= 1; master -= master % nodes_per_interval; int previous_master = master + nodes_per_interval / 2; if (ME == previous_master) { send(master, incomplete); } if (ME == master) { if (previous_master < NODES) { Incomplete second_part = receive(previous_master); incomplete = merge(incomplete, second_part); } FOR(i,1,nodes_per_interval) if (ME + i < NODES) send(ME+i, incomplete); } else { incomplete = receive(master); } if (incomplete.first == -1) continue; assert(master + nodes_per_interval <= NODES); bool need_to_check_single = true; int single_radius = 2*interval_length; if (incomplete.first < incomplete.last) { need_to_check_single = false; int interval_start = interval_length * (master / nodes_per_interval); int interval_end = interval_start + interval_length; int diff = (incomplete.second - incomplete.first); int cyc = 2 * diff; int L = incomplete.first - 2 * interval_length; L += (nodes_per_interval - L%nodes_per_interval) % nodes_per_interval; L += (ME - master); int R = incomplete.last + 2 * interval_length; R -= (R % nodes_per_interval); R += -nodes_per_interval + (ME - master); int startL = L, startR = R; do { L -= nodes_per_interval; } while (L >= interval_start - 4 * interval_length - 5 && get(L + cyc) == get(L)); do { R += nodes_per_interval; } while (R <= interval_end + 4 * interval_length + 5 && get(R - cyc) == get(R)); cyc = -1; // don't use if (ME != master) { PutInt(master, L); PutInt(master, R); Send(master); } if (ME == master) { FOR(i,1,nodes_per_interval) { Receive(ME+i); L = max(L, GetInt(ME+i)); R = min(R, GetInt(ME+i)); } FOR(i,1,nodes_per_interval) { PutInt(ME+i, L); PutInt(ME+i, R); Send(ME+i); } } if (ME != master) { Receive(master); L = GetInt(master); R = GetInt(master); } int new_first = 1e9 + 500; int new_last = -1; for (int i = incomplete.first + (ME - master) * diff; i <= incomplete.last; i += nodes_per_interval * diff) { int radius = min(R - i, i-1 - L); if (radius >= 4 * interval_length || get(i - radius - 1) == get(i + radius)) { new_first = min(new_first, i); new_last = max(new_last, i); if (radius < 4 * interval_length) { need_to_check_single = true; single_radius = max(single_radius, radius); } } else { result += (radius+1)/2; } } if (ME != master) { PutInt(master, new_first); PutInt(master, new_last); PutInt(master, need_to_check_single); PutInt(master, single_radius); Send(master); } if (ME == master) { FOR(i,1,nodes_per_interval) { Receive(ME + i); new_first = min(new_first, GetInt(ME + i)); new_last = max(new_last, GetInt(ME + i)); need_to_check_single = GetInt(ME + i) || need_to_check_single; single_radius = max(single_radius, GetInt(ME+i)); } FOR(i,1,nodes_per_interval) { PutInt(ME+i, new_first); PutInt(ME+i, new_last); PutInt(ME+i, need_to_check_single); PutInt(ME+i, single_radius); Send(ME+i); } } if (ME != master) { Receive(master); new_first = GetInt(master); new_last = GetInt(master); need_to_check_single = GetInt(master) || need_to_check_single; single_radius = max(single_radius, GetInt(master)); } if (new_first > new_last) incomplete = Incomplete(); else { incomplete.first = new_first; incomplete.last = new_last; incomplete.second = (incomplete.first == incomplete.last ? -1 : incomplete.first + diff); } } if (need_to_check_single && incomplete.first != -1 && incomplete.first == incomplete.last) { int c = incomplete.first; int radius = single_radius - (single_radius%nodes_per_interval) - nodes_per_interval + (ME - master); do { radius += nodes_per_interval; } while (radius <= 4 * interval_length + 5 && get(c + radius - 1) == get(c - radius)); if (ME != master) { PutInt(master, radius); Send(master); } else { FOR(i,1,nodes_per_interval) { Receive(ME + i); radius = min(radius, GetInt(ME + i)); } } --radius; if (ME == master) { if (radius < 4 * interval_length) { //printf("(2)[%d %d]\n", c, radius); result += (radius+1)/2; incomplete.first = incomplete.second = incomplete.last = -1; } } } } if (ME > 0) { PutLL(0, result); Send(0); } else { //REP(i, LENGTH) printf("[%d]%c", i, get(i)); printf("\n"); FOR(i,1,NODES) { Receive(i); result += GetLL(i); } printf("%lld\n", result); } } |