A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 152160 Accepted: 47174
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.
思路:这题是线段树区间更新的模版题,所以说一下线段树区间更新的思路。区间更新和单点更新不一样,单点更新一般是递归找到要更新的子节点,更新子节点,之后用push_up的方式就可以回溯更新父节点维护的信息。但是区间更新如果到某个节点,该节点代表的区间完全在要更新的区间范围内,则可以直接更新该节点维护的信息,相应的置该节点的lazy为要更新的值,同时其所有的子节点维护的信息都相应的被修改,但是这个修改并不马上进行,而是到需要向下递归时,先检测当前节点的lazy标记,lazy标记不为0,则更新该节点维护的值。
#include "iostream"
#include "cstdio"
#include "cstring"
using namespace std;
#define ll long long
int N, M;
const int maxn = 1e5 + 6;
int a[maxn];
struct node{
int l, r, lazy;
ll sum;
int mid(){
return (l + r) / 2;
}
void update(int val){
sum += 1ll * (r - l + 1) * val;
lazy += val;//标记该节点被修改过,当通过该节点要访问子节点时需要push_down
//因为有可以对一个节点多次更新,所以用+=,因为当前节点的lazy标记可能也不为0,不能用=!!!
}
} tree[4 * maxn + 1];
void build(int x, int l, int r){
tree[x].l = l, tree[x].r = r;
tree[x].sum = tree[x].lazy = 0;//建立树的时候,当前节点并没有被更新
if(l == r){//叶节点
tree[x].sum = a[l];
return;
}
else{
int mid = tree[x].mid();
build(x << 1, l, mid);//二分区间
build(x << 1 | 1, mid + 1, r);
tree[x].sum = tree[x << 1].sum + tree[x << 1 | 1].sum;//push_up
}
}
void push_down(int x){
int lazyval = tree[x].lazy;
if(lazyval){
tree[x << 1].update(lazyval);//更新子节点维护的信息,同时更新子节点的lazy标记
tree[x << 1 | 1].update(lazyval);
tree[x].lazy = 0;//当前节点的lazy标记清零
}
}
void update(int x, int l, int r, int val){//区间[l, r]的值加上val
int L = tree[x].l, R = tree[x].r;
if (l <= L && R <= r){//不要到叶子节点才更新效率太低,会超时,[L,R]在[l,r]区间内就可以更新
tree[x].update(val);
}
else{
push_down(x);//下面需要使用到递归到x的子节点,先检测该节点lazy标记(是否被更新过)
int mid = tree[x].mid();
if(l<=mid){
update(x << 1, l, r, val);
}
if(r>mid){
update(x << 1 | 1, l, r, val);
}
tree[x].sum = tree[x << 1].sum + tree[x << 1 | 1].sum;//push_up
}
}
ll query(int x, int l, int r){
int L = tree[x].l, R = tree[x].r;
if(l<=L && R<=r){
return tree[x].sum;
}
else{
push_down(x);//要向下递归子节点之前先将lazy标志下传,更新子节点
ll ans = 0;
int mid = tree[x].mid();
if(l<=mid)
ans += query(x << 1, l, r);
if(r>mid)
ans += query(x << 1 | 1, l, r);
return ans;
}
}
int main()
{
scanf("%d%d", &N, &M);
for (int i = 1; i <= N; i++)
{
scanf("%d", &a[i]);
}
build(1, 1, N);
for (int i = 1; i <= M; i++)
{
char ch[2];
scanf("%s", ch);
int begin, end;
scanf("%d%d", &begin, &end);
if (ch[0] == 'Q'){
printf("%lld\n", query(1, begin, end));
}
if(ch[0] == 'C'){
int val;
scanf("%d", &val);
update(1, begin, end, val);
}
}
//system("pause");
return 0;
}