前天晚上的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. DotNet中的继承,剖析面向对象中继承的意义

    继承是面向对象程序设计不可缺少的机制,有了继承这个东西,可以提高代码的重用,提高代码的效率,减轻代码员的负担. 面向对象三大特性:封装.继承.多态是相辅相成的.封装为了继承,限制了父类的哪些成员被子类 ...

  2. VUE - 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法

     created() {     var that=this     axios.get('http://jsonplaceholder.typicode.com/todos')     .then( ...

  3. git 创建分支并提交代码

    1.查看所有分支 git branch -a 2.查看当前分支 git branch 3.新建一个分支 git branch feature-xx 4.切换到新建分支上面 git checkout f ...

  4. 连接mysql报错java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized...解决方法

    报错内容: java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents mo ...

  5. idea-plugin-easycode

    1.背景 在练习使用mybatis-generator时候,无意间看到博文esaycode(代码神器),https://www.jianshu.com/p/e4192d7c6844,试验完,感觉这个工 ...

  6. 001.Delphi插件之QPlugins,一个最简单的插件

    安装QPlugins里面的Demo,复制粘贴着写了一个最简单的插件,看看好不好用 EXE代码如下: unit Main_Frm; interface uses Winapi.Windows, Wina ...

  7. JS: 图片轮播模板——左右移动,点击编码移动,自动轮播

    <!DOCTYPE html><html> <head>  <meta charset="UTF-8">  <title> ...

  8. HDU 4960 Handling the past 2014 多校9 线段树

    首先确定的基本思想是按时间离散化后来建线段树,对于每个操作插入到相应的时间点上 但是难就难在那个pop操作,我之前对pop操作的处理是找到离他最近的那个点删掉,但是这样对于后面的peak操作,如果时间 ...

  9. 关于重定向RedirectAttributes的用法

    刚才做项目,遇到了redirectAttributes使用的问题,上网找了找,看到一篇写的很不错的博客,解决我对于RedirectAttributes的困惑,也给大家推荐下. 原文链接:href=&q ...

  10. Linux服务器命令大全

    快捷提示键: table 查看文件夹:  ls , ls –all ,ls –l,ll 进入某个文件夹: cd usr/local 回到root 目录 : cd /root/ 回到根目录:cd / 回 ...