P4458 [BZOJ2018]链上二次求和 题解
前言:
本初二蒟蒻对数论一窍不通,本题解推导过程都是无脑数学式,似乎比别的题解长得多。
这是本蒟蒻 AC 的第一道黑题,也是本蒟蒻的第一篇题解,希望大家对文中的不足提出建议吧。
(话说我怎么第一篇题解就要写辣么多数学柿子呀)
题目大意:
给一条长度为
- 将结点
u 到v 之间的所有结点(包括u 和v )的点权都+d 。 - 求出所有长度在
[l,r] 范围内的链的二次求和。从u 到v 和从v 到u 被视作同一条链。
数据范围:
题解:
1. 写在最前面:一定要注意!
读完这道题目,大家可能会直呼:这不就是个序列吗!为什么出题人非要说成链呢?
因为他要埋伏陷害你啊!
如果仔细看一下冗长的数据范围,你会发现,好像只说了
则有:
(现在应该知道为什么前面要分类
手推一下,得:
这一段对答案的贡献(为了节省篇幅,过程略):
于是可以在
这一部分的代码:
LL s1 = query(1, 1, 1, n, 1, x);
LL s2 = query(2, 1, 1, n, 1, x);
ret = (ret + ((x << 1 | 1) * s1 % mod - s2 + mod) % mod * P % mod) % mod;
则有:
显然,
所以愉快地拆掉
这一段对答案的贡献:
调用
就搞定啦。
这一部分的代码:
if(x < n - x + 1) ret = (ret + query(0, 1, 1, n, x + 1, n - x + 1) * x % mod * (x + 1) % mod * P % mod) % mod;
注意先特判一下
3. i \in [n-x+2,n]
则有:
这个就比较麻烦了,要进一步分类讨论。
(1)
令:
则:
这一段对答案的贡献:
(2)
令:
这一段对答案的贡献:
放一下整个
if(n - x + 2 <= n) {
LL X = n + 1, Y = n + 2;
LL s0 = query(0, 1, 1, n, n - x + 2, n);
LL s1 = query(1, 1, 1, n, n - x + 2, n);
LL s2 = query(2, 1, 1, n, n - x + 2, n);
ret = (ret + (X * Y % mod * s0 % mod - (X + Y) * s1 % mod + s2 + mod) % mod * P % mod) % mod;
LL E = n + 1, F = x - n - 1;
ret = (ret + (E * F % mod * s0 % mod + (E - F) * s1 % mod - s2 + mod * 2) % mod) % mod; //这里必须要+mod*2,因为F=x-n-1是负数,导致前面E*F%mod也会产生一个负数,所以要+mod*2扳回来。如果+mod,就只剩25分了!
}
话说我们终于把
二. x>n-x+1
1. i \in [1,n-x+1]
这一段对答案的贡献:
这一段的代码放一下:
LL s1 = query(1, 1, 1, n, 1, n - x + 1);
LL s2 = query(2, 1, 1, n, 1, n - x + 1);
ret = (ret + ((x << 1 | 1) * s1 % mod - s2 + mod) % mod * P % mod) % mod;
2. i \in [n-x+2,x]
则有:
(1)
则有:
令:
这一段对答案的贡献:
(2)
则有:
令:
(话说我令的都是些啥字母)
则有:
这一段对答案的贡献:
整个
LL A = n - x + 1, B = n + x + 2;
LL s0 = query(0, 1, 1, n, n - x + 2, x);
LL s1 = query(1, 1, 1, n, n - x + 2, x);
LL s2 = query(2, 1, 1, n, n - x + 2, x);
ret = (ret + (A * B % mod * s0 % mod - 2 * A * s1 % mod + mod) % mod * P % mod) % mod;
LL S = n + 1, T = x - n - 1;
ret = (ret + (S * T % mod * s0 % mod + (S - T) * s1 % mod - s2 + mod * 2) % mod) % mod; //同样注意+mod*2。
3. i \in [x+1,n]
则有:
继续分类讨论:
(1)
则有:
令:
这一部分对答案的贡献:
(2)
令:
这一段对答案的贡献:
把整个
if(x < n) {
LL M = n + 1, N = n + 2;
LL s0 = query(0, 1, 1, n, x + 1, n);
LL s1 = query(1, 1, 1, n, x + 1, n);
LL s2 = query(2, 1, 1, n, x + 1, n);
ret = (ret + (M * N % mod * s0 % mod - (M + N) * s1 % mod + s2 + mod) % mod * P % mod) % mod;
LL G = x - n - 1, H = n + 1;
ret = (ret + (G * H % mod * s0 % mod + (H - G) * s1 % mod - s2 + mod * 2) % mod) % mod; //又是恶心的+mod*2。
}
终于推完式子了!
简单讲一下代码中
由于答案要对
写个快速幂就行了。
于是代码就出来了:
#include <cstdio>
using namespace std;
#define MAXN 200010
#define mod 1000000007
#define LL long long
LL sp[3][MAXN << 2], lazy[MAXN << 2], sum[3][MAXN];
LL n, m, P;
inline LL pow(LL x, LL y) {
LL ret = 1;
while(y) {
if(y & 1) ret = ret * x % mod;
x = x * x % mod;
y >>= 1;
}
return ret;
}
inline void swap(LL &x, LL &y) {
x ^= y ^= x ^= y;
}
inline void up(LL p) {
for(LL i = 0; i < 3; i++)
sp[i][p] = (sp[i][p << 1] + sp[i][p << 1 | 1]) % mod;
}
inline void build(LL p, LL l, LL r) {
if(l == r) {
scanf("%lld", &sp[0][p]);
sp[1][p] = sp[0][p] * l % mod;
sp[2][p] = sp[0][p] * l % mod * l % mod;
return;
}
LL m = (l + r) >> 1;
build(p << 1, l, m);
build(p << 1 | 1, m + 1, r);
up(p);
}
inline void down(LL p, LL l, LL r) {
if(lazy[p]) {
lazy[p << 1] = (lazy[p << 1] + lazy[p]) % mod;
lazy[p << 1 | 1] = (lazy[p << 1 | 1] + lazy[p]) % mod;
LL m = (l + r) >> 1;
for(LL i = 0; i < 3; i++) {
sp[i][p << 1] = (sp[i][p << 1] + (sum[i][m] - sum[i][l - 1] + mod) % mod * lazy[p] % mod) % mod;
sp[i][p << 1 | 1] = (sp[i][p << 1 | 1] + (sum[i][r] - sum[i][m] + mod) % mod * lazy[p] % mod) % mod;
}
lazy[p] = 0;
}
}
inline void update(LL p, LL l, LL r, LL x, LL y, LL d) {
if(x <= l && r <= y) {
lazy[p] = (lazy[p] + d) % mod;
for(LL i = 0; i < 3; i++)
sp[i][p] = (sp[i][p] + (sum[i][r] - sum[i][l - 1] + mod) % mod * d % mod) % mod;
return;
}
down(p, l, r);
LL m = (l + r) >> 1;
if(x <= m) update(p << 1, l, m, x, y, d);
if(y > m) update(p << 1 | 1, m + 1, r, x, y, d);
up(p);
}
inline LL query(LL id, LL p, LL l, LL r, LL ql, LL qr) {
if(ql <= l && r <= qr) return sp[id][p];
down(p, l, r);
LL m = (l + r) >> 1, ret = 0;
if(ql <= m) ret = (ret + query(id, p << 1, l, m, ql, qr)) % mod;
if(qr > m) ret = (ret + query(id, p << 1 | 1, m + 1, r, ql, qr)) % mod;
return ret;
}
inline LL solve(LL x) {
if(x == 0) return 0;
LL ret = 0;
if(x <= n - x + 1) {
LL s1 = query(1, 1, 1, n, 1, x);
LL s2 = query(2, 1, 1, n, 1, x);
ret = (ret + ((x << 1 | 1) * s1 % mod - s2 + mod) % mod * P % mod) % mod;
if(x < n - x + 1) ret = (ret + query(0, 1, 1, n, x + 1, n - x + 1) * x % mod * (x + 1) % mod * P % mod) % mod;
if(n - x + 2 <= n) {
LL X = n + 1, Y = n + 2;
LL s0 = query(0, 1, 1, n, n - x + 2, n);
LL s1 = query(1, 1, 1, n, n - x + 2, n);
LL s2 = query(2, 1, 1, n, n - x + 2, n);
ret = (ret + (X * Y % mod * s0 % mod - (X + Y) * s1 % mod + s2 + mod) % mod * P % mod) % mod;
LL E = n + 1, F = x - n - 1;
ret = (ret + (E * F % mod * s0 % mod + (E - F) * s1 % mod - s2 + mod * 2) % mod) % mod;
}
}
else {
LL s1 = query(1, 1, 1, n, 1, n - x + 1);
LL s2 = query(2, 1, 1, n, 1, n - x + 1);
ret = (ret + ((x << 1 | 1) * s1 % mod - s2 + mod) % mod * P % mod) % mod;
LL A = n - x + 1, B = n + x + 2;
LL s0 = query(0, 1, 1, n, n - x + 2, x);
s1 = query(1, 1, 1, n, n - x + 2, x);
s2 = query(2, 1, 1, n, n - x + 2, x);
ret = (ret + (A * B % mod * s0 % mod - 2 * A * s1 % mod + mod) % mod * P % mod) % mod;
LL S = n + 1, T = x - n - 1;
ret = (ret + (S * T % mod * s0 % mod + (S - T) * s1 % mod - s2 + mod * 2) % mod) % mod;
if(x < n) {
LL M = n + 1, N = n + 2;
LL s0 = query(0, 1, 1, n, x + 1, n);
LL s1 = query(1, 1, 1, n, x + 1, n);
LL s2 = query(2, 1, 1, n, x + 1, n);
ret = (ret + (M * N % mod * s0 % mod - (M + N) * s1 % mod + s2 + mod) % mod * P % mod) % mod;
LL G = x - n - 1, H = n + 1;
ret = (ret + (G * H % mod * s0 % mod + (H - G) * s1 % mod - s2 + mod * 2) % mod) % mod;
}
}
return ret;
}
int main() {
P = pow(2, mod - 2);
scanf("%lld %lld", &n, &m);
for(LL i = 1; i <= n; i++) {
sum[0][i] = (sum[0][i - 1] + 1) % mod;
sum[1][i] = (sum[1][i - 1] + i) % mod;
sum[2][i] = (sum[2][i - 1] + i * i) % mod;
}
build(1, 1, n);
while(m--) {
LL opt, l, r, x;
scanf("%lld", &opt);
if(opt == 1) {
scanf("%lld %lld %lld", &l, &r, &x);
if(l > r) swap(l, r);
update(1, 1, n, l, r, x);
}
else {
scanf("%lld %lld", &l, &r);
printf("%lld\n", (solve(r) - solve(l - 1) + mod) % mod);
}
}
return 0;
}
别 Ctrl+C/V 啊!这个是
问题出在哪里了呢?
一方面,对同一区间在三棵线段树中分别求和,很浪费时间;另一方面,虽然古话说不取模见祖宗,但是这样疯狂取模未免过分了点?
比如说,以下是从
sp[i][p << 1] = (sp[i][p << 1] + (sum[i][m] - sum[i][l - 1] + mod) % mod * lazy[p] % mod) % mod;
可以改成:
sp[i][p << 1] = (sp[i][p << 1] + (sum[i][m] - sum[i][l - 1] + mod) * lazy[p]) % mod;
一下子干掉了
对于问题
修改后的代码如下(AC,最大测试点约
#include <cstdio>
using namespace std;
#define MAXN 200010
#define mod 1000000007
#define LL long long
struct ans {
LL s0, s1, s2;
};
LL sp[3][MAXN << 2], lazy[MAXN << 2], sum[3][MAXN];
LL n, m, P;
inline LL pow(LL x, LL y) {
LL ret = 1;
while(y) {
if(y & 1) ret = ret * x % mod;
x = x * x % mod;
y >>= 1;
}
return ret;
}
inline void swap(LL &x, LL &y) {
x ^= y ^= x ^= y;
}
inline void up(LL p) {
for(LL i = 0; i < 3; i++)
sp[i][p] = (sp[i][p << 1] + sp[i][p << 1 | 1]) % mod;
}
inline void build(LL p, LL l, LL r) {
if(l == r) {
scanf("%lld", &sp[0][p]);
sp[1][p] = sp[0][p] * l % mod;
sp[2][p] = sp[0][p] * l % mod * l % mod;
return;
}
LL m = (l + r) >> 1;
build(p << 1, l, m);
build(p << 1 | 1, m + 1, r);
up(p);
}
inline void down(LL p, LL l, LL r) {
if(lazy[p]) {
lazy[p << 1] = (lazy[p << 1] + lazy[p]) % mod;
lazy[p << 1 | 1] = (lazy[p << 1 | 1] + lazy[p]) % mod;
LL m = (l + r) >> 1;
for(LL i = 0; i < 3; i++) {
sp[i][p << 1] = (sp[i][p << 1] + (sum[i][m] - sum[i][l - 1] + mod) * lazy[p]) % mod;
sp[i][p << 1 | 1] = (sp[i][p << 1 | 1] + (sum[i][r] - sum[i][m] + mod) * lazy[p]) % mod;
}
lazy[p] = 0;
}
}
inline void update(LL p, LL l, LL r, LL x, LL y, LL d) {
if(x <= l && r <= y) {
lazy[p] = (lazy[p] + d) % mod;
for(LL i = 0; i < 3; i++)
sp[i][p] = (sp[i][p] + (sum[i][r] - sum[i][l - 1] + mod) * d) % mod;
return;
}
down(p, l, r);
LL m = (l + r) >> 1;
if(x <= m) update(p << 1, l, m, x, y, d);
if(y > m) update(p << 1 | 1, m + 1, r, x, y, d);
up(p);
}
inline ans query(LL p, LL l, LL r, LL ql, LL qr) {
if(ql <= l && r <= qr) return (ans){sp[0][p], sp[1][p], sp[2][p]};
down(p, l, r);
LL m = (l + r) >> 1, s0 = 0, s1 = 0, s2 = 0;
if(ql <= m) {
ans now = query(p << 1, l, m, ql, qr);
s0 += now.s0;
s1 += now.s1;
s2 += now.s2;
}
if(qr > m) {
ans now = query(p << 1 | 1, m + 1, r, ql, qr);
s0 += now.s0;
s1 += now.s1;
s2 += now.s2;
}
return (ans){s0 % mod, s1 % mod, s2 % mod};
}
inline LL solve(LL x) {
if(x == 0) return 0;
LL ret = 0;
if(x <= n - x + 1) {
ans t1 = query(1, 1, n, 1, x);
LL s1 = t1.s1, s2 = t1.s2;
ret = (ret + ((x << 1 | 1) * s1 % mod - s2 + mod) * P) % mod;
if(x < n - x + 1) {
ans t2 = query(1, 1, n, x + 1, n - x + 1);
ret = (ret + t2.s0 * x % mod * (x + 1) % mod * P) % mod;
}
if(n - x + 2 <= n) {
LL X = n + 1, Y = n + 2;
ans t3 = query(1, 1, n, n - x + 2, n);
LL s0 = t3.s0, s1 = t3.s1, s2 = t3.s2;
ret = (ret + (X * Y % mod * s0 % mod - (X + Y) * s1 % mod + s2 + mod) % mod * P) % mod;
LL E = n + 1, F = x - n - 1;
ret = (ret + E * F % mod * s0 % mod + (E - F) * s1 % mod - s2 + mod * 2) % mod;
}
}
else {
ans t4 = query(1, 1, n, 1, n - x + 1);
LL s1 = t4.s1, s2 = t4.s2;
ret = (ret + ((x << 1 | 1) * s1 % mod - s2 + mod) % mod * P) % mod;
LL A = n - x + 1, B = n + x + 2;
ans t5 = query(1, 1, n, n - x + 2, x);
LL s0 = t5.s0;
s1 = t5.s1; s2 = t5.s2;
ret = (ret + (A * B % mod * s0 % mod - 2 * A * s1 % mod + mod) % mod * P) % mod;
LL S = n + 1, T = x - n - 1;
ret = (ret + S * T % mod * s0 % mod + (S - T) * s1 % mod - s2 + mod * 2) % mod;
if(x < n) {
LL M = n + 1, N = n + 2;
ans t6 = query(1, 1, n, x + 1, n);
LL s0 = t6.s0, s1 = t6.s1, s2 = t6.s2;
ret = (ret + (M * N % mod * s0 % mod - (M + N) * s1 % mod + s2 + mod) % mod * P) % mod;
LL G = x - n - 1, H = n + 1;
ret = (ret + G * H % mod * s0 % mod + (H - G) * s1 % mod - s2 + mod * 2) % mod;
}
}
return ret;
}
int main() {
P = pow(2, mod - 2);
scanf("%lld %lld", &n, &m);
for(LL i = 1; i <= n; i++) {
sum[0][i] = (sum[0][i - 1] + 1) % mod;
sum[1][i] = (sum[1][i - 1] + i) % mod;
sum[2][i] = (sum[2][i - 1] + i * i) % mod;
}
build(1, 1, n);
while(m--) {
LL opt, l, r, x;
scanf("%lld", &opt);
if(opt == 1) {
scanf("%lld %lld %lld", &l, &r, &x);
if(l > r) swap(l, r);
update(1, 1, n, l, r, x);
}
else {
scanf("%lld %lld", &l, &r);
printf("%lld\n", (solve(r) - solve(l - 1) + mod) % mod);
}
}
return 0;
}
这下可以放心 Ctrl+C/V 了。
复制粘贴有意思吗?
总结:
- 对于这样的二次求和题,一般从每个数对最后答案的贡献考虑。
- 如果需要对同一个区间询问多次,可以仿照此题,用一个结构体把若干个答案一次算出来。
- 虽说不取模见祖宗,但是进行无意义的取模也会拖慢代码效率!
- 如果没有考虑
u>v 会只剩下25 分,如果没有+mod \times 2 也会只剩下25 分,都没考虑到还剩多少分?
最重要的一句话:代码写错非实弱,细节想假毁一生!
后记:
本蒟蒻花了
(话说为了一道黑题推这么多式子不为过吧)