A Simple Problem with Integers
You have N integers, A1, A2, … , AN. You need to deal with two kindsof operations. One type of operation is to add some given number toeach 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 <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define MAX_LEN 100005
ll add[MAX_LEN<<2];
struct node{
int ln,rn; //记录左右节点
ll sum; //节点的值
}tree[MAX_LEN*4];
void push_Up(int root) //数据上浮更新
{
tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;
}
void push_Down(int root,int length) //数据下沉,传入下沉结点和下沉区间差值
{
if(add[root]){ //如果root对应区间需要更新
add[root<<1]+=add[root]; //左右子叶状态更新
add[root<<1|1]+=add[root];
tree[root<<1].sum+=add[root]*(length-(length>>1)); //左右子叶元素和更新
tree[root<<1|1].sum+=add[root]*(length>>1);
add[root]=0; //标记root区间状态已更新
}
}
void build_Tree(int root,int L,int R) //建树
{
add[root]=0; //初始化所有结点对应区间均不需要更新
tree[root].ln=L;
tree[root].rn=R;
if(tree[root].ln==tree[root].rn){ //最底层接收数据
scanf("%I64d",&tree[root].sum);
return;
}
int mid=(L+R)>>1;
build_Tree(root<<1,L,mid); //建立左子叶
build_Tree(root<<1|1,mid+1,R); //建立右子叶
push_Up(root); //建立后的子叶开始数据上浮
}
void update(int root,int left,int right,int num) //更新
{
if(left<=tree[root].ln&&right>=tree[root].rn){ //当前区间完全处于待更新区间内
add[root]+=num;
tree[root].sum+=(long long int)num*(tree[root].rn-tree[root].ln+1);
return;
}
push_Down(root,tree[root].rn-tree[root].ln+1);
int mid=(tree[root].ln+tree[root].rn)>>1;
if(right<=mid) //如果待查找区间完全在当前区间左子叶部分
update(root<<1,left,right,num);
else if(left>mid) //如果待查找区间完全在当前区间右子叶部分
update(root<<1|1,left,right,num);
else{ //如果待查找区间横跨当前区间
update(root<<1,left,right,num);
update(root<<1|1,left,right,num);
}
push_Up(root);
}
ll query(int root,int left,int right) //查询
{
if(left<=tree[root].ln&&right>=tree[root].rn) //当前区间完全在查询区间范围内
return tree[root].sum; //返回当前区间总和
push_Down(root,tree[root].rn-tree[root].ln+1); //由于是延迟更新,所以数据需要下沉一层
int mid=(tree[root].ln+tree[root].rn)>>1;
ll sum=0;
if(right<=mid) //待查找区间完全在当前区间左子叶部分
sum+=query(root<<1,left,right);
else if(mid<left) //待查找区间完全在当前区间右子叶部分
sum+=query(root<<1|1,left,right);
else{ //待查找区间分别在区间左右子叶
sum+=query(root<<1,left,right);
sum+=query(root<<1|1,left,right);
}
return sum;
}
int main()
{
char s[10];
int n,q;
int x,y,z;
cin>>n>>q;
build_Tree(1,1,n);
while(q--){
cin>>s;
if(s[0]=='Q'){
cin>>x>>y;
cout<<query(1,x,y)<<endl;
}
else if(s[0]=='C'){
cin>>x>>y>>z;
update(1,x,y,z);
}
}
return 0;
}