【成端更新线段树模板】POJ3468-A Simple Problem with Integers
http://poj.org/problem?id=3468
_(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说。
【问题描述】
成段加某一个值,然后询问区间和。
【思路】
讲一下pushdown和pushup出现的几个位置。
pushup:
(1)build的结尾,当叶子节点分别有对应的值后,它的父亲们就等于它们求和。
(2)update的结尾,因为此时当前根的左右孩子已经更新了,故它也需要更新。
pushdown(延迟标记):
*pushdown会出现在一切要从当前结点往下的位置
query和update中,要先把add的值传给两个儿子。
接下来整理一下思路,每一个子过程分别需要如何操作:
build(建立线段树)
(1)将add清零
(2)如果左子树等于右子树,则说明已经到达叶子节点,读入sum的值,返回
(3)向左右子树递归
(4)pushup更新当前根
update(修改)
(1)如果当前的L和R包含在l和r中,则让add+δ,让sum+δ*当前区间的长度,返回
(2)pushdown向下延迟标记
(3)向左右子树递归
(4)pushup更新当前根
query(查询)
(1)如果当前的L和R包含在l和r中,则直接返回sum[rt]
(2)pushdown向下延迟标记
(3)向左右子树递归,将得到的返回值加到result总
(4)返回result
一般的线段树就是这样操作的,其实还挺简单(。・∀・)ノ゙最近学业有点忙好久没写程序了,整理一个当作恢复一下手速。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
const int MAXN=+;
int n,q;
LL add[MAXN];
LL sum[MAXN]; void pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
} void build(int l,int r,int rt)
{
add[rt]=;
if (l==r)
{
scanf("%lld",&sum[rt]);
return;
}
int m=(l+r)/;
build(lson);
build(rson);
pushup(rt);
} void pushdown(int rt,int m)
{
if (add[rt])
{
add[rt<<]+=add[rt];
add[rt<<|]+=add[rt];
sum[rt<<]+=add[rt]*(m-(m>>));
sum[rt<<|]+=add[rt]*(m>>);
add[rt]=;
}
} LL query(int L,int R,int l,int r,int rt)
{
if (L<=l && R>=r)
{
return sum[rt];
}
pushdown(rt,r-l+);
int m=(l+r)/;
LL rs=;
if (m>=L) rs+=query(L,R,lson);
if (m<R) rs+=query(L,R,rson);
return rs;
} void update(int L,int R,int delta,int l,int r,int rt)
{
if (L<=l && R>=r)
{
add[rt]+=delta;
sum[rt]+=(LL)delta*(r-l+);
return;
}
pushdown(rt,r-l+);
int m=(l+r)/;
if (m>=L) update(L,R,delta,lson);
if (m<R) update(L,R,delta,rson);
pushup(rt);
} int main()
{
scanf("%d%d",&n,&q);
build(,n,);
for (int i=;i<q;i++)
{
char c[];
scanf("%s",c);
if (c[]=='C')
{
int fr,ed,ad;
scanf("%d%d%d",&fr,&ed,&ad);
update(fr,ed,ad,,n,);
}
else
{
int a,b;
scanf("%d%d",&a,&b);
cout<<query(a,b,,n,)<<endl;
}
}
return ;
}
【成端更新线段树模板】POJ3468-A Simple Problem with Integers的更多相关文章
- 线段树专题 POJ3468 A Simple Problem with Integers
题意:n个点.m个操作.两种操作类型.C X Y K 表示区间[x,y]上每一个点值加k.Q X Y 求区间[x,y]的和 分析:线段树区间求和,裸模板 注意:结果会超int,要用long long ...
- (线段树模板)A Simple Problem with Integers --POJ--3468
链接: http://poj.org/problem?id=3468 代码: #include<stdio.h> #include<algorithm> #include< ...
- POJ 3468 (线段树 区间增减) A Simple Problem with Integers
这题WA了好久,一直以为是lld和I64d的问题,后来发现是自己的pushdown函数写错了,说到底还是因为自己对线段树理解得不好. 因为是懒惰标记,所以只有在区间分开的时候才会将标记往下传递.更新和 ...
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- POJ3468 A Simple Problem with Integers 【段树】+【成段更新】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57666 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- poj3468 A Simple Problem with Integers(zkw区间修改模板)
此题是一道线段树的裸题,这里只是为了保存我的zkw线段树模板 #include <cstdio> #include <cstring> #include <iostrea ...
- poj3468 A Simple Problem with Integers (树状数组做法)
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- poj------(3468)A Simple Problem with Integers(区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 60745 ...
随机推荐
- 项目记录 -- zfs get all [volume] python实现的数据构造
zfs get all [volume]命令实现中构造数据结构 一.zfs get all [volume]命令源代码C实现中用到的数据结构有zprop_get_cbdata 和 callback_d ...
- idea 调试远程tomcat
# ----- Execute The Requested Command ----------------------------------------- JAVA_OPTS="-age ...
- dump_stack 实现分析【转】
转自:http://kernel.meizu.com/2017/03/18-40-19-dump_stack.html 1 简介 说起 dump_stack() ,相信从事 Linux 内核或者驱动相 ...
- java===java基础学习(6)---流程控制,for,if,switch,continue,break
注意点: for循环的用法和python截然不同,注意格式 switch~,switch对应的case每当执行完毕都要break,由于基本不怎么用switch,所以作为了解. 中断流程控制语句,请考虑 ...
- centos rar 文件打开办法
http://hi.baidu.com/nmxiaoxin/item/7642a139918a95677d034b6a Centos下解压rar.zip文件的方法 ============zip文件的 ...
- Tomcat debug模式下特别慢但是run正常处理方法
转载自:http://blog.csdn.net/builderwfy/article/details/50785749 到网上查资料发现这是由eclipse和tomcat交互时,在debug模式启动 ...
- ajax之深入解析(1)
AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML).AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. AJ ...
- HTML5晃动DeviceMotionEvent事件
关于devicemotion html5提供了几个新的DOM事件来获得设备物理方向及运动的信息,包括:陀螺仪.罗盘及加速计. 第一个DOM事件是**deviceorientation**,其提供设 ...
- mybatis 联结查询
一.查询 员工(tbl_employee)时,关联 查询出 员工对于的部门信息 (tab1_dept),一对一查询,或者多对一查询 适用 emp bean里面 包含 部门bean dept属性对象 1 ...
- 深入理解python多进程编程
1.python多进程编程背景 python中的多进程最大的好处就是充分利用多核cpu的资源,不像python中的多线程,受制于GIL的限制,从而只能进行cpu分配,在python的多进程中,适合于所 ...