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
#include <vector>
#include <list>
#include <unordered_map>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>

#include "palindromy.h"
#include "message.h"

#define PB          push_back
#define ST          first
#define ND          second

using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using VII = vector<PII>;
using LL = long long int;
using ULL = unsigned long long int;

const int MAX_SINGLE = 40 * 1000 * 1000;

int PID;
int Nodes;
int N;

const int MAXN = MAX_SINGLE;
int odd[MAXN], even[MAXN];
char s[MAXN];
unordered_map<int, int> extra;

void SolveSingle() {
  int l, r;
  LL ans = 0;

  for (int i = 0; i < N; ++i) {
    s[i] = GetLetter(i);
  }

  // Odd case
  l = r = -1;
  for (int i = 0; i < N; i++) {
    int cur = 1;
    if (i < r) {
      cur = min(r - i + 1, odd[l + r - i]);
    }
    while (i + cur < N && i - cur >= 0 && s[i - cur] == s[i + cur]) {
      cur++;
    }
    odd[i] = cur;
    ans += cur;
    if (i + cur - 1 > r) {
      l = i - cur + 1; 
      r = i + cur - 1;
    }
  }

  // Even case 
  l = r = -1;
  for (int i = 0; i < N; i++) {
    int cur = 0;
    if (i < r) {
      cur = min(r - i + 1, even[l + r - i + 1]);
    }
    while (i + cur < N && i - 1 - cur >= 0 && s[i - 1 - cur] == s[i + cur]) {
      cur++;
    }
    even[i] = cur;
    ans += cur;
    if (i + cur - 1 > r) {
      l = i - cur;
      r = i + cur - 1;
    }
  }
  printf("%lld\n", ans);
}

int min_pos, max_pos;
int block;

char Letter(int i) {
  if (i >= min_pos && i <= max_pos) {
    return s[i-min_pos];
  } else {
    return GetLetter(i);
  }
}

const int message_block = 300;

int Tab(int i, int is_odd) {
  if (i >= min_pos && i <= max_pos) {
    if (is_odd) {
      return odd[i - min_pos];
    } else {
      return even[i - min_pos];
    }
  } else {
    auto it = extra.find(i);
    if (it != extra.end()) {
      return it->ND;
    }
    int who = i / block;
    int count = min(message_block, i % block + 1);
    PutInt(who, is_odd);
    PutInt(who, i);
    Send(who);
    Receive(who);
    for (int x = 0; x < count; ++x) {
      extra.insert({i - x, GetInt(who)});
    }
    return extra[i];
  }
}

void SolveMulti() {
  block = N / Nodes;
  min_pos = PID * (N / Nodes);
  max_pos = min(N, (PID + 1) * (N / Nodes) - 1);
  for (int i = 0; i <= max_pos - min_pos; ++i) {
    s[i] = GetLetter(i + min_pos);
  }
  int l_odd, r_odd, l_even, r_even;
  LL ans = 0;

  if (PID == 0) {
    l_odd = r_odd = -1;
    l_even = r_even = -1;
  } else {
    Receive(PID - 1);
    l_odd = GetInt(PID - 1);
    r_odd = GetInt(PID - 1);
    l_even = GetInt(PID - 1);
    r_even = GetInt(PID - 1);
  }
  // Odd case
  for (int i = min_pos; i <= max_pos; i++) {
    int cur = 1;
    if (i < r_odd) {
      cur = min(r_odd - i + 1, Tab(l_odd + r_odd - i, 1));
    }
    while (i + cur < N && i - cur >= 0 &&
           Letter(i - cur) == Letter(i + cur)) {
      cur++;
    }
    odd[i - min_pos] = cur;
    ans += cur;
    if (i + cur - 1 > r_odd) {
      l_odd = i - cur + 1; 
      r_odd = i + cur - 1;
    }
  }

  extra.clear();
  // Even case 
  for (int i = min_pos; i <= max_pos; i++) {
    int cur = 0;
    if (i < r_even) {
      cur = min(r_even - i + 1, Tab(l_even + r_even - i + 1, 0));
    }
    while (i + cur < N && i - 1 - cur >= 0 &&
           Letter(i - 1 - cur) == Letter(i + cur)) {
      cur++;
    }
    even[i - min_pos] = cur;
    ans += cur;
    if (i + cur - 1 > r_even) {
      l_even = i - cur;
      r_even = i + cur - 1;
    }
  }
  if (PID != Nodes - 1) {
    PutInt(PID + 1, l_odd);
    PutInt(PID + 1, r_odd);
    PutInt(PID + 1, l_even);
    PutInt(PID + 1, r_even);
    Send(PID + 1);
  }

  // I finished.
  for (int i = 0; i < PID; ++i) {
    PutInt(i, -1);
    Send(i);
  }
  // Respond.
  int others_left = Nodes - PID - 1;
  while (others_left > 0) {
    int who = Receive(-1);
    int what = GetInt(who);
    if (what == -1) {
      --others_left;
    } else {
      // Answer.
      int pos = GetInt(who) - min_pos;
      int count = min(message_block, pos + 1);
      for (int x = 0; x < count; ++x) {
        PutInt(who, what ? odd[pos - x] : even[pos - x]);
      }
      Send(who);
    }
  }

  if (PID == Nodes - 1) {
    for (int i = 0; i < Nodes - 1; ++i) {
      Receive(i);
      ans += GetLL(i);
    }
    printf("%lld\n", ans);
  } else {
    PutLL(Nodes - 1, ans);
    Send(Nodes - 1);
  }
}

int main() {
  PID = MyNodeId();
  Nodes = NumberOfNodes();
  N = GetLength();
  if (N <= MAX_SINGLE || Nodes == 1) {
    if (PID == 0) {
      SolveSingle();
    }
    return 0;
  }
  SolveMulti();
  return 0;
}