前天晚上的CF比赛div2的E题,很明显一个线段树,当时还在犹豫复杂度的问题,因为他是区间修改和区间查询,肯定是要用到懒惰标记。

然后昨天真的是给这道题跪了,写了好久好久,。。。我本来是写了个add标记作为累加用,因为这个题目很明显是累加型的懒惰标记,但是后来不知道脑子怎么想的,又把add标记给删了,认为只要用一个set标记标注此时该树是否是连续相同的,如果是,则值就存在setv【rt】中,然后就开始了漫长的WA的过程,很明显,我这样是采用了覆盖性的懒惰标记,每次我pushdonw下去 还要先pushdonw下一层的,。。。如果真这么写 绝壁TL,不这么写 肯定WA,。。明显一个累加型的懒惰标记,硬是给我写跪了,其实我后来意识到了要用累加型的,可是就是想沿着这个思路 改好。。最后躺在床上一想,真的只能用累加型的懒惰标记

发现线段树的懒惰标记这一块真的还不熟练,各种容易出错。。。赶紧练

#include <iostream>
#include <cstdio>
#include <cstring>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define LL __int64
using namespace std;
const int N=+;
LL d[N<<],cur[N<<],flag[N<<],add[N<<];
int n,m;
LL abs(LL x){
if (x<) return -x;
return x;
}
void build(int rt,int l,int r)
{
d[rt]=cur[rt]=flag[rt]=add[rt]=;
if (l>=r){
cur[rt]=l;
flag[rt]=;
return;
}
int mid=(l+r)>>;
build(lson);
build(rson);
}
void up(int rt,int l,int r)
{
d[rt]=d[rt<<]+d[rt<<|];
if (flag[rt<<] && flag[rt<<|] && cur[rt<<]==cur[rt<<|]){
cur[rt]=cur[rt<<];
flag[rt]=;
}
else{
flag[rt]=;
}
}
void pushdown(int rt,int l,int r)
{
if (l>=r) return;
int mid=(l+r)>>;
if (add[rt]>){
d[rt<<]+=add[rt]*(mid-l+);
d[rt<<|]+=add[rt]*(r-mid);
add[rt<<]+=add[rt];
add[rt<<|]+=add[rt];
add[rt]=;
}
if (flag[rt]>){
cur[rt<<]=cur[rt<<|]=cur[rt];
flag[rt<<]=flag[rt<<|]=;
}
}
void fix(int L,int R,LL v,int rt,int l,int r)
{
int mid=(l+r)>>;
if (L<=l && r<=R){
if (flag[rt]>){
d[rt]+=abs(v-cur[rt])*(r-l+);
add[rt]+=abs(v-cur[rt]);
cur[rt]=v;
return;
}
}
pushdown(rt,l,r);
if (L<=mid) fix(L,R,v,lson);
if (R>mid) fix(L,R,v,rson);
up(rt,l,r); }
LL query(int L,int R,int rt,int l,int r)
{
//pushdown(rt,l,r);
if (L<=l && r<=R) return d[rt];
int mid=(l+r)>>;
pushdown(rt,l,r);
LL ret1,ret2;
ret1=ret2=;
if (L<=mid) ret1=query(L,R,lson);
if (R>mid) ret2=query(L,R,rson);
return ret1+ret2;
}
int main()
{
int op,a,b;
LL c;
while (scanf("%d%d",&n,&m)!=EOF)
{
build(,,n);
while (m--)
{
scanf("%d",&op);
if (op==){
scanf("%d%d%I64d",&a,&b,&c);
fix(a,b,c,,,n);
}
else{
scanf("%d%d",&a,&b);
LL ans=query(a,b,,,n);
printf("%I64d\n",ans);
}
}
}
return ;
}

