POJ 3468 A Simple Problem with Integers

做这道题起因是,无意中发现了一个叫 zkw 线段树的东西 QAQ,从下往上遍历挺有意思的,不仅非递归,而且常数很低。这题用线段树就能无脑解决吧,后来从网上发现有树状数组解决的,差分的思想可以学习。

zkw 线段树解法

线段树节点记录三个值:

  1. nc:子树的包含的点数
  2. add:对子树的每个值的加数
  3. sum:子树和,不含其父节点们的加数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <cstdio>

const int MAX_M = (1 << 17) + 10;

int n, m, q;
long long sum[MAX_M << 1], add[MAX_M << 1];
int nc[MAX_M << 1];
char cmd[5];
int a, b, c, l, r, lnc, rnc;
long long res;

int main() {
scanf("%d%d", &n, &q);
// 叶子节点(m 个)需要容纳 n + 2 个点(后来发现容纳 n + 1 个点就行)
for (m = 1; m < n + 1; m <<= 1)
;
for (int i = 1; i <= n; ++i) {
scanf("%d", &a);
sum[m + i] = add[m + i] = a;
nc[m + i] = 1;
}
for (int i = m - 1; i > 0; --i) {
nc[i] = nc[i << 1] + nc[i << 1 ^ 1];
sum[i] = sum[i << 1] + sum[i << 1 ^ 1];
}
while (q--) {
scanf("%s%d%d", cmd, &a, &b);
if (cmd[0] == 'C') {
scanf("%d", &c);
lnc = rnc = 0;
l = m + a - 1, r = m + b + 1;
while (l ^ r ^ 1) {
if (~l & 1) {
add[l ^ 1] += c;
sum[l ^ 1] += nc[l ^ 1] * c;
lnc += nc[l ^ 1];
}
if (r & 1) {
add[r ^ 1] += c;
sum[r ^ 1] += nc[r ^ 1] * c;
rnc += nc[r ^ 1];
}
sum[l >>= 1] += lnc * c;
sum[r >>= 1] += rnc * c;
}
while (l > 1) sum[l >>= 1] += (b - a + 1) * c;
} else {
res = 0;
lnc = rnc = 0;
l = m + a - 1, r = m + b + 1;
while (l ^ r ^ 1) {
if (~l & 1) {
res += sum[l ^ 1];
lnc += nc[l ^ 1];
}
if (r & 1) {
res += sum[r ^ 1];
rnc += nc[r ^ 1];
}
res += lnc * add[l >>= 1] + rnc * add[r >>= 1];
}
while (l > 1) res += (b - a + 1) * add[l >>= 1];
printf("%lld\n", res);
}
}
return 0;
}

运行时间 1266ms

树状数组解法

记原数组为 ai{a_i},令 ci=aiai1c_i = a_i - a_{i-1}a0=0a_0=0),那么有 ai=j=1icja_i = \sum_{j=1}^{i} c_j,原题的对 [l, r] 加 v 操作就变成了改变两个数:

cl+v,cr+1vc_l + v, c_{r + 1} - v

加下来看对 [l, r] 的和查询,方便起见,可以把结果看作 [1, r] 的和减去 [1, l - 1] 的和,即我们只需考虑 [1, x] 的和:

i=1xai=i=1xj=1icj=i=1x(x+1i)ci=(x+1)i=1xcii=1xici\sum_{i=1}^{x} a_i = \sum_{i=1}^{x} \sum_{j=1}^{i} c_j = \sum_{i=1}^{x} (x + 1 - i) * c_i = (x + 1) * \sum_{i=1}^{x} c_i - \sum_{i=1}^{x} i * c_i

于是,我们需要在维护 i=1xci\sum_{i=1}^{x} c_i 的同时维护 i=1xici\sum_{i=1}^{x} i * c_i,代码里 nci=icinc_i = i * c_i

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <cstdio>

const int MAX_N = 100000 + 10;

typedef long long ll;

int n, q;
ll c[MAX_N], nc[MAX_N];
char cmd[5];
int a, b, v;

void add(int x, int m) {
ll nm = (ll)x * m;
while (x <= n) {
c[x] += m;
nc[x] += nm;
x += x & (-x);
}
}

ll get_sum(int x) {
ll sc = 0, snc = 0;
int ox = x;
while (x > 0) {
sc += c[x];
snc += nc[x];
x -= x & (-x);
}
return (ox + 1) * sc - snc;
}

int main() {
scanf("%d%d", &n, &q);
b = 0;
for (int i = 1; i <= n; ++i) {
scanf("%d", &a);
add(i, a - b);
b = a;
}
while (q--) {
scanf("%s%d%d", cmd, &a, &b);
if (cmd[0] == 'C') {
scanf("%d", &v);
add(a, v);
add(b + 1, -v);
} else {
printf("%lld\n", get_sum(b) - get_sum(a - 1));
}
}
return 0;
}

运行时间 969ms

0%