树状数组关于区间修改区间求和的问题

Total Submission(s) : 77   Accepted Submission(s) : 19
Problem 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
题意:
就是给你N个数M个操作,然后输出每一个查询区间的和。 
这是最基础的区间修改区间求和的实现问题。
#if 0
#include
#include
#include

using namespace std;
const int MAXN=100000+50;
typedef long long ll;
ll  b[MAXN],c[MAXN];
ll  a[MAXN]; 

int lowbit(ll  x)
{
	return x&(-x);
}

void add_B(ll  *a,ll  x,ll  v)
{
	for( ; x>0; x-=lowbit(x))
		a[x]+=v;
}
ll sum_B(ll  *a,ll  x)
{
	ll  s=0;
	for(; x0; x-=lowbit(x))
	{
		s+=a[x];
	}
	return s;
}

void add(ll x,ll v)
{
	add_B(b,x-1,v);
	add_C(c,x,v*x);
}
void go_add(ll a,ll b,ll v)
{
	add(b,v);
	add(a-1,-v); //
}

ll sum(ll x)
{
	return sum_C(c,x)+sum_B(b,x)*x;
}
ll go_sum(ll a,ll b)
{
	return sum(b)-sum(a-1);
}

int main()
{


	ll n,t;
	char ssc;

	scanf("%lld%lld",&n,&t);
	
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	
	for(int i=3; i<=n+2; i++)
	{
		ll x;
		scanf("%lld",&x);
		go_add(i,i,x);
	}
	

	while(t--)
	{
		
		cin>>ssc;
		if(ssc=='Q')
		{
			ll ai,bi;
			
			scanf("%lld%lld",&ai,&bi);
			ai+=2;
			bi+=2;
	
			
			printf("%lld\n",go_sum(ai,bi));	
		}
		else if(ssc=='C')
		{
			ll ai,bi;
			ll v;
			scanf("%lld%lld%lld",&ai,&bi,&v);
			
			ai+=2;
			bi+=2;
			go_add(ai,bi,v);
		}

	}


}
#endif


























你可能感兴趣的:(树状数组)