Codeforces 444C 线段树 懒惰标记的更多相关文章

  1. Transformation HDU - 4578(线段树——懒惰标记的妙用)

    Yuanfang is puzzled with the question below: There are n integers, a 1, a 2, …, a n. The initial val ...

  2. 【HDU】4092 Nice boat(多校第四场1006) ——线段树 懒惰标记

    Nice boat Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) To ...

  3. CodeForces 444C 线段树

    想分块想了很久一点思路都没有,结果一看都是写的线段树= = ...完全忘记了还有线段树这种操作 题意:给一个数组,一种操作是改变l到r为c,还有一种操作是查询l到r的总和差 线段树记得+lazy标记 ...

  4. DZY Loves Colors CodeForces - 444C (线段树势能分析)

    大意:有$n$个格子, 初始$i$位置的颜色为$i$, 美丽值为0, 有两种操作 将区间$[l,r]$内的元素全部改为$x$, 每个元素的美丽值增加$|x-y|$, $y$为未改动时的值 询问区间$[ ...

  5. hdu-3397 Sequence operation 线段树多种标记

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3397 题目大意: 0 a b表示a-b区间置为0 1 a b表示a-b区间置为1 2 a b表示a- ...

  6. HDU 3468:A Simple Problem with Integers(线段树+延迟标记)

    A Simple Problem with Integers Case Time Limit: 2000MS Description You have N integers, A1, A2, ... ...

  7. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  8. Codeforces 1149C - Tree Generator™(线段树+转化+标记维护)

    Codeforces 题目传送门 & 洛谷题目传送门 首先考虑这个所谓的"括号树"与直径的本质是什么.考虑括号树上两点 \(x,y\),我们不妨用一个"DFS&q ...

  9. Codeforces Round #312 (Div. 2) E. A Simple Task 线段树 延时标记

    E. A Simple Task time limit per test5 seconds memory limit per test512 megabytes inputstandard input ...

随机推荐

  1. NoSQL技术

    NoSQL技术使用场景: 在我们日常的开发中,无不都是使用数据库来进行数据的存储,由于一般的系统任务中通常不会存在高并发的情况,所以这样看起来并没有什么问题,可是一旦涉及大数据量的需求,比如一些商品抢 ...

  2. JdbcTemplate常用方法

    JdbcTemplate简介 JdbcTemplate是Spring JDBC的核心类,借助该类提供的方法可以很方便的实现数据的增删改查. Spring对数据库的操作在jdbc上面做了深层次的封装,使 ...

  3. C++ 类 与 static

    背景 从学习C++到使用现在,发现很多新的东西,正好整理一下. static 为静态,指是当类编译加载的时候,内存就会开辟存储空间的. static 数据成员 在类中,static 可修饰 类中的成员 ...

  4. phpstudy后门漏洞复现php5.2

    前段时间phpstudy被人发现某些版本存在后门,许多人就这样被当作肉鸡长达两年之久 后门隐藏在程序自带的php的php_xmlrpc.dll模块 影响的版本:phpstudy2016和2018 在H ...

  5. 001.CI4框架CodeIgniter的默认访问路径url

    1. 我们解压缩CI4的压缩包,找到app目录,点开Controllers目录,在Home.php文件中,写入我们的如下代码: 002.我们来访问我们的网站 http://127.0.0.1/CI4/ ...

  6. DOM基础2——元素

    1.造元素 document.createElement("标签名") 例:var div_new=document.createElement("div"); ...

  7. Ubuntu下搭建yocto

    参考自: https://www.jianshu.com/p/f6e0debb5e1f https://blog.csdn.net/qq_31041847/article/details/902114 ...

  8. B - Stacks of Flapjacks UVA - 120

    BackgroundStacks and Queues are often considered the bread and butter of data structures and find us ...

  9. golang 读取 chrome保存的网站账号信息

    package main import ( "database/sql" "fmt" "log" "os" " ...

  10. http请求的过程及潜在的性能优化点

    web前端的看富于部署过程 开发者将开发的代码发布到远程的服务器(webserver/cdn),用户通过访问浏览器输入相应的网址,浏览器向远程服务器发送请求,动态的增量式的加载资源 web前端就是一个 ...