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.
线段树区间修改
交了好多次才AC
#include <iostream>
#include<stdio.h>
using namespace std;
struct node{
long long right,left,value;
long long lazy_add;
bool exist;
};
node tree[1600020];
long long number[100005];
long long found[100005];
void build_tree(long long i,long long le,long long ri)
{
tree[i].left=le;
tree[i].right=ri;
tree[i].exist=true;
tree[i].value=0;
tree[i].lazy_add=0;
if(le==ri)
{
found[le]=i;
tree[i].value=number[le];
return ;
}
build_tree(i<<1,le,(le+ri)>>1);
build_tree((i<<1)+1,((le+ri)>>1)+1,ri);
}
void complete(long long N)
{
for(long long i=4*N-1;i>=1;i=i-2)
{
if(tree[i].exist==true)
tree[i>>1].value=tree[i].value+tree[i-1].value;
}
}
long long m=0;
void push_down(long long i)
{
tree[i].value+= tree[i].lazy_add;
tree[i<<1].lazy_add+= (tree[i].lazy_add)*(tree[i<<1].right-tree[i<<1].left+1)/(tree[i].right-tree[i].left+1);
tree[(i<<1)+1].lazy_add+= (tree[i].lazy_add)*(tree[(i<<1)+1].right-tree[(i<<1)+1].left+1)/(tree[i].right-tree[i].left+1);
tree[i].lazy_add=0;
}
void push_up(long long i,long long ar)
{ long long p=ar;
while(i)
{
i=i/2;
tree[i].value+=p;
}
}
void query(long long i,long long le,long long ri)
{
if((tree[i].lazy_add!=0)&&!(tree[i].left==le&&tree[i].right==ri))
push_down(i);
if(tree[i].left==le&&tree[i].right==ri)
{
m=m+tree[i].value+tree[i].lazy_add;
return ;
}
i=i<<1;
if(le<=tree[i].right)
{
if(ri<=tree[i].right)
query(i,le,ri);
else
query(i,le,tree[i].right);
}
i=i+1;
if(ri>=tree[i].left)
{
if(le>=tree[i].left)
query(i,le,ri);
else
query(i,tree[i].left,ri);
}
}
long long z;
void lazy_change(long long i,long long le,long long ri,long long z)
{
if(tree[i].left==le&&tree[i].right==ri)
{
tree[i].lazy_add+=z*(ri-le+1);
long long ar=z*(ri-le+1);
push_up(i,ar);
return ;
}
i=i<<1;
if(le<=tree[i].right)
{
if(ri<=tree[i].right)
lazy_change(i,le,ri,z);
else
lazy_change(i,le,tree[i].right,z);
}
i=i+1;
if(ri>=tree[i].left)
{
if(le>=tree[i].left)
lazy_change(i,le,ri,z);
else
lazy_change(i,tree[i].left,ri,z);
}
}
int main()
{
long long N,Q;
long long i;
char s[2];
while(scanf("%lld%lld",&N,&Q)!=EOF)
{
for(i=0;i<1600020;i++)
tree[i].exist=false;
for(i=1;i<=N;i++)
{
scanf("%lld",&number[i]);
}
long long x,y;
build_tree(1,1,N);
complete(N);
while(Q--)
{
scanf("%s",&s);
if(s[0]=='Q')
{
m=0;
scanf("%lld%lld",&x,&y);
query(1,x,y);
printf("%lld\n",m);
}
if(s[0]=='C')
{
scanf("%lld%lld%lld",&x,&y,&z);
long long yu=0;
lazy_change(1,x,y,z);
}
}
}
return 0;
}