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 <stack>
using namespace std;
#define LL long long
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
const int MAXN=1e5+10;
LL sum[MAXN<<2];
LL lazy[MAXN<<2];
void PushUp(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void PushDown(int rt, int m)
{
if(lazy[rt])
{
lazy[rt<<1]+=lazy[rt];
lazy[rt<<1|1]+=lazy[rt];
sum[rt<<1]+=lazy[rt]*(m-(m>>1));
sum[rt<<1|1]+=lazy[rt]*(m>>1);
lazy[rt]=0;
}
}
void build(int l, int r, int rt)
{
lazy[rt]=0;
if(l==r)
{
scanf("%lld",&sum[rt]);
return ;
}
int mid=(l+r)>>1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int ll, int rr, int x, int l, int r, int rt)
{
if(ll<=l&&rr>=r)
{
lazy[rt]+=x;
sum[rt]+=(LL)x*(r-l+1);
return ;
}
PushDown(rt, r-l+1);
int mid=(l+r)>>1;
if(ll<=mid) update(ll,rr,x,lson);
if(rr>mid) update(ll,rr,x,rson);
PushUp(rt);
}
LL query(int ll, int rr, int l, int r, int rt)
{
if(ll<=l&&rr>=r)
{
return sum[rt];
}
PushDown(rt,r-l+1);
LL ans=0;
int mid=(l+r)>>1;
if(ll<=mid) ans+=query(ll,rr,lson);
if(rr>mid) ans+=query(ll,rr,rson);
return ans;
}
int main()
{
int n, q, i, a, b, c;
char ch[3];
scanf("%d%d",&n,&q);
build(1,n,1);
while(q--)
{
getchar();
scanf("%s",&ch);
if(ch[0]=='Q')
{
scanf("%d%d",&a,&b);
LL ans=query(a,b,1,n,1);
printf("%lld\n",ans);
}
else
{
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,1,n,1);
}
}
return 0;
}
POJ 3468 A Simple Problem with Integers(线段树区间更新)的更多相关文章
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
- POJ 3468 A Simple Problem with Integers 线段树 区间更新
#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
随机推荐
- 面试中的一些小问题之html5和html4的区别?
HTML5建立的一些新规则: 新特性应该基于HTML.CSS.DOM.JavaScript: 减少对外部插件的需求,如flash将会用video标签和audio标签代替: 更加优秀的错误处理: 更多取 ...
- C++(Typedef声明)
typedef 声明: 使用 typedef 为一个已有的类型取一个新的名字.下面是使用 typedef 定义一个新类型的语法: typedef type newname; 例如,下面的语句会告诉编译 ...
- (转)Hibernate框架基础——一对多关联关系映射
http://blog.csdn.net/yerenyuan_pku/article/details/52746413 上一篇文章Hibernate框架基础——映射集合属性详细讲解的是值类型的集合(即 ...
- 从Element.getElementsByTagName方法说起
一.getElementsByTagName方法: 我们先看几个解释:1)W3C:getElementsByTagName() 方法可返回带有指定标签名的对象的集合.没有说明返回值的具体类型.2)菜鸟 ...
- pringboot开启找回Run Dashboard
代码中加入 <option name="configurationTypes"> <set> <option value="SpringBo ...
- iOS APP EuclidStudy Service Support
Hi,If you have any questions, you can send a message here or send them to us. We will answer you as ...
- P1269 信号放大器
P1269 信号放大器 给一棵有根树,树的边上有距离.根上有一个信号发射器,会发生强度为 h 的信号,信号会往所有的节点传播,然而每经过一条边强度就会削减距离的大小,当信号到达某点时小于 1,则信号传 ...
- LOJ #6009 「网络流 24 题」软件补丁
题面 某公司发现其研制的一个软件中有 $ n $ 个错误,随即为该软件发放了一批共 $ m $ 个补丁程序.每一个补丁程序都有其特定的适用环境,某个补丁只有在软件中包含某些错误而同时又不包含另一些错误 ...
- Luogu P1692 部落卫队
解题思路 数据范围不是很大,那应该不是那些普遍的图论的算法.考虑搜索,用暴力解决.从1到N枚举每一个点的位置,搜索这个点事选还是不选.如果在这个点之前选到的点中又和他冲突的点,那就不选,要么就选. 附 ...
- Gym - 101550A(Artwork 倒序+并查集)
题目: 思路: 1.对输入数据离线,先把所有的黑线都画出来,统计一下剩余的白色连通块的个数,dfs过程将一个连通块放到一个集合中. 2.倒着往前消去黑线,如果当前的块A是白块就看他的四周有没有白块:有 ...