码迷,mamicode.com
首页 > 其他好文 > 详细

POJ-3468 - A Simple Problem with Integers(线段树区间更新模板)

时间:2019-08-22 09:20:54      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:hint   fir   nta   ref   als   you   number   upd   script   

http://poj.org/problem?id=3468

 

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.
 
 
线段树区间更新求和的模板题,直接上代码
  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <iostream>
  4 #include <string>
  5 #include <math.h>
  6 #include <algorithm>
  7 #include <vector>
  8 #include <queue>
  9 #include <set>
 10 #include <map>
 11 #include <math.h>
 12 const int INF=0x3f3f3f3f;
 13 typedef long long LL;
 14 const int mod=1e9+7;
 15 //const double PI=acos(-1);
 16 const int maxn=1e5+10;
 17 using namespace std;
 18 //ios::sync_with_stdio(false);
 19 //    cin.tie(NULL);
 20 
 21 int n,m;
 22 struct node
 23 {
 24     int l;
 25     int r;
 26     LL lazy;//注意lazy也要开LL 
 27     LL sum;
 28 }SegTree[maxn<<2];
 29 
 30 void PushUp(int rt)
 31 {
 32     SegTree[rt].sum=SegTree[rt<<1].sum+SegTree[rt<<1|1].sum;
 33 }
 34 
 35 void PushDown(int rt)
 36 {
 37     if(SegTree[rt].lazy!=0)
 38     {
 39         SegTree[rt<<1].lazy+=SegTree[rt].lazy;
 40         SegTree[rt<<1|1].lazy+=SegTree[rt].lazy;
 41         SegTree[rt<<1].sum+=(SegTree[rt<<1].r-SegTree[rt<<1].l+1)*SegTree[rt].lazy;
 42         SegTree[rt<<1|1].sum+=(SegTree[rt<<1|1].r-SegTree[rt<<1|1].l+1)*SegTree[rt].lazy;
 43         SegTree[rt].lazy=0;
 44     }
 45 }
 46 
 47 void Build(int l,int r,int rt)
 48 {
 49     SegTree[rt].l=l;
 50     SegTree[rt].r=r;
 51     SegTree[rt].lazy=0;//多样例时必须加 
 52     if(l==r)
 53     {
 54         scanf("%lld",&SegTree[rt].sum);
 55         return;
 56     }
 57     int mid=(l+r)>>1;
 58     Build(l,mid,rt<<1);
 59     Build(mid+1,r,rt<<1|1);
 60     PushUp(rt);
 61 }
 62 
 63 void Update(int L,int R,int add,int rt)
 64 {
 65     int l=SegTree[rt].l;
 66     int r=SegTree[rt].r;
 67     if(L<=l&&R>=r)
 68     {
 69         SegTree[rt].sum+=(SegTree[rt].r-SegTree[rt].l+1)*add;
 70         SegTree[rt].lazy+=add;
 71         return ;
 72     }
 73     PushDown(rt);//向下更新lazy 
 74     int mid=(l+r)>>1;
 75     if(L<=mid)
 76         Update(L,R,add,rt<<1);
 77     if(R>mid)
 78         Update(L,R,add,rt<<1|1);
 79     PushUp(rt);
 80 }
 81 
 82 LL Query(int L,int R,int rt)
 83 {
 84     int l=SegTree[rt].l;
 85     int r=SegTree[rt].r;
 86     if(L<=l&&R>=r)
 87     {
 88         return SegTree[rt].sum;
 89     }
 90     PushDown(rt);//向下更新lazy 
 91     int mid=(l+r)>>1;
 92     LL sum=0;
 93     if(L<=mid)
 94         sum+=Query(L,R,rt<<1);
 95     if(R>mid)
 96         sum+=Query(L,R,rt<<1|1);
 97     return sum;
 98 }
 99 
100 int main()
101 {
102     while(~scanf("%d %d",&n,&m))
103     {
104         Build(1,n,1);
105         for(int i=1;i<=m;i++)
106         {
107             char c[5];
108             int a,b;
109             scanf("%s %d %d",c,&a,&b);
110             if(c[0]==Q)
111             {
112                 printf("%lld\n",Query(a,b,1));
113             }
114             else if(c[0]==C)
115             {
116                 int add;
117                 scanf("%d",&add);
118                 Update(a,b,add,1);
119             }
120         }
121     }
122     return 0;
123 }

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

POJ-3468 - A Simple Problem with Integers(线段树区间更新模板)

标签:hint   fir   nta   ref   als   you   number   upd   script   

原文地址:https://www.cnblogs.com/jiamian/p/11392142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!