题目链接:http://poj.org/problem?id=3468
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.
ti'm题目大意:给你n个数,m个操作,
每个操作有两种情况,输入Q时,输入两个数,a,b,求a~b的值的和
输入C时,输入三个数,a,b,x,让a~b区间的每个数都加上x。
区间修改+qu'j区间查询,模板
ac:
#include<stdio.h>
#include<string.h>
#include<math.h>
//#include<map>
//#include<set>
#include<deque>
#include<queue>
#include<stack>
#include<bitset>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define mod 1000000007
#define clean(a,b) memset(a,b,sizeof(a))// 水印
//区间修改,区间查询:
//sum(1,k)表示区间1-k的和。
//则sum(1,k)=c1(1)+( c1(1)+c1(2) )+( c1(3)+c1(3)+c1(3) )+
//...+(c1(1)+c1(2)+c1(3)+...+c1(k)).
//打开多项式,合并:
//sum(1,k)=k*( c1(1)+c1(2)+...+c1(k) )-( 0*c1(1)+1*c1(2)+2*c1(3)+...+(k-1)*c1(k) )。
//因此分成两个数组,建立两个树状数组,
//分别为:
//tree1 = k*( c1(1)+c1(2)+...+c1(k) ) 前缀和
//tree1[]=c1(1),c1(2),c1(3), ... ,c1(k);
///*-------*/
//tree2 = ( 0*c1(1)+1*c1(2)+2*c1(3)+...+(k-1)*c1(k) ) i的前缀和
//tree2[]=c1(1)*0,c1(2)*1,c1(3)*2, ... ,(k-1)*c1(k);
//用第一个减去第二个就是区间查询的结果了
const ll N=100100;
ll tree1[N],tree2[N];
ll sum[N];
ll n,q;
ll lowbit(ll i)
{
return i&(-i);
}
void updata(ll i,ll x,ll *tree)
{
while(i<=n)
{
tree[i]=tree[i]+x;
i=i+lowbit(i);
}
}
ll Query(ll i,ll *tree)
{
ll sum=0;
while(i>0)
{
sum=sum+tree[i];
i=i-lowbit(i);
}
return sum;
}
int main()//poj-3468
{
scanf("%lld%lld",&n,&q);
clean(tree1,0);
clean(tree2,0);
clean(sum,0);
for(ll i=1;i<=n;++i)
{
scanf("%lld",&sum[i]);
sum[i]=sum[i]+sum[i-1];
}
for(ll i=1;i<=q;++i)
{
// for(int j=1;j<=50;++j)
// cout<<sum[j]<<" ";
// cout<<endl;
// for(int j=1;j<=50;++j)
// cout<<tree1[j]<<" ";
// cout<<endl;
// for(int j=1;j<=50;++j)
// cout<<tree2[j]<<" ";
// cout<<endl;
char s[10];
scanf("%s",s);
// cout<<s<<endl;
if(strcmp(s,"Q")==0)
{
ll a,b;
scanf("%lld%lld",&a,&b);
ll res=sum[b]-sum[a-1];
res=res+(b+1)*Query(b,tree1)-a*Query(a-1,tree1);
res=res-(Query(b,tree2)-Query(a-1,tree2));
cout<<res<<endl;
//sum=sum_tree1-sum_tree2
// cout<<(sum[b]+(b+1)*Query(b,tree1)-Query(b,tree2))-
// (sum[a-1]+a*Query(a-1,tree1)-Query(a-1,tree2))
// <<endl;
}
else if(strcmp(s,"C")==0)
{
ll a,b,x;
scanf("%lld%lld%lld",&a,&b,&x);
updata(a,x,tree1);
updata(b+1,-x,tree1);
updata(a,a*x,tree2);
updata(b+1,-(b+1)*x,tree2);
}
}
}