转载请注明出处:http://blog.csdn.net/u012860063

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

代码例如以下:

//线段树功能:update:成段增减 query:区间求和
//此题为Poj 3468 代码 #include <cstdio>
#include <algorithm>
using namespace std;
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
//lson和rson分辨表示结点的左儿子和右儿子
//rt表示当前子树的根(root),也就是当前所在的结点
#define LL long long
const int maxn = 111111;
//maxn是题目给的最大区间,而节点数要开4倍,确切的来说节点数要开大于maxn的最小2x的两倍
LL add[maxn<<2];//用来标记每一个节点,为0则表示没有标记,否则为标记。
LL sum[maxn<<2];//求和
void PushUp(int rt) //把当前结点的信息更新到父结点
{
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void PushDown(int rt,int len)//把当前结点的信息更新给儿子结点,len为分区间长度
{//对某一个区间进行改变,假设被标记了,在查询的时候就得把改变传给子节点,由于查询的并不一定是当前区间
if (add[rt]) //已经标记过。该区间被改变过
{
//由于rt的儿子节点可能被多次延迟标记。而且rt的儿子节点的延迟标记没有向rt的孙子节点移动,所以用“+=”
add[rt<<1] += add[rt];
add[rt<<1|1] += add[rt];
/*此处用add[rt]乘以区间长度,不是add[rt<<1], 由于rt的儿子节点假设被多次标记,之前被标记时,
就已经对sum[rt<<1]更新过了。 */
sum[rt<<1] += add[rt] * (len - (len >> 1));//更新左儿子的和
sum[rt<<1|1] += add[rt] * (len >> 1);//更新右儿子的和
add[rt] = 0;//将标记向儿子节点移动后。父节点的延迟标记去掉
}
}
void build(int l,int r,int rt)
{
add[rt] = 0;//初始化为全部结点未被标记
if (l == r)
{
scanf("%lld",&sum[rt]);
return ;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
if (L <= l && r <= R)
{
add[rt] += c;
sum[rt] += (LL)c * (r - l + 1);//更新代表某个区间的节点和,该节点不一定是叶子节点
return ;
}
/*当要对被延迟标记过的这段区间的儿子节点进行更新时,先要将延迟标记向儿子节点移动
当然,假设一直没有对该段的儿子节点更新,延迟标记就不须要向儿子节点移动,这样就使
更新操作的时间复杂度仍为O(logn),也是使用延迟标记的原因。
*/
PushDown(rt , r - l + 1);//向下传递
int mid = (l + r) >> 1;
if (L <= mid)
update(L , R , c , lson);//更新左儿子
if (mid < R)
update(L , R , c , rson);//更新右儿子
PushUp(rt);//向上传递更新和
}
LL query(int L,int R,int l,int r,int rt)
{
if (L <= l && r <= R)
{
return sum[rt];
}//要取rt子节点的值时。也要先把rt的延迟标记向下移动
PushDown(rt , r - l + 1);
int mid = (l + r) >> 1;
LL ret = 0;
if (L <= mid)
ret += query(L , R , lson);
if (mid < R)
ret += query(L , R , rson);
return ret;
}
int main()
{
int N , Q;
scanf("%d%d",&N,&Q);//N为节点数
build(1 , N , 1); //建树
while (Q--)//Q为询问次数
{
char op[2];
int a , b , c;
scanf("%s",op);
if (op[0] == 'Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",query(a , b , 1 , N , 1));
}
else
{
scanf("%d%d%d",&a,&b,&c);//c为区间a到b添加的值
update(a , b , c , 1 , N , 1);
}
}
return 0;
}

poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)的更多相关文章

  1. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  2. POJ3468 A Simple Problem with Integers(线段树延时标记)

    题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...

  3. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

  4. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  5. poj 3468 A Simple Problem with Integers 线段树 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3468 线段树模板 要背下此模板 线段树 #include <iostream> #include <vector> ...

  6. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  7. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

    A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...

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

    A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...

  9. Poj 3468-A Simple Problem with Integers 线段树,树状数组

    题目:http://poj.org/problem?id=3468   A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

随机推荐

  1. C#版二维码生成器附皮肤下载

    原文 C#版二维码生成器附皮肤下载 前言 本文所使用的二维码生成代码是谷歌开源的条形码图像处理库完成的,c#版的代码可去https://code.google.com/p/zxing/download ...

  2. 配置Jenkins的slave节点的详细步骤适合windows等其他平台(转)

    @  新建一个slave节点在Jenkins服务器上 1,进入Jenkins的主界面,进入“Manage Jenkins” 页面: 2,点击如下图中的“Manage  Nodes”: 3,进入页面后点 ...

  3. boost:库program_options--第一篇

    程式執行參數處理函式庫:Boost Program Options(1/N) 一般程式寫得大一點.或是需要比較有彈性,通常都需要在程式執行的時候,從外部讀取一些參數,來做為內部的設定值.一般來說,比較 ...

  4. grunt getHTML

    var Base = require( "../common/base" ) , HandlerBase = require( "../common/handlerBas ...

  5. 统一横轴墨卡托投影(UTM)

    UTM 坐标系统使用基于网格的方法表示坐标.UTM 系统将地球分为 60 个区,每一个区基于横轴墨卡托投影.画图法中的地图投影方法能够在平面中表示一个两维的曲面,比如一个标准地图.图 1 展示了一个横 ...

  6. jquery validate 插件使用小结

    项目中整合了jquery validate插件,仿照别人的使用写了我的表单验证,结果不起作用.然后就各种找原因. 在网上下了jquery validate插件的完整包,看了看里边的例子,跟我的使用貌似 ...

  7. 如何使用不同dll的相同namespace下的相同接口

    问题: 程序里加载了2个dll,这2个dll里都声明了同样的命名空间(这个不违法),然后在这个同样的命名空间下,他俩又定义了同名的interface. 然后我程序里直接using这个命名空间,使用这个 ...

  8. linux下shutdown无法关闭tomcat进程的解决方式

    1.问题 笔者在linux下发现使用tomcat6.0.41自带的./shutdown.sh常常无法停止进程,导致各种问题的发生,令笔者相当反感! 2.解决方式一: 查找到全部的tomcat进程 $ ...

  9. opencv实现连通域

    在本文中使用图像连通域统计使用opencv中的cvFloodFill方法,可是在cvFloodFill方法中CvConnectedComp參数无法返回详细点坐标位置信息,找了些资料.给CvSeq分配空 ...

  10. iOS经常使用加密方式(MD5,AES,BASE64)与网络数据安全

    演示样例项目下载地址  https://github.com/cerastes/Encryption 1MD5 创建MD5类 #import <Foundation/Foundation.h&g ...