POJ3468【线段树lazy操作】
上午理论AC,打到现在快吐了。。。
一个那么**Lazy操作打成这样,query操作和update操作都有问题,妈蛋,发现是mid<=s+1…真是蠢到家,明明是mid+1<=s卧槽连左和右都分不清。。。
什么是lazy?
lazy做法:
查询一个区间,如果这个节点的区间正好是满足,那么直接返回,眼睛都不眨一下,如果不是,就要让根的标志和他的子节点搞一搞,然后继续分下去,判断与mid的关系,因为是从根节点下去,所以他肯定是满足区间的,只是怎么分的问题,有两个是区间变小,分别是查询区间完全在mid右边和左边,还有就是mid就在区间里面那么切一切就好了。同理更新啊。。。。。。。。。瞎瘠薄搞吧,巨巨,画个树,然后拿各种区间,分分看就知道了。
贴上我的挫code………………..
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int INF=-0x3f3f3f3f;
const int N=100000;
struct st{
LL left,right;
LL sum;
LL val; //对该节点的下一层子节点而言的,不是对于其本身,所以其本身要本来就加上了这个value;
};
st q[N*4];
LL n;
int m;
void build(int num,LL L,LL R)
{
q[num].left=L;
q[num].right=R;
q[num].val=0;
if(L==R)
{
scanf("%I64d",&q[num].sum);
return;
}
build(2*num,L,(L+R)/2);
build(2*num+1,(L+R)/2+1,R);
q[num].sum=q[2*num].sum+q[2*num+1].sum;
}
void update(int num,LL s,LL t,LL x)
{
if(q[num].left==s&&q[num].right==t)
{
q[num].val+=x;
q[num].sum+=x*(q[num].right-q[num].left+1);
return;
}
if(q[num].left==q[num].right) return;
if(q[num].val)
{
q[2*num].val+=q[num].val;
q[2*num].sum+=q[num].val*(q[2*num].right-q[2*num].left+1);
q[2*num+1].val+=q[num].val;
q[2*num+1].sum+=q[num].val*(q[2*num+1].right-q[2*num+1].left+1);
q[num].val=0;
}
LL mid=(q[num].left+q[num].right)/2;
if(mid>=t)
update(2*num,s,t,x);
else if(mid+1<=s)
update(2*num+1,s,t,x);
else{
update(2*num,s,mid,x);
update(2*num+1,mid+1,t,x);
}
q[num].sum=q[2*num].sum+q[2*num+1].sum;
}
LL query(int num,LL s,LL t)
{
if(q[num].left==s&&q[num].right==t)
return q[num].sum;
if(q[num].val)
{
q[2*num].val+=q[num].val;
q[2*num].sum+=q[num].val*(q[2*num].right-q[2*num].left+1);
q[2*num+1].val+=q[num].val;
q[2*num+1].sum+=q[num].val*(q[2*num+1].right-q[2*num+1].left+1);
q[num].val=0;
}
LL ans=0;
LL mid=(q[num].left+q[num].right)/2;
if(mid>=t)
ans+=query(2*num,s,t);
else if(mid+1<=s)
ans+=query(2*num+1,s,t);
else{
ans+=query(2*num,s,mid);
ans+=query(2*num+1,mid+1,t);
}
return ans;
}
int main()
{
scanf("%I64d%d",&n,&m);
build(1,1,n);
while(m--)
{
char ss[5];
LL a,b,c;
scanf("%s",ss);
if(strcmp(ss,"Q")==0)
{
scanf("%I64d%I64d",&a,&b);
printf("%I64d\n",query(1,a,b));
}
else{
scanf("%I64d%I64d%I64d",&a,&b,&c);
update(1,a,b,c);
}
}
return 0;
}
POJ3468【线段树lazy操作】的更多相关文章
- poj3468 线段树+lazy标记
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92921 ...
- HDU 3954 Level up(多颗线段树+lazy操作)
又是一开始觉得的水题,结果GG了好久的东西... 题意是给你n个英雄,每个英雄开始为1级经验为0,最多可以升到k级并且经验一直叠加,每一级都有一个经验值上限,达到就升级.接着给你两种操作:W li r ...
- poj 3237 树链剖分模板(用到线段树lazy操作)
/* 本体在spoj375的基础上加了一些操作,用到线段树的lazy操作模板类型 */ #include<stdio.h> #include<string.h> #includ ...
- hdoj1698【线段树Lazy操作】
区间更新lazy操作一发. #include<cstdio> #include<iostream> #include<string.h> #include<a ...
- 分块+lazy 或者 线段树+lazy Codeforces Round #254 (Div. 2) E
E. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 线段树+lazy标记 2019年8月10日计蒜客联盟周赛 C.小A的题
题目链接:https://nanti.jisuanke.com/t/40852 题意:给定一个01串s,进行m次操作,|s|<=1e6,m<=5e5 操作有两种 l r 0,区间[l,r] ...
- POJ 2777——线段树Lazy的重要性
POJ 2777 Count Color --线段树Lazy的重要性 原题 链接:http://poj.org/problem?id=2777 Count Color Time Limit: 1000 ...
- JuQueen(线段树 lazy)
JuQueen Time Limit: 5 Sec Memory Limit: 512 MB Description Input Output Sample Input 10 10 5 state ...
- hdu 2871 线段树(各种操作)
Memory Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
随机推荐
- centos 7 -- Disk Requirements: At least 134MB more space needed on the / filesystem.
用了幾年的centos7,今天執行yum update時,彈出一行有錯誤的提示:Disk Requirements: At least 134MB more space needed on the ...
- 关于 thinkPHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback
Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback 关于thinkPHP rpc调 ...
- Cocos2d-x 3.0 简捷的物理引擎
Cocos2d-x 3.0 开发(九)使用Physicals取代Box2D和chipmunk http://www.cocos2d-x.org/docs/manual/framework/native ...
- com.sun.jdi.InvocationException occurred invoking method 异常
参考链接:https://stackoverflow.com/questions/4123628/com-sun-jdi-invocationexception-occurred-invoking-m ...
- C#json数据的序列化和反序列化(将数据转换为对象或对象集合)
引用 System.Runtime.Serialization.Json
- Oracle RAC cache fusion原理測试
Oracle RAC cache fusion是RAC最核心的工作机制.他把全部实例的SGA虚拟成一个大的SGA区,每当不同的实例请求同样的数据块,这个数据块就须要在实例间进行传递. 那究竟什么时候传 ...
- iOS开发 - App程序启动原理
Info.plist和pch文件的作用 建立一个project后,会在Supporting files目录下看到一个"project名-Info.plist"的文件,该文件对pro ...
- openwrt gstreamer实例学习笔记(三.深入了解gstreamer 的 Element)
在前面的部分,我们简要介绍过 GstElementFactory 可以用来创建一个element的实例,但是GstElementFactory不仅仅只能做这件事,GstElementFactory作为 ...
- 阿里云安装nginx 启动失败的原因。
阿里云编译安装nginx服务器后启动一直报下面错误. 百度了一圈,看到一个说要先关掉apache服务,感觉这个好像是对的,立马做了下面操作. 果然把nginx起了起来. 从这边才知道apache和ng ...
- C语言restrict关键字的使用----可以用来优化代码
C99中新增加了restrict修饰的指针:由restrict修饰的指针是最初唯一对指针所指向的对象进行存取的方法,仅当第二个指针基于第一个时,才能对对象进行存取.对对象的存取都限定于基于由restr ...