您的位置:首页 > 其它

poj3468树状数组之区间更新+区间询问

2014-03-11 19:10 387 查看
Language:
Default

A Simple Problem with Integers

Time Limit: 5000MSMemory Limit: 131072K
Total Submissions: 54069Accepted: 16249
Case Time Limit: 2000MS
Description

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 Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.

"Q a b" means querying the sum of Aa, Aa+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

/*分析:由于本题更新的时候是区间更新
所以不能直接去一个个更新区间内的点,肯定会超时
对于每次更新C(a,b,d)表示区间[a,b]内的值增加d
用ans[a]表示a~n区间元素增加的值,所以对于C(a,b,d)有:ans[a]+=d,ans[b+1]-=d;
则每次询问的时候Q(a,b),求a~b的和SUM=sum(a,b)+ans[a]*(b-a+1)+ans[a+1]*(b-a)...+ans[b]//sum(a,b)表示a,b的和 
Sum=sum(a,b)+sum(ans[a+t]*(b-a-t+1))=sum(a,b)+sum(ans[i]*(b-i+1));a<=i<=b;
Sum=sum(a,b)+(b+1)*sum(ans[i])-sum(ans[i]*i);//1~b所以(b+1)*sum(ans[i]),1~a-1则a*sum(ans[i]) 
所以可以用两个树状数组分别表示ans[i]的前缀和 和 ans[i]*i的前缀和 
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=100000+10;
LL n,q;
LL sum[MAX],c1[MAX],c2[MAX];

LL lowbit(LL x){
	return x&(-x);
}

void Update(LL x,LL d,LL *c){
	while(x<=n){
		c[x]+=d;
		x+=lowbit(x);
	}
}

LL Query(LL x,LL *c){
	LL sum=0;
	while(x>0){
		sum+=c[x];
		x-=lowbit(x);
	}
	return sum;
}

int main(){
	char op[3];
	LL x,y,d;
	while(~scanf("%lld%lld",&n,&q)){
		memset(c1,0,sizeof c1);
		memset(c2,0,sizeof c2);
		for(int i=1;i<=n;++i)scanf("%lld",sum+i),sum[i]+=sum[i-1];
		for(int i=0;i<q;++i){
			scanf("%s",op);
			if(op[0] == 'C'){//ans[x]+=d,ans[y+1]-=d
				scanf("%lld%lld%lld",&x,&y,&d);
				Update(x,d,c1);
				Update(y+1,-d,c1);
				Update(x,x*d,c2);
				Update(y+1,-(y+1)*d,c2);
			}else{
				scanf("%lld%lld",&x,&y);
				printf("%lld\n",sum[y]-sum[x-1]+Query(y,c1)*(y+1)-Query(x-1,c1)*x-Query(y,c2)+Query(x-1,c2));
			}
		}
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: