转载请注明出处: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. 6.6.2 自己主动泛型化(automatic generalization)

    6.6.2 自己主动泛型化(automatic generalization) 在这一章,我们已经实现了几个 F# 的高阶函数.也看到了在 F# 和 C# 中并排的实现.F# 实现的非常重要方面,是我 ...

  2. HDU2571:命运(DP)

    Problem Description 穿过幽谷意味着离大魔王lemon已经无限接近了! 可谁能想到,yifenfei在斩杀了一些虾兵蟹将后,却再次面临命运大迷宫的考验,这是魔王lemon设下的又一个 ...

  3. easyUI相关知识

    $("#sportGroupInfoDialog").dialog("open");//打开一个对话框,设置这个对话框的的布局方式 $('#sportGroup ...

  4. UML03-类图

    1.在类图中,聚合关系表达总体与局部的关系. 2.请根据下面的需求,画出用例图和类图. 系统允许管理员通过磁盘加载存货数据来运行存货清单报告: 管理员通过从磁盘加载存货数据.向磁盘保存存货数据来更新存 ...

  5. ADO.NET 操作数据库 --- 01 简单封装

    由于我是Java转的C#开始的时候就用的NHihernate,和EF 对ADO.NET使用较少,现在封装一个ADO.NET的工具类来实现数据库的操作,比较简单,望大家多多提意见. 如果大家有什么学习中 ...

  6. 基于visual Studio2013解决面试题之1307二分查找

     题目

  7. 阿根廷探戈(Argentine Tango)舞步

    阿根廷探戈(Argentine Tango)舞步 阿根廷探戈(Argentine Tango)舞步 2011-11-22 13:05:11   不像其它大部分的社交舞,阿根廷探戈没有固定的舞步,它是一 ...

  8. if判断 和&&

    function aaa(){   console.log('我是aaa');  };   aaa && aaa();  //如果aaa函数存在 就调用 aaa()    //等价写法 ...

  9. 安全威胁无孔不入:基于Linux系统的病毒(转)

    虽然在Linux里传播的病毒不多,但也是存在一些.我从一些安全网站搜集了一些资料. 1.病毒名称: Linux.Slapper.Worm 类别: 蠕虫 病毒资料: 感染系统:Linux 不受影响系统: ...

  10. xcode-build/version-bump

    # xcode-build-bump.sh # @desc Auto-increment the build number every time the project is run. # @usag ...