https://vjudge.net/problem/POJ-3468
注意点:该改longlong的一定要改,数组要开3倍左右,字符读入建议%s字符数组
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.
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL long long
const int maxn=1e5+10;
LL tree[maxn*4];
LL a[maxn*4];
LL lazy[maxn*4];
void Pushup(int rt)
{
tree[rt]=tree[rt*2]+tree[rt*2+1];
// printf("rt=%d,tree[rt]=%d\n",rt,tree[rt]);
}
void Build(int l,int r,int rt)
{
if(l==r)
{
tree[rt]=a[l];return;
}
int m=(l+r)/2;
Build(l,m,rt*2); ///1和L要分清啊...
Build(m+1,r,rt*2+1);
Pushup(rt);
}
void Pushdown(int rt,int llen,int rlen)
{
if(lazy[rt]!=0)
{
lazy[rt*2] += lazy[rt];
lazy[rt*2+1] += lazy[rt];
tree[rt*2] +=lazy[rt]*llen;
tree[rt*2+1] +=lazy[rt]*rlen;
lazy[rt]=0;
}
}
void Update(int l,int r,int rt,int L,int R,LL C)
{
if( L<=l && r<=R )
{
tree[rt]+=(LL)C*(r-l+1);
lazy[rt]+=C;
return;
}
int m=(l+r)/2;
Pushdown(rt,m-l+1,r-m);
if(L<=m) Update(l,m,rt*2,L,R,C);
if(m<R) Update(m+1,r,rt*2+1,L,R,C);
Pushup(rt);
}
LL Query(int l,int r,int rt,int L,int R)
{
if(L<=l&&r<=R)
{
return tree[rt];
}
LL ans=0;
int m=(l+r)/2;
Pushdown(rt,m-l+1,r-m);
if(L<=m) ans+=Query(l,m,rt*2,L,R);
if(m<R) ans+=Query(m+1,r,rt*2+1,L,R);
return ans;
}
int main(void)
{
int n,q;
while(scanf("%d%d", &n,&q)!=EOF)
{
memset(lazy,0,sizeof(lazy));memset(tree,0,sizeof(tree));memset(a,0,sizeof(a));
for(long int i=1;i<=n;i++)
scanf("%lld", &a[i]);
Build(1,n,1);
char s[3];
int k,b;
LL c;
for(long int j=1;j<=q;j++)
{
scanf("%s", s);
if(s[0]=='Q')
{
// getchar();
scanf("%d%d", &k, &b);
printf("%lld\n",Query(1,n,1,k,b));
}
if(s[0]=='C')
{
// getchar();
scanf("%d%d%lld", &k, &b, &c);
Update(1,n,1,k,b,c);
}
}
}
return 0;
}