poj 题目3468 A Simple Problem with Integers (线段树)

 

 

A Simple Problem with Integers
 
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 47665 Accepted: 14016
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

Hint

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

 

 

 

/**********************

Time: 1579MS   Memory: 6724KB Author: zyh Status: Accepted **********************/

#include<stdio.h>
#include<string.h>
#define N 100010
struct IntevalTree{
	int L,R;
	__int64 value,add;
}tree[N*4];

int k;
int a[N];
__int64 sum;

void build(int root,int l,int r){
	tree[root].L = l; tree[root].R = r;
	tree[root].add = 0;
	if(tree[root].L == tree[root].R){
		tree[root].value = a[k++];
		return;
	} 
	
	int mid = (l+r)>>1;	
	build(root<<1,l,mid);
	build(root<<1|1,mid+1,r);
	
	tree[root].value = tree[root<<1].value + tree[root<<1|1].value;
}

void change(int root,int l,int r,long long v){
	
	
	if( tree[root].L == l && tree[root].R == r){	//最后一次不加了,最后结算时再加上	
		tree[root].add += v;			
		return;
	}
	tree[root].value += v*(r-l+1);
		
	int mid = (tree[root].L + tree[root].R)>>1;		
	if(r<=mid)	change(root<<1,l,r,v);		
	else if(l>mid)	change(root<<1|1,l,r,v);
	else {
		change(root<<1,l,mid,v);
		change(root<<1|1,mid+1,r,v);
	}
}
long long query(int p, int left, int right) {
    int mid,v;
    v=p<<1;
    mid=(tree[p].L+tree[p].R)>>1;
    
    if(tree[p].L==left&&tree[p].R==right) {
        return (tree[p].value+tree[p].add*(tree[p].R-tree[p].L+1));
    }
    else{//可能区间有增量,需要计算并下移
    	tree[v].add+=tree[p].add;
        tree[v+1].add+=tree[p].add;//增量下移 
        tree[p].value+=(tree[p].R-tree[p].L+1)*tree[p].add;
        tree[p].add=0;
    }
    if(right<=mid)
        return query(v,left,right);
    else if(left>=mid+1)
        return query(v+1,left,right);
    else
        return query(v,left,mid)+query(v+1,mid+1,right);
}

int main()
{
	int n,q,i,aa,b,c;
	char s[2];
	while(scanf("%d%d",&n,&q)!=EOF){

		for(i=0;i<n;i++) scanf("%d",&a[i]);
		
		k=0;	build(1,1,n);
		
		while(q--){
			scanf("%s",&s);
			if(s[0]=='C'){
				scanf("%d%d%d",&aa,&b,&c);
				change(1,aa,b,c);
			}
			else{
				scanf("%d%d",&aa,&b);			
					
				printf("%I64d\n",query(1,aa,b));
			}
		}
	}
	return 0;
}


/*
10 20
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
Q 1 6
Q 1 9
C 1 4 2
C 2 4 2
Q 1 1
Q 2 2
Q 3 3
Q 4 4
Q 5 5
Q 6 6
Q 7 7
Q 1 3

*/