跳到正文

题解

题解:P8527 [Ynoi2003] 樋口円香

WorldMachine约 5 分钟
原文
文章信息与版本

文章信息

发布
2025年6月7日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 5 分钟
获赞
4
题目
P8527

版本

  1. v1当前

一眼题。

考虑对 aa 分块,散块暴力加。对于整块,考虑逐块处理,发现这个块对序列 bb 的贡献可以写成卷积的形式。

具体地说,对于左端点为 xx 的块,记录序列 cc,对于包含该整块的操作 (l,r,L)(l,r,L),将 cL−l+xc_{L-l+x} 加 11,最后求出该块和序列 cc 的卷积即可。

复杂度为 O(mB+nB(m+nlog⁡n))\mathcal O\left(mB+\dfrac{n}{B}(m+n\log n)\right),块长为 O(n2log⁡nm+n)\mathcal O\left(\sqrt{\dfrac{n^2\log n}{m}+n}\right) 时有最优复杂度 O(nmlog⁡n+mn)\mathcal O(n\sqrt{m\log n}+m\sqrt n)。

看起来很有常数,但不卡常。

C++
#include <bits/stdc++.h>
using namespace std;
#define il inline
#define IOSIZE (1 << 20)
char buf[IOSIZE], *p1 = buf, *p2 = buf;
#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, IOSIZE, stdin), p1 == p2) ? EOF : *p1++)
il int read() { int x = 0; char c = '%'; while (c < '0' || c > '9') c = gc(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc(); return x; }
typedef complex<double> comp;
const int N = 131075, M = 1e6 + 5, len = 2048;
const double pi = acos(-1);
int n, m, a[N], b[N], blk[N], tot, L[N], R[N], tax[N], c[N];
comp F[N], G[N];
struct opt { int l, r, L; } q[M];
void init(int k) {
	for (int i = 1; i <= 1 << k; i++) tax[i] = tax[i >> 1] >> 1 | ((i & 1) << k - 1);
}
void FFT(int n, comp A[]) {
	for (int i = 1; i < n; i++) if (i < tax[i]) swap(A[i], A[tax[i]]);
	for (int i = 1, j = 2; i < n; i <<= 1, j <<= 1) {
		const comp w(cos(pi / i), sin(pi / i));
		for (int l = 0, r = j - 1; r <= n; l += j, r += j) {
			comp t(1, 0);
			for (int k = l; k < l + i; k++) {
				comp x = A[k] + t * A[k + i], y = A[k] - t * A[k + i];
				A[k] = x, A[k + i] = y, t *= w;
			}
		}
	}
}
int main() {
	n = read();
	for (int i = 1; i <= n; i++) a[i] = read();
	m = read();
	for (int i = 1; i <= n; i++) blk[i] = (i - 1) / len + 1; tot = blk[n];
	for (int i = 1; i <= tot; i++) L[i] = R[i - 1] + 1, R[i] = i * len; R[tot] = n;
	for (int i = 1, l, r, _l; i <= m; i++) {
		l = read(), r = read(), _l = read(), q[i] = {l, r, _l};
		const int bl = blk[l], br = blk[r];
		if (bl == br) {
			for (int j = l; j <= r; j++) b[_l + j - l] += a[j];
			continue;
		}
		for (int j = l; j <= R[bl]; j++) b[_l + j - l] += a[j];
		for (int j = L[br]; j <= r; j++) b[_l + j - l] += a[j];
	}
	int N = 1, k = 0; while (N <= n) N <<= 1, k++; init(k);
	for (int i = 1; i <= tot; i++) {
		for (int j = 1; j <= n; j++) c[j] = 0;
		for (int j = 1; j <= m; j++) {
			if (q[j].l < L[i] && R[i] < q[j].r) c[q[j].L - q[j].l + L[i]]++;
		}
		for (int j = 0; j < N; j++) F[j] = G[j] = 0;
		for (int j = L[i]; j <= R[i]; j++) F[j - L[i]] = a[j];
		for (int j = 1; j <= n; j++) G[j] = c[j];
		FFT(N, F), FFT(N, G);
		for (int j = 0; j < N; j++) F[j] *= G[j];
		FFT(N, F), reverse(F + 1, F + N);
		for (int j = 1; j <= n; j++) b[j] += int(F[j].real() / N + 0.5);
	}
	for (int i = 1; i <= n; i++) cout << b[i] << '\n';
}
原文