题目链接:
http://poj.org/problem?id=3468
A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 149649 Accepted: 46438 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.
Source
POJ Monthly--2007.11.25, Yang Yi
区间更新和和区间查询的模板
This is the code
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long //1844674407370955161
#define INT_INF 0x3f3f3f3f //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
const int MAXN=100005;
LL sum[MAXN<<2],lazy[MAXN<<2];
//sum[]模拟线段树维护区间和,lazy[]为懒惰标记
void PushUp(int i)
{
sum[i]=sum[i<<1]+sum[i<<1|1];
}
void PushDown(int i,int ln,int rn)//ln表示左子树元素结点个数,rn表示右子树结点个数
{
if (lazy[i])
{
lazy[i<<1]+=lazy[i];
lazy[i<<1|1]+=lazy[i];
sum[i<<1]+=lazy[i]*ln;
sum[i<<1|1]+=lazy[i]*rn;
lazy[i]=0;
}
}
void Build(int l,int r,int i)//建树,Build(1,n,1);
{
if (l==r)
{
scanf("%lld",&sum[i]);
return;
}
int mid=(l+r)>>1;
Build(l,mid,i<<1);
Build(mid+1,r,i<<1|1);
PushUp(i);
}
void Add(int L,int C,int l,int r,int i)//单点更新,Add(L,C,1,n,1);
{
if (l==r)
{
sum[i]+=C;
return;
}
int mid=(l+r)>>1;
PushDown(i,mid-l+1,r-mid); //若既有点更新又有区间更新,需要这句话
if (L<=mid)
Add(L,C,l,mid,i<<1);
else
Add(L,C,mid+1,r,i<<1|1);
PushUp(i);
}
void Update(int L,int R,int C,int l,int r,int i)//区间更新,Update(L,R,C,1,n,1);
{
if (L<=l&&r<=R)
{
sum[i]+=C*(r-l+1);
lazy[i]+=C;
return;
}
int mid=(l+r)>>1;
PushDown(i,mid-l+1,r-mid);
if (L<=mid)
Update(L,R,C,l,mid,i<<1);
if (R>mid)
Update(L,R,C,mid+1,r,i<<1|1);
PushUp(i);
}
LL Query(int L,int R,int l,int r,int i)//区间查询,Query(L,R,1,n,1);
{
if (L<=l&&r<=R)
return sum[i];
int mid=(l+r)>>1;
PushDown(i,mid-l+1,r-mid);//若更新只有点更新,不需要这句
LL ANS=0;
if (L<=mid)
ANS+=Query(L,R,l,mid,i<<1);
if (R>mid)
ANS+=Query(L,R,mid+1,r,i<<1|1);
return ANS;
}
int main()
{
//freopen("D:\\chnegxubianji\\inORout\\in.txt", "r", stdin);
//freopen("D:\\chnegxubianji\\inORout\\out.txt", "w", stdout);
int n,q;
scanf("%d%d",&n,&q);
Build(1,n,1);
while(q--)
{
char str;
scanf(" %c",&str);
if(str=='Q')
{
int x,y;
scanf("%d%d",&x,&y);
printf("%lld\n",Query(x,y,1,n,1));
}
else
{
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
Update(x,y,w,1,n,1);
}
}
return 0;
}