POJ 3468 区间更新,区间求和(经典)

A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 72265   Accepted: 22299
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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.

Source

 
题目意思:
给一个长度为n的数组,有q组操作,操作有两种:Q l, r  即查询l到r的和。  C l, r, val   即把l到r数组元素都加上val
 
思路:
线段树经典题目,需要用lazy思想,也就是当把l r加上val时,只标记这个区间lazy=val即可 ,当下次再加上一个val时且在l r之间,那么向下传递。
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 100005
#define ll root<<1
#define rr root<<1|1
#define mid (a[root].l+a[root].r)/2 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n; struct node{
int l, r;
__int64 val, sum;
}a[N*]; void build(int l,int r,int root){
a[root].l=l;
a[root].r=r;
a[root].val=;
if(l==r){
scanf("%I64d",&a[root].sum);
return;
}
build(l,mid,ll);
build(mid+,r,rr);
a[root].sum=a[ll].sum+a[rr].sum;
} void down(int root){
if(a[root].val&&a[root].l!=a[root].r) {
a[ll].val+=a[root].val;
a[rr].val+=a[root].val;
a[ll].sum+=(__int64)(a[ll].r-a[ll].l+)*a[root].val;
a[rr].sum+=(__int64)(a[rr].r-a[rr].l+)*a[root].val;
a[root].val=;
}
} void update(int l,int r,__int64 val,int root){
if(a[root].l==l&&a[root].r==r){
a[root].val+=val;
a[root].sum+=(__int64)(a[root].r-a[root].l+)*val;
return;
}
down(root);
if(l>=a[rr].l) update(l,r,val,rr);
else if(r<=a[ll].r) update(l,r,val,ll);
else {
update(l,mid,val,ll);
update(mid+,r,val,rr);
}
a[root].sum=a[ll].sum+a[rr].sum;
} __int64 query(int l,int r,int root){
if(a[root].l==l&&a[root].r==r){
return a[root].sum;
}
down(root);
if(r<=a[ll].r) return query(l,r,ll);
else if(l>=a[rr].l) return query(l,r,rr);
else return query(l,mid,ll)+query(mid+,r,rr);
} void out(int root){
if(a[root].l==a[root].r) {
printf("%I64d ",a[root].sum); return;
}
down(root);
out(ll);
out(rr);
}
main()
{
int i, j, k;
int q;
while(scanf("%d %d",&n,&q)==){
build(,n,);
char s[];
__int64 w;
int l, r;
while(q--){
scanf("%s",s);
if(strcmp(s,"Q")==){
scanf("%d %d",&l,&r);
printf("%I64d\n",query(l,r,));
}
else{
scanf("%d %d %I64d",&l,&r,&w);
update(l,r,w,);
// out(1);
}
}
}
}
上一篇:Linux soft lockup分析


下一篇:nginx fastcgi php-fpm的关系梳理