跳到正文

题解

题解 P5309 [Ynoi2011]初始化

Ryo_Yamada约 6 分钟
原文
文章信息与版本

文章信息

发布
2020年10月12日
收录
2026年2月10日
更新
2026年11月8日
篇幅
约 6 分钟
题目
P5309

版本

  1. v2当前
  2. v1

知识点:

  • 分块
  • 前缀和
  • 根号分治
  • 卡常小技巧

给定一个区间,维护区间和 ⁣ ⁣mod  109+7\!\!\mod 10^9+7,下标为 y, y+x, y+2x,…,y+kxy,\,y+x,\,y+2x,\dots,y+kx 的数加 zz。

对于修改:

  1. 如果 x≥Fx \ge F,直接每次跳 xx,分块维护。
  2. 如果 x<Fx < F,我们可以前缀和维护数组 Qi,jQ_{i,j},表示跳 ii 个,从 jj 开始已经加了几次。

对于统计区间和:

  1. 对于两边不完整块,分块直接暴力统计即可。
  2. 对于每块统计,用 Qi,jQ_{i,j} 算,统计前缀和即可。

卡常小技巧:

FF 可以取 70→8070 \to 80,实测跑的飞快。

注意到数据范围,1≤n,m≤2×1051 \le n, m \le 2 \times 10^5,0≤ai,z≤109+70 \le a_i, z \le 10^9+7,并不需要每次修改都取模,开 LL 是可以存的下的,注意输出时取模就好了,可以省去大量的取模运算时间。

于是就一发最优解了。

Code\text{Code}:

C++
#include <iostream>
#include <cstdio>
#include <cmath>
#define ll long long
#define il inline
#define rg register

namespace IO {
    const int SIZE = (1 << 20) + 1;
    char ibuf[SIZE], *iS, *iT, obuf[SIZE],*oS = obuf, *oT = obuf + SIZE - 1;
    char _st[55];
    int _qr = 0;
    inline char gc() {
        return (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++) : *iS++);
    }
    inline void qread() {}
    template<class T1, class ...T2>
    inline void qread(T1 &IEE, T2&... ls) {
        register T1 __ = 0, ___ = 1;
        register char ch;
        while(!isdigit(ch = gc())) ___ = (ch == '-') ? -___ : ___;
        do {
            __ = (__ << 1) + (__ << 3) + (ch ^ 48);
        }while(isdigit(ch = gc()));
        __ *= ___;
        IEE = __;
        qread(ls...);
        return ;
    }
    inline void flush() {
        fwrite(obuf, 1, oS - obuf, stdout);
        oS = obuf;
        return ;
    }
    inline void putc_(char _x) {
        *oS++ = _x;
        if(oS == oT) flush();
    }
    inline void qwrite() {}
    template<class T1, class ...T2>
    inline void qwrite(T1 IEE, T2... ls) {
        if(!IEE) putc_('0');
        if(IEE < 0) putc_('-'), IEE = -IEE;
        while(IEE) _st[++_qr] = IEE % 10 + '0', IEE /= 10;
        while(_qr) putc_(_st[_qr--]);
        qwrite(ls...);
        return ;
    }
    struct Flusher_{~Flusher_(){flush();}}io_flusher;
}
//快读来自@SPFA(uid=177999)。
using namespace IO;
using namespace std;

const int mod = 1e9 + 7;
const int N = 2e5 + 5;
const int M = 205;

int n, m, blocksize, Faster = 80;
int blocknum[N];
ll a[N], sum[M << 3], Query[M][M];

int main() {
	qread(n, m);
	blocksize = sqrt(n);
	for(rg int i = 1; i <= n; ++i) blocknum[i] = i / blocksize;
	for(rg int i = 1; i <= n; ++i) qread(a[i]), sum[blocknum[i]] += a[i];
	while(m--) {
		int opt;
		qread(opt);
		if(opt == 1) {
			int x, y;
			ll z;
			qread(x, y, z);
			if(x >= Faster) for(rg int i = y; i <= n; i += x) a[i] += z, sum[blocknum[i]] += z;
			else {
				y %= x;
				for(rg int i = y; i < x; ++i) Query[x][i] += z;
			}
		}
		else {
			int x, y;
			ll ans = 0;
			qread(x, y);
			if(blocknum[x] == blocknum[y]) for(int i = x; i <= y; ++i) ans += a[i];
			else {
				for(rg int i = x; i < (blocknum[x] + 1) * blocksize; ++i) ans += a[i];
				for(rg int i = blocknum[x] + 1; i < blocknum[y]; ++i) ans += sum[i];
				for(rg int i = blocknum[y] * blocksize; i <= y; ++i) ans += a[i];
			}
			for(rg int i = 1; i <= Faster; ++i) {
				if(x / i == y / i) ans += (Query[i][y % i] - Query[i][x % i - 1]);
				else ans += Query[i][i - 1] * (y / i - x / i) - Query[i][x % i - 1] + Query[i][y % i];
			}
			printf("%lld\n", ans % mod);
		}
	}
	return 0;
}
原文
存档评论10 条抓取于