版本比较
题解:P13665 「TPOI-5D」「僕は…」
正文
增加 31 行删除 31 行
75 行未改动767778798080818282838485868788888990919293949596979898991001011021021031041051061071081091101101111121131144 行未改动1191201211221231231241251261261271281291301301311321331342 行未改动13713813914014114114214314414414514614714814914915015115215315415515515615715815916016116216216316416516616716816917017117117217317317417517617617717817918018018118218218318418518618718818918919019119219319319419519619619719819920020020120220320420520620620720820921021121121221321421521621721821922022122122222322422522622722822922923023123223323423523623723823923823924024124224324424524624624724824824925025125275 行未改动struct acam { vector<array<int, 26>> son; int *dfn, *rev, *low, *fail, siz, clock; vector<vector<int>> g;
inline acam(int n = 0) { son.reserve(n), siz = 0, new_node(); }
inline int new_node() { son.emplace_back(); son[siz].fill(-1); return siz++; }
inline int insert(const string& S) { int u = 0; for (auto c : S) { int v = c - 'a'; if (son[u][v] == -1) son[u][v] = new_node(); u = son[u][v]; } return u; }
inline void build() { fail = alloc<int>(siz); queue<int> que;
for (int i = 0; i < 26; i++) { if (son[0][i] == -1) son[0][i] = 0; else { fail[son[0][i]] = 0; que.push(son[0][i]); } }
while (!que.empty()) { int u = que.front(); que.pop(); for (int i = 0; i < 26; i++) {4 行未改动 que.push(v); } } }
g.resize(siz); for (int i = 1; i < siz; i++) g[fail[i]].push_back(i);
dfn = alloc<int>(siz), rev = alloc<int>(siz), low = alloc<int>(siz); clock = 0, dfs(0); }
void dfs(int u) { rev[dfn[u] = clock++] = u; for (auto v : g[u]) dfs(v); low[u] = clock - 1;2 行未改动
struct Block { int n, blocks; int *a, *pre;
inline Block() {} inline Block(int _n) : n(_n), blocks((_n + B - 1) / B) { clear(); }
inline void clear() { a = alloc<int>(n); pre = alloc<int>(blocks); }
inline void add(int x, int v) { const int bl = x / B; for (int i = x; i < n && i / B == bl; i++) a[i] += v; for (int i = bl; i < blocks; i++) pre[i] += v; }
inline int ask(int x) { if (x < 0) return 0; const int bl = x / B; if (bl == 0) return a[x]; else return pre[bl - 1] + a[x]; }
inline int ask(int l, int r) { return ask(r) - ask(l - 1); }};
signed main() {/* ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);*/
int n = read<int>(), q = read<int>();
vector<string> s(n); for (int i = 0; i < n; i++) cin >> s[i];
const int blocks = (n + B - 1) / B; int *L = alloc<int>(blocks), *R = alloc<int>(blocks); for (int i = 0; i < blocks; i++) L[i] = i * B, R[i] = min(L[i] + B, n) - 1;
vector<vector<query>> qb(blocks), qs(n);
auto hang = [&](int l, int r, int ql, int qr, int i) { if (l / B == r / B) { qs[qr].emplace_back(l, r, 1, i); if (ql > 0) qs[ql - 1].emplace_back(l, r, -1, i); return; }
int l1 = l / B + 1, r1 = r / B - 1; qb[r1].emplace_back(ql, qr, 1, i); if (l1 > 0) qb[l1 - 1].emplace_back(ql, qr, -1, i);
qs[qr].emplace_back(l, L[l1] - 1, 1, i); if (ql > 0) qs[ql - 1].emplace_back(l, L[l1] - 1, -1, i);
qs[qr].emplace_back(R[r1] + 1, r, 1, i); if (ql > 0) qs[ql - 1].emplace_back(R[r1] + 1, r, -1, i); };
for (int i = 0, l, r, s, t; i < q; i++) { l = read<int>(), r = read<int>(), s = read<int>(), t = read<int>(); l--, r--, s--, t--; hang(l, r, s, t, i); }
acam T(n); int *pos = alloc<int>(n); for (int i = 0; i < n; i++) pos[i] = T.insert(s[i]); T.build();
i64 *sum = alloc<i64>(T.siz), *w = alloc<i64>(n + 1), *ans = alloc<i64>(q); auto ask = [&](const string& S) { int p = 0, ret = 0; for (auto i : S) { p = T.son[p][i - 'a']; ret += sum[p]; } return ret; };
for (int b = 0; b < blocks; b++) { for (int i = 0; i < T.siz; i++) sum[i] = 0; for (int i = 0; i <= R[b]; i++) sum[pos[i]]++; for (int i = 1; i < T.clock; i++) sum[T.rev[i]] += sum[T.fail[T.rev[i]]]; for (int i = 0; i < n; i++) w[i + 1] = w[i] + ask(s[i]); for (auto [ql, qr, qt, id] : qb[b]) ans[id] += qt * (w[qr + 1] - w[ql]); }
Block ds(T.clock); auto upd = [&](const string& S, int k) { int p = 0; for (auto i : S) { p = T.son[p][i - 'a']; ds.add(T.dfn[p], k); } };
for (int i = 0; i < n; i++) { upd(s[i], 1); for (auto [ql, qr, qt, id] : qs[i]) for (int j = ql; j <= qr; j++) ans[id] += qt * ds.ask(T.dfn[pos[j]], T.low[pos[j]]); }
for (int i = 0; i < q; i++) write(ans[i]), putchar_unlocked('\n');
return 0;}```::::