「僕は…」

· · 题解

Description

给定 n 个字符串 s_1\sim s_n,由小写字母组成,设 \text{occ}(s,t) 表示 s 在 t 中的出现次数。

回答 q 次查询 l_k,r_k,L_k,R_k,求出

\sum_{i\in [l_k,r_k]}\sum_{j\in[L_k,R_k]} \text{occ}(s_i,s_j)

Limitations

1\le n,q,\sum|s_i|\le 2\times 10^5 1\le l_k\le r_k\le n,1\le L_k\le R_k\le n 1.5\text{s},128\text{MB}

Solution

视 n,q,\sum{|s_i|} 同阶,建立所有 s_i 的 ACAM,并对第一维 [l,r] 分块。

对于散块,由于里面只有 O(\sqrt n) 个点,考虑 CF547E 做法,把第二维 [L,R] 差分后扫描线,对每个串 s_i,在 fail 树上单点加 1,然后对于查询,暴力枚举 k\in [l,r],每个 k 的贡献就是 endpos 的子树和。会产生 O(n) 次修改和 O(n\sqrt n) 次查询,用 O(\sqrt n)-O(1) 分块平衡即可。

对于整块,预处理 f_{b,i} 表示前 b 块,第二维为 [1,i] 的答案,直接给用到的 endpos 打 \text{tag} 然后向上传即可,由于上面已经离线,可以逐块处理,复杂度 O(n\sqrt n)。

最终时间复杂度 O(n\sqrt n),空间复杂度 O(n),卡一下常就过了(可以把散块用的 DS 换成迭代分块)。

::::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;
}

::::