题干:

You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.

解题报告:

       有的同学会说,这不是动态差分数组吗?所以可以树状数组啊,树状数组的单点查询区间更新就是进化版的差分数组啊,是这样说没错,但是你要知道,单点查询是动态的,但是这里是区间查询,所以你需要算前缀和,而前缀和是离线查询的!不能动态

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 100000 + 5;
int n,m;
struct TREE {
	int l,r;
	ll val;
	ll laz;
} tree[4*MAX];
int a[MAX];
void pushup(int cur) {
	tree[cur].val = tree[2*cur].val + tree[2*cur + 1].val;
}
void pushdown(int l,int r,int cur) {
	if(tree[cur].laz == 0) return ;
	int m = (l + r)/2;
	tree[2*cur].val += (m-l+1) * tree[cur].laz;
	tree[2*cur].laz += tree[cur].laz;
	tree[2*cur+1].val += (r-m) * tree[cur].laz;
	tree[2*cur+1].laz += tree[cur].laz;
	tree[cur].laz = 0;
}
void build(int l,int r,int cur) {
	if(l == r) {
		tree[cur].l = tree[cur].r = l;
		tree[cur].val = a[r];
		tree[cur].laz = 0;
		return ;
	}
	int m = (l+r)/2;
	tree[cur].l = l; tree[cur].r = r;
	build(l,m,2*cur);build(m+1,r,2*cur+1);
	pushup(cur);
}
void update(int pl,int pr,ll val,int l,int r,int cur) {
	if(pl <= l && pr >= r) {
		tree[cur].val += (r-l+1)*val;
		tree[cur].laz += val;
		return ;
	}
	pushdown(l,r,cur);
	int m = (l + r)/2;
	if(pl <= m) update(pl,pr,val,l,m,2*cur);
	if(pr >= m+1) update(pl,pr,val,m+1,r,2*cur+1);
	pushup(cur);
}
ll query(int pl,int pr,int l,int r,int cur) {
	if(pl <= l && pr >= r) return tree[cur].val;
	pushdown(l,r,cur);
	int m = (l+r)/2;
	ll ans = 0 ;
	if(pl <= m ) ans += query(pl,pr,l,m,2*cur);
	if(pr >= m+1) ans += query(pl,pr,m+1,r,2*cur+1);//别写成cur了 
	return ans; 
}
int main()
{
	cin>>n>>m;
	int tmp1,tmp2;
	ll tmp3;
	char op[10];
	for(int i = 1; i<=n; i++) {
		scanf("%lld",&a[i]);
	}
	build(1,n,1);
	while(m--) {
		scanf("%s",op);
		if(op[0] == 'C') {
			scanf("%d%d%lld",&tmp1,&tmp2,&tmp3); 
			update(tmp1,tmp2,tmp3,1,n,1);
		}
		else {
			scanf("%d%d",&tmp1,&tmp2);
			printf("%lld\n",query(tmp1,tmp2,1,n,1));
		}
	}
	return 0 ;
 } 

总结:

    1WA了,因为pushdown的laz修改的时候有个地方写成了=  应该是+=