跳到正文

题解

题解:P13665 「TPOI-5D」「僕は…」

pystraf11约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 11 分钟
题目
P13665

版本

  1. v2当前
  2. v1

Description

给定 nn 个字符串 S=(S1,S2,⋯ ,Sn)S=(S_1,S_2,\cdots,S_n),由小写字母组成。

设 f(s,t)f(s,t) 表示 ss 在 tt 中的出现次数。

给你 qq 次查询 (l,r,L,R)(l,r,L,R),你要求出 ∑i=lr∑j=LRf(Si,Sj)\sum_{i=l}^r \sum_{j=L}^R f(S_i,S_j)。

Limitations

1≤n,q≤2×1051\le n,q\le 2\times 10^5

∑∣Si∣≤2×105\sum |S_i|\le 2\times 10^5

1≤l≤r≤n1\le l\le r\le n

1≤L≤R≤n1\le L\le R\le n

1.5s,128MB1.5\text{s},128\text{MB}

Solution

视 n,q,∑∣Si∣n,q,\sum{|S_i|} 同阶。

如果你会 CF547E 和 CF587F 那么这题就很简单了。

考虑对 l,rl,r 这维分块,拆开后,问题变为:

  • 整块对 SL∼SRS_L\sim S_R :拆成前缀,总共 O(n)O(n) 次查询,那么直接用 CF587F 的大串做法,预处理前缀和即可。
  • 散块对 SL∼SRS_L\sim S_R:由于 r−l+1≤nr-l+1\le \sqrt n,l,rl,r 这维可以化为单点,套用 CF547E 即可,因为总共有 O(n)O(n) 次修改,O(nn)O(n\sqrt n) 次查询,要用 O(n)−O(1)O(\sqrt{n})-O(1) 分块来平衡复杂度。

然后做完了,记得要离线处理,时间复杂度 O(nn)O(n\sqrt n),块长建议取 256256。

code
C++
#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::read;
using fastio::write;

constexpr int cap = 2e7 + 10, N = 2.7e5 + 10, B = 256;
int pool[cap], *ptr = pool;

template<class T>
inline T* alloc(int n) {
	int siz = sizeof(T) / sizeof(int) * n;
	T* res = (T*)ptr; ptr += siz;
	return res;
}

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) {}
};

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++) {
		        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 = 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;
	}
};

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;
}
原文