poj3468(线段树)
题目连接:http://poj.org/problem?id=3468
线段树功能:update:成段增减 query:区间求和。
分析:需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define M 1000000000
#define maxn 111111
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
LL sum[maxn<<];
LL col[maxn<<];
void Pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void Pushdown(int rt,int m)
{
if(col[rt])
{
col[rt<<]+=col[rt];
col[rt<<|]+=col[rt];
sum[rt<<]+=col[rt]*(m-(m>>));
sum[rt<<|]+=col[rt]*(m>>);
col[rt]=;
}
}
void build(int l,int r,int rt)
{
col[rt]=;
if(l==r)
{
scanf("%lld",&sum[rt]);
return;
}
int m=(l+r)>>;
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)
{
col[rt]+=c;
sum[rt]+=(LL)(r-l+)*c;
return;
}
Pushdown(rt,r-l+);
int m=(l+r)>>;
if(L<=m)update(L,R,c,lson);
if(m<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];
Pushdown(rt,r-l+);
int m=(r+l)>>;
LL res=;
if(L<=m)res+=query(L,R,lson);
if(R>m)res+=query(L,R,rson);
return res;
}
int main()
{
int n,m;
char op[];
while(scanf("%d%d",&n,&m)>)
{
build(,n,);
while(m--)
{
int a,b,c;
scanf("%s",op);
if(op[]=='Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",query(a,b,,n,));
}
else
{
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,,n,);
}
}
}
return ;
}
poj3468(线段树)的更多相关文章
- poj3468 线段树的懒惰标记
题目链接:poj3468 题意:给定一段数组,有两种操作,一种是给某段区间加c,另一种是查询一段区间的和 思路:暴力的方法是每次都给这段区间的点加c,查询也遍历一遍区间,复杂度是n*n,肯定过不去,另 ...
- POJ3468 线段树(区间更新,区间求和,延迟标记)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 97196 ...
- poj3468 线段树+lazy标记
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92921 ...
- POJ3468(线段树 区间修改 lazy-tag)
我的线段树真的没救了......还是多练几道吧....... You have N integers, A1, A2, ... , AN. You need to deal with two kind ...
- POJ3468(线段树区间维护)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 85502 ...
- poj3468(线段树区间更新&区间求和模板)
题目链接: http://poj.org/problem?id=3468 题意: 输入 n, m表初始有 n 个数, 接下来 m 行输入, Q x y 表示询问区间 [x, y]的和: C x y z ...
- POJ-3468(线段树+区间更新+区间查询)
A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是up ...
- poj3468(线段树 边覆盖)
#include<cstdio> int lb,rb,data; long long sum[5000000],extra[5000000]; void add(int l,int r,i ...
- poj3468线段树标记永久化
#include<map> #include<set> #include<list> #include<cmath> #include<queue ...
- poj3468 A Simple Problem with Integers(线段树区间更新)
https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...
随机推荐
- perl lwp编码
$var= $response->content; $var= $response->decoded_content;
- perl eval函数
29.2.32 eval • eval BLOCK • eval EXPR • eval eval 关键字在Perl 里起两种不同的但相关的作用.这些目的是用两种形式的语法 来表现的, eval BL ...
- 基于visual Studio2013解决面试题之0608找出两个只出现一次的数
题目
- 如何在SourceInsight中选中匹配的大括号中的内容
如何在SourceInsight中选中匹配的大括号中的内容 要分析的代码很长,多个for,if等分析嵌套在一起,代码有点乱,找到了这个分支的头,却不知道尾在哪,找到了尾却不知道哪是开头,在网上找了一下 ...
- 语音信号处理之(三)矢量量化(Vector Quantization)
语音信号处理之(三)矢量量化(Vector Quantization) zouxy09@qq.com http://blog.csdn.net/zouxy09 这学期有<语音信号处理>这门 ...
- IOS 应用的架构解析
首先新建一个IOS 的应用工程,主要讲解一下的文件组成: main.m XXXXDelegete.h\.m MainWindow.xib info.plist 文件 IOS 应用程序由UIKit 封装 ...
- UVA 10494-If We Were a Child Again(一流的塔尔苏斯)
Problem C If We Were a Child Again Input: standard input Output: standard output seconds "Oo ...
- EL表达式(3)
本篇讲解使用EL表达式来调用Java方法(自定义EL函数)和Sun公司开发的EL函数库. 简单来说,我们在一个类中的某个方法,可以使用EL进行调用,这个能被EL表达式调用的方法称之为EL函数,但是这种 ...
- CSipSimple最新版本号
要使用CSipSimple有两种方法:第一种是不编译jni,另外一种是编译jni. 这里介绍的是第一种:不编译jni. 首先,用SVNclient检出CSipSimple源代码:svn checkou ...
- OWAP Top 10
2013 Top 10 List A1-Injection Injection flaws, such as SQL, OS, and LDAP injection occur when untr ...