「僕は…」
Description
给定
回答
Limitations
Solution
视
对于散块,由于里面只有
对于整块,预处理
最终时间复杂度
::::info[code]
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
using ui128 = unsigned __int128;
using f4 = float;
using f8 = double;
using f16 = long double;
template<class T>
bool chmax(T &a, const T &b){
if(a < b){ a = b; return true; }
return false;
}
template<class T>
bool chmin(T &a, const T &b){
if(a > b){ a = b; return true; }
return false;
}
namespace Fastio {}
using Fastio::qin;
using Fastio::qout;
struct acam {
vector<array<int, 26>> son;
vector<int> dfn, ord, low, fail;
vector<vector<int>> g;
int siz, clock;
inline acam(int n = 0) { siz = 0, son.reserve(n), new_node(); }
inline int new_node() {
array<int, 26> def;
def.fill(-1);
son.push_back(def);
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.resize(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++) {
int v = son[u][i];
if (v == -1) son[u][i] = son[fail[u]][i];
else {
fail[v] = son[fail[u]][i];
que.push(v);
}
}
}
g.resize(siz);
for (int i = 1; i < siz; i++) g[fail[i]].push_back(i);
dfn.resize(siz), ord.resize(siz), low.resize(siz);
clock = 0, dfs(0);
}
void dfs(int u) {
ord[dfn[u] = clock++] = u;
for (auto v : g[u]) dfs(v);
low[u] = clock - 1;
}
};
struct Block {
int n;
vector<int> a1, a2, a3;
inline Block() {}
inline Block(int _n) { n = _n, clear(); }
inline void clear() {
a1.assign(n, 0);
a2.assign((n + (1 << 6) - 1) / (1 << 6), 0);
a3.assign((n + (1 << 12) - 1) / (1 << 12), 0);
}
void add(int x, int v) {
while (x < a1.size() && (x & ((1 << 6) - 1)) != 0) a1[x++] += v;
if (x >= a1.size()) return; else x >>= 6;
while (x < a2.size() && (x & ((1 << 6) - 1)) != 0) a2[x++] += v;
if (x >= a2.size()) return; else x >>= 6;
while (x < a3.size()) a3[x++] += v;
}
int ask(int x) { if (x < 0) return 0; return a1[x] + a2[x >> 6] + a3[x >> 12]; }
int ask(int l, int r) { return ask(r) - ask(l - 1); }
};
struct query {
int l, r, t, id;
inline query() {}
inline query(int l, int r, int t, int id) : l(l), r(r), t(t), id(id) {}
};
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, q;
qin >> n >> q;
vector<string> s(n);
for (int i = 0; i < n; i++) qin >> s[i];
const int B = 256, blocks = (n - 1) / B + 1;
vector<int> L(blocks), R(blocks);
for (int i = 0; i < blocks; i++) L[i] = i * B, R[i] = min(L[i] + B, n) - 1;
vector<vector<query>> cq(blocks), iq(n);
auto hang_cq = [&](int bl, int br, int ql, int qr, int id) {
cq[br].emplace_back(ql, qr, 1, id);
if (bl > 0) cq[bl - 1].emplace_back(ql, qr, -1, id);
};
auto hang_iq = [&](int l, int r, int ql, int qr, int id) {
iq[qr].emplace_back(l, r, 1, id);
if (ql > 0) iq[ql - 1].emplace_back(l, r, -1, id);
};
for (int i = 0, l, r, ql, qr; i < q; i++) {
qin >> l >> r >> ql >> qr;
l--, r--, ql--, qr--;
int bl = l / B, br = r / B;
if (bl == br) { hang_iq(l, r, ql, qr, i); continue; }
if (bl + 1 < br) hang_cq(bl + 1, br - 1, ql, qr, i);
hang_iq(l, R[bl], ql, qr, i), hang_iq(L[br], r, ql, qr, i);
}
acam T(n); vector<int> ep(n);
for (int i = 0; i < n; i++) ep[i] = T.insert(s[i]);
T.build();
vector<i64> tag(T.siz), pre(n + 1), ans(q);
auto ask = [&](const string& S) {
int p = 0, ret = 0;
for (auto i : S) p = T.son[p][i - 'a'], ret += tag[p];
return ret;
};
for (int b = 0; b < blocks; b++) {
tag.assign(T.siz, 0);
for (int i = 0; i <= R[b]; i++) tag[ep[i]]++;
for (int i = 1; i < T.clock; i++) tag[T.ord[i]] += tag[T.fail[T.ord[i]]];
for (int i = 0; i < n; i++) pre[i + 1] = pre[i] + ask(s[i]);
for (auto [l, r, v, id] : cq[b]) ans[id] += v * (pre[r + 1] - pre[l]);
}
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 [l, r, v, id] : iq[i])
for (int j = l; j <= r; j++) ans[id] += v * ds.ask(T.dfn[ep[j]], T.low[ep[j]]);
}
for (int i = 0; i < q; i++) qout << ans[i] << '\n';
return 0;
}
::::