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 ...
随机推荐
- sql 导入数据库 出现乱码问题 解决办法 设置 --default-character-set=utf8
mysql -u root -p --default-character-set=utf8 use dbname source /root/newsdata.sql
- Ubuntu搭建Android开发环境
前言 由于迁移到新的笔记本,所以Android开发环境须要又一次配置了.android官网有配置教程,我正好回想一遍 配置Java环境 下载jdk.官网地址:http://www.oracle.com ...
- 【手记】走近科学之为什么JObject不能调用LINQ扩展方法
Json.NET的JObject明明实现了IEnumerable<T>,具体来说是IEnumerable<KeyValuePair<string, JToken>> ...
- Intel processor brand names-Xeon,Core,Pentium,Celeron----Celeron
http://en.wikipedia.org/wiki/Celeron Celeron From Wikipedia, the free encyclopedia Celeron Produ ...
- 手动编译一个c文件(Win7下如何使用GCC编译器)
主要参考这篇http://jingyan.baidu.com/article/c275f6bacc0126e33c756771.html 我没找到minGW的下载地址,而是直接用codeblocks自 ...
- Maple入门使用教程
http://anony3721.blog.163.com/blog/static/51197420105173915247/ 命令的运行:1.每条命令必须用":"(运行后不显示) ...
- python day - 19 抽象类 接口类 多态 封装
一. 抽象类接口类即制定一个规范 特点: 1.不可被实例化. 2.规范子类当中必须事先某个方法. 3.在python中有原生实现抽象类的方法,但没有原生实现接口类的方法. 例题:制定一个规范就是,子类 ...
- 【bzoj3620】似乎在梦中见过的样子
枚举左端点,对于每个右端点处理出以右端点为结尾最大长度使得从左端点开始的前缀等于以右端点结束的后缀,即next数组 然后一直往前跳,直到长度小于子串长度的一半为止. #include<algor ...
- C/C++ 操作符优先级
不能光转贴,有空要熟悉之后,要写点心得.现在发现 . 的优先级确实很高. C: Precedence Operator Description Associativity 1 ++ -- Suffix ...
- Quartz -第一篇-入门
学习地址:https://www.imooc.com/learn/846 官网:www.quartz-scheduler.org 特点:分布式+集群 设计模式: 工厂模式 builder模式 组件模式 ...