跳到正文

题解

P13354 [GDCPC 2024] 拨打电话

EuphoricStar约 21 分钟
原文
文章信息与版本

文章信息

发布
2026年5月14日
收录
2026年11月7日
更新
2026年11月7日
篇幅
约 21 分钟
题目
P13354

版本

  1. v1当前

首先可以把所有 ai,bi,ci,dia_i, b_i, c_i, d_i 都视为哈希值。然后考虑设叶子 xx 的权值为 (n+1)x+1(n + 1)x + 1,那么我们只要对询问求所有满足条件的叶子的权值和,最后看 ⌊ansn+1⌋\left\lfloor\frac{ans}{n + 1}\right\rfloor 和 ans mod (n+1)ans \bmod (n + 1) 确定个数以及是哪个叶子。

考虑枚举路径 LCA uu,枚举 uu 的轻子树的叶子上的询问,设 fbifb_i 为 ii 到根的所有 bjb_j 的从下到上的拼接,fcifc_i 为 ii 到根的所有 cjc_j 的从上到下的拼接,那么相当于求 dfnvdfn_v 在一个区间内,fcvfc_v 等于某个定值的叶子 vv 的权值和,相当于 O(nlog⁡n)O(n \log n) 次一维数点,可以直接在桶里面二分。

uu 的重子树的叶子上的询问还没有被处理,但是此时有贡献的一定是 uu 的轻子树中的叶子。枚举这个叶子 vv,设 gig_i 为第 ii 个询问的 LCA 可能的最浅的深度(这个可以倍增求出),lil_i 为第 ii 个询问串的长度,此时限制是,询问串以某个 tt 为后缀,且 gi≥depug_i \ge dep_u,且询问点 xx 在 uu 的重子树内,且 ∣fbx∣−∣fbu∣+∣du∣+∣fcv∣−∣fcu∣=li|fb_x| - |fb_u| + |d_u| + |fc_v| - |fc_u| = l_i。相当于是 O(L)O(L) 次询问,O(nlog⁡n)O(n \log n) 个点的二维数点。

设 LL 为输入总串长,时间复杂度 O(nlog⁡2n+Llog⁡n)O(n \log^2 n + L \log n)。

代码
C++
// Problem: P13354 [GDCPC 2024] 拨打电话
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P13354
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

namespace IO {
	const int maxn = 1 << 20;
	
	char ibuf[maxn], *iS, *iT, obuf[maxn], *oS = obuf;

