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

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 }

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

时间: 2024-10-19 15:02:08

POJ-3468 - A Simple Problem with Integers(线段树区间更新模板)的相关文章

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

题目地址:POJ 3468 打了个篮球回来果然神经有点冲动..无脑的狂交了8次WA..居然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题.区间更新就是加一个lazy标记,延迟标记,只有向下查询的时候才将lazy标记向下更新.其他的均按线段树的来就行. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <math.h> #include <stac

(简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

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. 题意

[POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

A Simple Problem with Integers 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

POJ 3468 A Simple Problem with Integers(线段树区间更新区间求和)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   Accepted: 20818 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

poj 3468 A Simple Problem with Integers 线段树区间更新

点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 63565   Accepted: 19546 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations.

POJ 3468 A Simple Problem with Integers 线段树 区间更新 区间查询

题目链接: http://poj.org/problem?id=3468 题目描述: 一组数列, 可进行一段区间加上某一个数, 和区间查询 解题思路: 线段树, 之前的那道题是求总区间直接输出sum[1] 就可以了, 这次有了区间查询, 同理, 查询的时候Pushdown 代码: #include <iostream> #include <cstdio> #include <string> #include <vector> #include <map

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

http://poj.org/problem?id=3468 题意 :对于一个序列有两种操作 1 查询 l到r 的和 2 对于l 到r上的每个数 加上 up 思路: 用单点更新必然超时 所以需要区间更新 (位运算时 注意 m-m>>1 与 m-(m>>1) 的区别) #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using names

POJ 3468 A Simple Problem with Integers (线段树区域更新)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 62431   Accepted: 19141 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

A Simple Problem with Integers 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

POJ 3468 A Simple Problem with Integers //线段树的成段更新

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59046   Accepted: 17974 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of