跳到正文

题解

【题解】P8349

Renshey约 9 分钟
原文
文章信息与版本

文章信息

发布
2022年8月29日
收录
2026年7月16日
更新
2026年11月13日
篇幅
约 9 分钟
获赞
8
题目
P8349

版本

  1. v1当前

题解。

考虑将出现数量 ≤B\le B 与 >B>B 的分开处理。不妨设 cntx≤cntycnt_x \le cnt_y。

  1. cntx≤B,cnty≤Bcnt_x \le B,cnt_y \le B

将两个序列归并在一起后可以 O(B)O(B) 暴力,时间复杂度 O(qB)O(qB)。

  1. cntx>B,cnty>Bcnt_x > B, cnt_y > B

本质不同的询问不会太多,考虑每个 xx 对应的 ∑cnty≤n\sum cnt_y \le n,则 ∑cntx+cnty≤nB×∑cnty≤n2B\sum cnt_x + cnt_y \le \frac{n}{B} \times \sum cnt_y \le \frac{n^2}{B},仍然归并在一起暴力,时间复杂度 O(n2B)O(\frac{n^2}{B})。

  1. cntx≤B,cnty>Bcnt_x \le B, cnt_y > B

考虑离线,对于每个 yy 处理所有 xx 的答案。不难发现对于一组 (x,y)(x,y),yy 中只有 O(B)O(B) 个数是有可能对答案产生贡献的,如果和 xx 中的数间隔过远就不需要考虑。于是可以对于 xx 中的每个位置 pp 分析,如果 xx 只取这一个 pp,那么只需要找到 pp 向前/后第一个 yy 进行匹配。因此对于每个 pp 可以找到一个二元组 (prep,nxtp)(pre_p, nxt_p),接下来考虑相邻的二元组的合并关系,只要两段之间没有间隔一个 yy 就可以拼接,重叠的部分需要额外在两侧都补上。最后对于每一段做一次暴力即可。对于每个 yy 可以 O(n)O(n) 预处理所有信息,然后用栈维护上述过程,时间复杂度 O(n2B+qB)O(\frac{n^2}{B}+qB)。

综上,时间复杂度 O(n2B+qB)O(\frac{n^2}{B}+qB),取 B=nqB = \frac{n}{\sqrt q} 时得到最小值 O(nq)O(n\sqrt{q}),空间复杂度 O(n+q)O(n+q)。

代码

C++
#include <bits/stdc++.h>
#define Getchar() p1 == p2 and (p2 = (p1 = Inf) + fread(Inf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++
#define Putchar(c) p3 == p4 and (fwrite(Ouf, 1, 1 << 21, stdout), p3 = Ouf), *p3++ = c
char Inf[1 << 21], Ouf[1 << 21], *p1, *p2, *p3 = Ouf, *p4 = Ouf + (1 << 21);
inline int read (void)
{
	int x = 0; char c = Getchar(); bool f = (c != '-');
	while (c < '0' or c > '9') c = Getchar(), f &= c != '-';
	while (c >= '0' and c <= '9') x = x * 10 + c - 48, c = Getchar();
	return f ? x : -x;
}
inline void write (long long x)
{
	if (x < 0) Putchar('-'), x = -x;
	if (x >= 10) write(x / 10), x %= 10;
	Putchar(x ^ 48);
}
const long long inf = 1LL << 60;
const int maxn = 300000 + 10;
int n, m, q, B, a[maxn], b[maxn]; long long ans[1 << 20];
int cnt[maxn], pre[maxn], nxt[maxn], pos[maxn], id[maxn];
std::pair<int, int> st[maxn]; int tp;
std::vector<int> g[maxn];
std::vector<std::pair<int, int>> Q[maxn];
std::unordered_map<int, long long> map[maxn];
inline std::vector<int> merge (std::vector<int> A, std::vector<int> B)
{
	std::vector<int> C;
	for (int p = 0, q = 0; p < (int)A.size() or q < (int)B.size(); )
		C.push_back(p < (int)A.size() and (q == (int)B.size() or A[p] < B[q]) ? A[p++] : B[q++]);
	return C;
}
inline long long calc (std::vector<int> c, int x, int y)
{
	static int val[maxn]; static long long sum[maxn], min[maxn << 1];
	long long res = -inf; int m = (int)c.size();
	for (int i = 1; i <= m; i++) val[i] = val[i - 1] + (a[c[i - 1]] == x ? 1 : -1), sum[i] = sum[i - 1] + b[c[i - 1]];
	for (int i = 0; i <= 2 * m; i++) min[i] = i == m ? 0 : inf;
	for (int i = 1; i <= m; i++) res = std::max(res, sum[i] - min[val[i] + m]), min[val[i] + m] = std::min(min[val[i] + m], sum[i]);
	return res;
}
inline long long force (int x, int y)
{
	if (x > y) std::swap(x, y);
	if (map[x].count(y)) return map[x][y];
	return map[x][y] = calc(merge(g[x], g[y]), x, y);
}
signed main ()
{
	n = read(); q = read(); B = n / sqrt(q);
	for (int i = 1; i <= n; i++) g[a[i] = read()].push_back(i);
	for (int i = 1; i <= n; i++) b[i] = read();
	for (int i = 1; i <= q; i++)
	{
		int x = read(), y = read();
		if (g[x].size() > g[y].size()) std::swap(x, y);
		if ((int)g[x].size() > B or (int)g[y].size() <= B) {ans[i] = force(x, y); continue;}
		Q[y].push_back({x, i});
	}
	for (int x = 1; x <= n; x++) if (!Q[x].empty())
	{
		m = 0; nxt[n + 1] = n + 1;
		for (int i = 1; i <= n; i++) cnt[i] = cnt[i - 1] + (a[i] == x);
		for (int i = 1; i <= n; i++) pre[i] = a[i] == x ? i : pre[i - 1];
		for (int i = n; i >= 1; i--) nxt[i] = a[i] == x ? i : nxt[i + 1];
		for (int i = 1; i <= n; i++) if (a[i] == x) pos[id[i] = ++m] = i;
		pos[id[n + 1] = ++m] = n + 1; cnt[n + 1] = cnt[n];
		for (auto [y, k]: Q[x])
		{
			ans[k] = -inf; tp = 0;
			for (int p: g[y])
			{
				std::pair<int, int> w = {pre[p], nxt[p]};
				while (tp and id[st[tp].second] + 1 >= id[w.first])
				{
					if (st[tp].first < w.first) std::swap(st[tp].first, w.first);
					if (st[tp].second > w.second) std::swap(st[tp].second, w.second);
					int r = cnt[st[tp].second] - cnt[st[tp].first - 1]; tp--;
					w.first = pos[std::max(id[w.first] - r, 0)];
					w.second = pos[std::min(id[w.second] + r, m)];
				}
				st[++tp] = w;
			}
			for (int i = 1, l = 0, r = -1; i <= tp; i++, l = r + 1)
			{
				while (r + 1 < (int)g[y].size() and g[y][r + 1] <= st[i].second) r++;
				std::vector<int> A, B;
				for (int j = cnt[st[i].first - 1] + 1; j <= cnt[st[i].second]; j++) A.push_back(pos[j]);
				for (int j = l; j <= r; j++) B.push_back(g[y][j]);
				ans[k] = std::max(ans[k], calc(merge(A, B), x, y));
			}
		}
	}
	for (int i = 1; i <= q; i++) write(ans[i]), Putchar('\n');
	fwrite(Ouf, 1, p3 - Ouf, stdout); fflush(stdout);
	return 0;
}
原文