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之间,那么向下传递。
 
代码:
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <vector>
  6 #include <queue>
  7 #include <cmath>
  8 #include <set>
  9 using namespace std;
 10 
 11 #define N 100005
 12 #define ll root<<1
 13 #define rr root<<1|1
 14 #define mid (a[root].l+a[root].r)/2
 15 
 16 
 17 int max(int x,int y){return x>y?x:y;}
 18 int min(int x,int y){return x<y?x:y;}
 19 int abs(int x,int y){return x<0?-x:x;}
 20 
 21 int n;
 22 
 23 struct node{
 24     int l, r;
 25     __int64 val, sum;
 26 }a[N*4];
 27 
 28 void build(int l,int r,int root){
 29     a[root].l=l;
 30     a[root].r=r;
 31     a[root].val=0;
 32     if(l==r){
 33         scanf("%I64d",&a[root].sum);
 34         return;
 35     }
 36     build(l,mid,ll);
 37     build(mid+1,r,rr);
 38     a[root].sum=a[ll].sum+a[rr].sum;
 39 }
 40 
 41 void down(int root){
 42     if(a[root].val&&a[root].l!=a[root].r) {
 43         a[ll].val+=a[root].val;
 44         a[rr].val+=a[root].val;
 45         a[ll].sum+=(__int64)(a[ll].r-a[ll].l+1)*a[root].val;
 46         a[rr].sum+=(__int64)(a[rr].r-a[rr].l+1)*a[root].val;
 47         a[root].val=0;
 48     }
 49 }
 50 
 51 void update(int l,int r,__int64 val,int root){
 52     if(a[root].l==l&&a[root].r==r){
 53         a[root].val+=val;
 54         a[root].sum+=(__int64)(a[root].r-a[root].l+1)*val;
 55         return;
 56     }
 57     down(root);
 58     if(l>=a[rr].l) update(l,r,val,rr);
 59     else if(r<=a[ll].r) update(l,r,val,ll);
 60     else {
 61         update(l,mid,val,ll);
 62         update(mid+1,r,val,rr);
 63     }
 64     a[root].sum=a[ll].sum+a[rr].sum;
 65 }
 66 
 67 __int64 query(int l,int r,int root){
 68     if(a[root].l==l&&a[root].r==r){
 69         return a[root].sum;
 70     }
 71     down(root);
 72     if(r<=a[ll].r) return query(l,r,ll);
 73     else if(l>=a[rr].l) return query(l,r,rr);
 74     else return query(l,mid,ll)+query(mid+1,r,rr);
 75 }
 76 
 77 void out(int root){
 78     if(a[root].l==a[root].r) {
 79         printf("%I64d ",a[root].sum); return;
 80     }
 81     down(root);
 82     out(ll);
 83     out(rr);
 84 }
 85 main()
 86 {
 87     int i, j, k;
 88     int q;
 89     while(scanf("%d %d",&n,&q)==2){
 90         build(1,n,1);
 91         char s[10];
 92         __int64 w;
 93         int l, r;
 94         while(q--){
 95             scanf("%s",s);
 96             if(strcmp(s,"Q")==0){
 97                 scanf("%d %d",&l,&r);
 98                 printf("%I64d
",query(l,r,1));
 99             }
100             else{
101                 scanf("%d %d %I64d",&l,&r,&w);
102                 update(l,r,w,1);
103             //    out(1);
104             }
105         }
106     }
107 }
原文地址:https://www.cnblogs.com/qq1012662902/p/4534246.html