题解 P5309 [Ynoi2011]初始化

· · 题解

知识点:

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

对于修改:

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

对于统计区间和:

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

卡常小技巧:

注意到数据范围,$1 \le n, m \le 2 \times 10^5$,$0 \le a_i, z \le 10^9+7$,**并不需要每次修改都取模**,开 LL 是可以存的下的,注意输出时取模就好了,可以省去大量的取模运算时间。 于是就一发最优解了。 $\text{Code}$: ```cpp #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; } ```