	inline char gc() {
		return (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, maxn, stdin), (iS == iT ? EOF : *iS++) : *iS++);
	}

	template<typename T = int>
	inline T read() {
		char c = gc();
		T x = 0;
		bool f = 0;
		while (c < '0' || c > '9') {
			f |= (c == '-');
			c = gc();
		}
		while (c >= '0' && c <= '9') {
			x = (x << 1) + (x << 3) + (c ^ 48);
			c = gc();
		}
		return f ? ~(x - 1) : x;
	}
	
	inline int reads(char *s) {
		char c = gc();
		int len = 0;
		while (isspace(c)) {
			c = gc();
		}
		while (!isspace(c) && c != EOF) {
			s[len++] = c;
			c = gc();
		}
		s[len] = '\0';
		return len;
	}
	
	inline string reads() {
		char c = gc();
		string s;
		while (isspace(c)) {
			c = gc();
		}
		while (!isspace(c) && c != EOF) {
			s += c;
			c = gc();
		}
		return s;
	}

	inline void flush() {
		fwrite(obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	
	struct Flusher {
		~Flusher() {
			flush();
		}
	} AutoFlush;

	inline void pc(char ch) {
		if (oS == obuf + maxn) {
			flush();
		}
		*oS++ = ch;
	}
	
	inline void write(char *s) {
		for (int i = 0; s[i]; ++i) {
			pc(s[i]);
		}
	}
	
	inline void write(const char *s) {
		for (int i = 0; s[i]; ++i) {
			pc(s[i]);
		}
	}

	template<typename T>
	inline void write(T x) {
		static char stk[64], *tp = stk;
		if (x < 0) {
			x = ~(x - 1);
			pc('-');
		}
		do {
			*tp++ = x % 10;
			x /= 10;
		} while (x);
		while (tp != stk) {
			pc((*--tp) | 48);
		}
	}
	
	template<typename T>
	inline void writesp(T x) {
		write(x);
		pc(' ');
	}
	
	template<typename T>
	inline void writeln(T x) {
		write(x);
		pc('\n');
	}
}

using IO::read;
using IO::reads;
using IO::write;
using IO::pc;
using IO::writesp;
using IO::writeln;

const int maxn = 100100;
const int logn = 18;
const int maxm = 3000100;
const int P = 2000083;
const int base = 131, mod1 = 998244911, mod2 = 1004535869, ib1 = 807740157, ib2 = 207041744;

int n, m, pw1[maxm], pw2[maxm], ipw1[maxm], ipw2[maxm], nt, anc[logn][maxn];
ll ans[maxn];
char s[maxm];
vector<int> G[maxn], vq[maxn];

struct info {
	int x, y, l;
	info(int _x = 0, int _y = 0, int _l = 0) : x(_x), y(_y), l(_l) {}
} a[maxn], b[maxn], c[maxn], d[maxn], fb[maxn], fc[maxn];

inline info operator + (const info &a, const info &b) {
	return info((1ULL * a.x * pw1[b.l] + b.x) % mod1, (1ULL * a.y * pw2[b.l] + b.y) % mod2, a.l + b.l);
}

inline info operator - (const info &a, const info &b) {
	return info((a.x - 1ULL * b.x * pw1[a.l - b.l] % mod1 + mod1) % mod1, (a.y - 1ULL * b.y * pw2[a.l - b.l] % mod2 + mod2) % mod2, a.l - b.l);
}

inline info operator ^ (const info &a, const info &b) {
	return info(1ULL * (a.x - b.x + mod1) * ipw1[b.l] % mod1, 1ULL * (a.y - b.y + mod2) * ipw2[b.l] % mod2, a.l - b.l);
}

inline bool operator == (const info &a, const info &b) {
	return a.x == b.x && a.y == b.y && a.l == b.l;
}

int fa[maxn], sz[maxn], dep[maxn], st[maxn], ed[maxn], tim, rnk[maxn];

int dfs(int u, int f) {
	fa[u] = anc[0][u] = f;
	sz[u] = 1;
	fb[u] = b[u] + fb[f];
	fc[u] = fc[f] + c[u];
	int mx = -1, w = 0;
	for (int v : G[u]) {
		dep[v] = dep[u] + 1;
		sz[u] += dfs(v, u);
		if (sz[v] > mx) {
			w = v;
			mx = sz[v];
		}
	}
	if (w) {
		G[u].erase(find(G[u].begin(), G[u].end(), w));
		G[u].insert(G[u].begin(), w);
	}
	return sz[u];
}

void dfs2(int u) {
	st[u] = ++tim;
	rnk[tim] = u;
	for (int v : G[u]) {
		dfs2(v);
	}
	ed[u] = tim;
}

struct que {
	int x, g;
	vector<info> f;
} qq[maxn];

struct HashTable {
	int hd[P], len, val[P], nxt[P];
	info to[P];
	
	inline void init() {
		for (int i = 1; i <= len; ++i) {
			info &x = to[i];
			int u = ((1LL << 31) * x.x + x.y) % P;
			hd[u] = 0;
			val[i] = nxt[i] = 0;
		}
		len = 0;
	}
	
	inline void add(info x, int y) {
		int u = ((1LL << 31) * x.x + x.y) % P;
		to[++len] = x;
		val[len] = y;
		nxt[len] = hd[u];
		hd[u] = len;
	}
	
	inline int find(info x) {
		int u = ((1LL << 31) * x.x + x.y) % P;
		for (int i = hd[u]; i; i = nxt[i]) {
			if (to[i] == x) {
				return val[i];
			}
		}
		return 0;
	}
} mp;

vector<pll> A[maxn];

struct event {
	int x, y, z;
	ll t;
	event(int _x = 0, int _y = 0, int _z = 0, ll _t = 0) : x(_x), y(_y), z(_z), t(_t) {}
};

vector<event> B[P];
bool vis[P];

namespace BIT {
	ll c[maxn];
	
	inline void update(int x, ll d) {
		for (int i = x; i <= n; i += (i & (-i))) {
			c[i] += d;
		}
	}
	
	inline void update(int l, int r, ll x) {
		update(l, x);
		update(r + 1, -x);
	}
	
	inline void clear(int x) {
		for (int i = x; i <= n; i += (i & (-i))) {
			c[i] = 0;
		}
	}
	
	inline ll query(int x) {
		ll res = 0;
		for (int i = x; i; i -= (i & (-i))) {
			res += c[i];
		}
		return res;
	}
}

void solve() {
	pw1[0] = pw2[0] = ipw1[0] = ipw2[0] = 1;
	for (int i = 1; i < maxm; ++i) {
		pw1[i] = 1ULL * pw1[i - 1] * base % mod1;
		pw2[i] = 1ULL * pw2[i - 1] * base % mod2;
		ipw1[i] = 1ULL * ipw1[i - 1] * ib1 % mod1;
		ipw2[i] = 1ULL * ipw2[i - 1] * ib2 % mod2;
	}
	n = read();
	m = read();
	int rt = 0;
	for (int i = 1; i <= n; ++i) {
		int p = read();
		int o = read();
		int len = reads(s + 1);
		if (p) {
			G[p].pb(i);
		} else {
			rt = i;
		}
		if (o) {
			for (int j = 1; j <= len; ++j) {
				a[i] = a[i] + info(s[j], s[j], 1);
			}
		} else {
			int p1 = 0, p2 = 0;
			for (int j = 1; j <= len; ++j) {
				if (s[j] == '/') {
					(p1 ? p2 : p1) = j;
				}
			}
			for (int j = 1; j < p1; ++j) {
				b[i] = b[i] + info(s[j], s[j], 1);
			}
			for (int j = p1 + 1; j < p2; ++j) {
				c[i] = c[i] + info(s[j], s[j], 1);
			}
			for (int j = p2 + 1; j <= len; ++j) {
				d[i] = d[i] + info(s[j], s[j], 1);
			}
		}
	}
	dfs(rt, 0);
	for (int j = 1; j < logn; ++j) {
		for (int i = 1; i <= n; ++i) {
			anc[j][i] = anc[j - 1][anc[j - 1][i]];
		}
	}
	dfs2(rt);
	for (int i = 1; i <= m; ++i) {
		qq[i].x = read();
		int len = reads(s + 1);
		vector<info>(len + 1).swap(qq[i].f);
		for (int j = 1; j <= len; ++j) {
			qq[i].f[j] = qq[i].f[j - 1] + info(s[j], s[j], 1);
		}
		vq[qq[i].x].pb(i);
		int x = qq[i].x;
		for (int j = logn - 1; ~j; --j) {
			if (anc[j][x]) {
				info t = fb[fa[qq[i].x]] ^ fb[fa[anc[j][x]]];
				if (t.l <= len && qq[i].f[t.l] == t) {
					x = anc[j][x];
				}
			}
		}
		qq[i].g = dep[x] - 1;
	}
	for (int i = 1; i <= n; ++i) {
		if (G[i].empty()) {
			int j = mp.find(fc[fa[i]] + a[i]);
			if (!j) {
				j = ++nt;
				mp.add(fc[fa[i]] + a[i], j);
			}
			A[j].pb(st[i], 1LL * i * (n + 1) + 1);
		}
	}
	for (int i = 1; i <= nt; ++i) {
		sort(A[i].begin(), A[i].end());
		for (int j = 1; j < (int)A[i].size(); ++j) {
			A[i][j].scd += A[i][j - 1].scd;
		}
	}
	for (int u = 1; u <= n; ++u) {
		if ((int)G[u].size() <= 1) {
			continue;
		}
		auto query = [&](int p, int l, int r) -> ll {
			if (l > r) {
				return 0;
			}
			l = lower_bound(A[p].begin(), A[p].end(), pll(l, 0)) - A[p].begin();
			r = upper_bound(A[p].begin(), A[p].end(), pll(r, 9e18)) - A[p].begin() - 1;
			if (l > r) {
				return 0;
			}
			return A[p][r].scd - (l ? A[p][l - 1].scd : 0);
		};
		for (int i = 1; i < (int)G[u].size(); ++i) {
			int v = G[u][i];
			for (int j = st[v]; j <= ed[v]; ++j) {
				int w = rnk[j];
				if (G[w].size()) {
					continue;
				}
				info x = (fb[fa[w]] ^ fb[u]) + d[u];
				for (int k : vq[w]) {
					int len = (int)qq[k].f.size() - 1;
					if (x.l <= len && qq[k].f[x.l] == x) {
						if (fc[u].l + len - x.l > 3000000) {
							continue;
						}
						int p = mp.find(fc[u] + (qq[k].f[len] - qq[k].f[x.l]));
						if (p) {
							ans[k] += query(p, st[u] + 1, st[v] - 1) + query(p, ed[v] + 1, ed[u]);
						}
					}
				}
			}
		}
	}
	mp.init();
	nt = 0;
	for (int u = 1; u <= n; ++u) {
		if ((int)G[u].size() <= 1) {
			continue;
		}
		for (int i = 1; i < (int)G[u].size(); ++i) {
			int v = G[u][i];
			for (int j = st[v]; j <= ed[v]; ++j) {
				int w = rnk[j];
				if (G[w].size()) {
					continue;
				}
				info x = d[u] + (fc[w] - fc[u]) + a[w];
				int p = x.l - fb[u].l;
				x = x + info((p + mod1) % mod1, (p + mod2) % mod2, 1);
				int t = mp.find(x);
				if (!t) {
					t = ++nt;
					mp.add(x, t);
				}
				B[t].pb(dep[u], st[G[u][0]], ed[G[u][0]], 1LL * w * (n + 1) + 1);
			}
		}
	}
	for (int i = 1; i <= m; ++i) {
		int len = (int)qq[i].f.size() - 1, p = fb[qq[i].x].l;
		for (int j = 1; j <= len; ++j) {
			info x = qq[i].f[len] - qq[i].f[j - 1];
			x = x + info((len - p + mod1) % mod1, (len - p + mod2) % mod2, 1);
			int t = mp.find(x);
			if (t) {
				vis[t] = 1;
				B[t].pb(qq[i].g, st[qq[i].x], i);
			}
		}
	}
	for (int i = 1; i <= nt; ++i) {
		if (!vis[i]) {
			continue;
		}
		sort(B[i].begin(), B[i].end(), [&](const event &a, const event &b) {
			return a.x > b.x || (a.x == b.x && a.t > b.t);
		});
		for (event u : B[i]) {
			if (u.t) {
				BIT::update(u.y, u.z, u.t);
			} else {
				ans[u.z] += BIT::query(u.y);
			}
		}
		for (event u : B[i]) {
			if (u.t) {
				BIT::clear(u.y);
				BIT::clear(u.z + 1);
			}
		}
	}
	for (int i = 1; i <= m; ++i) {
		int x = ans[i] % (n + 1);
		if (x == 1) {
			write("1 ");
			writeln(ans[i] / (n + 1));
		} else {
			write("0 ");
			writeln(x);
		}
	}
}

int main() {
	int T = 1;
	// scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}
原文