-
Notifications
You must be signed in to change notification settings - Fork 2
/
palindromic_tree.cpp
274 lines (250 loc) · 7.17 KB
/
palindromic_tree.cpp
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
struct Node {
int len;
int link;
vector<int> trans;
bool all_equal;
Node() {
len = 0;
link = 0;
trans.assign(26, -1);
all_equal = true;
}
};
struct Fenwick {
vector<int> t;
int n;
void init (int nn)
{
n = nn;
t.assign (n, 0);
}
int sum (int r)
{
int result = 0;
for (; r >= 0; r = (r & (r+1)) - 1)
result += t[r];
return result;
}
void inc (int i, int delta)
{
for (; i < n; i = (i | (i+1)))
t[i] += delta;
}
int sum (int l, int r)
{
return sum (r) - sum (l-1);
}
};
struct Eertree {
vector<Node> nodes;
vector<int> one_len;
Eertree() {
nodes.push_back(Node());
one_len.assign(26, -1);
}
vector<int> feed_string(const string& s) {
int v = 0;
int n = s.length();
vector<int> state(n);
for (int i = 0; i < s.length(); ++i) {
int c = s[i] - 'a';
bool flag = false;
while (v) {
if (nodes[v].all_equal && s[i] == s[i - 1]) {
if (nodes[v].trans[c] == -1) {
nodes[v].trans[c] = nodes.size();
nodes.push_back(Node());
nodes.back().len = nodes[v].len + 1;
nodes.back().all_equal = true;
nodes.back().link = v;
}
v = nodes[v].trans[c];
flag = true;
break;
}
if (i > nodes[v].len && s[i] == s[i - nodes[v].len - 1]) {
if (nodes[v].trans[c] == -1) {
nodes[v].trans[c] = nodes.size();
nodes.push_back(Node());
nodes.back().len = nodes[v].len + 2;
nodes.back().link = -1;
nodes.back().all_equal = false;
int cur_v = nodes[v].link;
while (cur_v) {
if (nodes[cur_v].trans[c] != -1) {
int cand = nodes[cur_v].trans[c];
if (s[i] == s[i - nodes[cand].len + 1]) {
nodes.back().link = nodes[cur_v].trans[c];
break;
}
}
cur_v = nodes[cur_v].link;
}
if (nodes.back().link == -1) {
if (nodes[cur_v].trans[c] != -1) {
nodes.back().link = nodes[cur_v].trans[c];
} else {
nodes[cur_v].link = 0;
}
}
}
v = nodes[v].trans[c];
flag = true;
break;
}
v = nodes[v].link;
}
if (!flag) {
if (one_len[c] == -1) {
nodes[v].trans[c] = nodes.size();
nodes.push_back(Node());
nodes.back().len = 1;
one_len[c] = nodes[v].trans[c];
nodes.back().all_equal = true;
nodes.back().link = 0;
} else {
nodes[v].trans[c] = one_len[c];
}
v = nodes[v].trans[c];
}
state[i] = v;
}
return state;
}
vector<vector<int>> parent;
vector<vector<int>> g;
vector<int> num;
int have_pals;
vector<int> tin, tout;
int timer;
void dfs(int v) {
tin[v] = timer++;
for (int to : g[v]) {
dfs(to);
}
tout[v] = timer;
}
Fenwick sum_tree;
void clear_stat() {
have_pals = 0;
num.assign(nodes.size(), 0);
sum_tree.init(nodes.size());
}
vector<int> max_up;
void calc_ups() {
parent.assign(17, vector<int>(nodes.size()));
g.resize(nodes.size());
for (int i = 0; i < nodes.size(); ++i) {
parent[0][i] = nodes[i].link;
if (i) {
g[nodes[i].link].push_back(i);
}
}
for (int i = 1; i < parent.size(); ++i) {
for (int j = 0; j < nodes.size(); ++j) {
parent[i][j] = parent[i - 1][parent[i - 1][j]];
}
}
tin.resize(nodes.size());
tout.resize(nodes.size());
timer = 0;
dfs(0);
}
double sum_jumps = 0.0, sum_calc_sum = 0, sum_inc = 0;
void modify_state(int v, int len, int val) {
for (int i = 16; i >= 0; --i) {
if (nodes[parent[i][v]].len >= len) {
v = parent[i][v];
}
}
if (nodes[v].len > len) {
v = parent[0][v];
}
assert(v);
int cur_val = sum_tree.sum(tin[v], tout[v] - 1);
if (cur_val == 0 && cur_val + val > 0) {
++have_pals;
} else if (cur_val > 0 && cur_val + val == 0) {
--have_pals;
}
sum_tree.inc(tin[v], val);
}
int get_res() const {
return have_pals;
}
};
struct Query {
int l, r;
int id;
bool operator < (const Query& ot) const {
if (r != ot.r) {
return r < ot.r;
}
return l < ot.l;
}
};
void solve(bool read) {
read = false;
string s;
cin >> s;
Eertree tree;
auto prefix_states = tree.feed_string(s);
tree.calc_ups();
reverse(all(s));
auto suffix_states = tree.feed_string(s);
reverse(all(s));
reverse(all(suffix_states));
int Q;
if (read) {
cin >> Q;
} else {
Q = 100000;
}
int n = s.length();
int block_size = max((int)(sqrt(n) * 1.5), 1);
int blocks = (n - 1) / block_size + 1;
vector<vector<Query>> q(blocks);
vector<int> stupid_ans(Q);
for (int i = 0; i < Q; ++i) {
Query cur;
if (read) {
cin >> cur.l >> cur.r;
} else {
cur.l = 1 + rand() % n;
cur.r = 1 + rand() % n;
if (cur.l > cur.r) {
swap(cur.l, cur.r);
}
}
--cur.l;
cur.id = i;
q[cur.l / block_size].push_back(cur);
}
vector<int> ans(Q);
for (int block = 0; block < blocks; ++block) {
tree.clear_stat();
sort(all(q[block]));
int left_border = block * block_size;
int cur_r = left_border;
int l = left_border;
for (auto qu : q[block]) {
while (cur_r < qu.r) {
tree.modify_state(prefix_states[cur_r], cur_r + 1 - l, 1);
++cur_r;
}
while (l < qu.l) {
tree.modify_state(suffix_states[l], qu.r - l, -1);
++l;
}
while (l > qu.l) {
--l;
tree.modify_state(suffix_states[l], qu.r - l, 1);
}
ans[qu.id] = tree.get_res();
}
}
li sum_ans = 0;
for (int i = 0; i < Q; ++i) {
cout << ans[i] << "\n";
}
}