A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 140849 | Accepted: 43682 | |
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.
题意:
给定Q (1 ≤ Q ≤ 100,000)个数A1,A2 … AQ,, 以及可能多次进行的两个操作:
1)对某个区间Ai … Aj的每个数都加n(n可变) 2) 求某个区间Ai … Aj的数的和
此线段树结点存放sum和增量
起初创建线段树时,就把每个区间的总和求出了
进行Add()操作时,某些值发生变化,也需要改变nsum和lnc的值,.在找区间的时候,若找到的区间正好覆盖一个节点,就增加它的lnc值,若不是覆盖的点区间,需要继续向下找,但此时的区间值的总和也需要增加,如在(3,6)区间加2,对于(1,4)区间它的总和也需要加(4-3)*2个。
查询进行sum()操作时,需要将增量往下带的原因时,下面的一部分属于所查范围,要将所查范围的增量增加。。
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
struct node{
int l;
int r;
long long nsum;
long long nc;
}tree[400010];//叶子结点有n个,那总结点为2n-1
void create(int root,int li,int ri){
tree[root].l=li;
tree[root].r=ri;
tree[root].nsum=0;
tree[root].nc=0;
int mid=(tree[root].l+tree[root].r)/2;
if(li!=ri){
create(2*root+1,li,mid);
create(2*root+2,mid+1,ri);
}
}
void Add(int root,int a1,int a2,int m){
if(tree[root].l==a1&&tree[root].r==a2){
tree[root].nc+=m;
return;
}
tree[root].nsum+=m*(a2-a1+1);
int mid=(tree[root].l+tree[root].r)/2;
if(a2<=mid)
Add(2*root+1,a1,a2,m);
else if(a1>mid)
Add(2*root+2,a1,a2,m);
else{
Add(2*root+1,a1,mid,m);
Add(2*root+2,mid+1,a2,m);
}
}
long long Sum(int root,int a1,int a2){//求区间内元素的和
if(tree[root].l==a1&&tree[root].r==a2){//区间正好覆盖一个结点,此时的该区间的和就为本身的sum值加上各个数的增量值
return tree[root].nsum+(tree[root].r-tree[root].l+1)*tree[root].nc;
}
tree[root].nsum+=tree[root].nc*(tree[root].r-tree[root].l+1);
int mid=(tree[root].l+tree[root].r)/2;
Add(2*root+1,tree[root].l,mid,tree[root].nc);
Add(2*root+2,mid+1,tree[root].r,tree[root].nc);
tree[root].nc=0;
if(a2<=mid)
return Sum(2*root+1,a1,a2);
else if(a1>mid)
return Sum(2*root+2,a1,a2);
else{
return Sum(2*root+1,a1,mid)+Sum(2*root+2,mid+1,a2);
}
}
void Insert(int root,int i,int h){
if(tree[root].l==i&&tree[root].r==i){
tree[root].nsum=h;
return;
}
tree[root].nsum+=h;
int mid=(tree[root].l+tree[root].r)/2;
if(i<=mid)
Insert(2*root+1,i,h);
else
Insert(2*root+2,i,h);
}
int main(){
int n,q,h;
char k;
int a1,a2,m;
scanf("%d%d",&n,&q);
create(0,1,n);
for(int i=1;i<=n;i++){
scanf("%d",&h);
Insert(0,i,h);
}
for(int i=0;i<q;i++){
getchar();
scanf("%c",&k);
if(k=='Q'){
scanf("%d%d",&a1,&a2);
printf("%lld\n",Sum(0,a1,a2));
}
else{
scanf("%d%d%d",&a1,&a2,&m);
Add(0,a1,a2,m);
}
}
}