线段树初步

 

线段树模板1:https://www.luogu.org/problem/show?pid=3372

线段树模板2:https://www.luogu.org/problem/show?pid=3373

这些都比较基础,就是1或2个lazy标记的时候怎么处理?几乎不用考虑兼容性的问题。

现在这里有一道充分考验线段树lazy的兼容性问题的题目,涉及到4个lazy标记,怎么处理?

例子1:求线段树维护一个区间,支持如下操作:区间修改为同一个值(更改1),区间加一个数(更改2),区间和(运算1),区间最大值(运算2):

解析:首先,不考虑lazy标记的兼容性是不行的,需要注意的是区间修改和区间和,区间最大值是不兼容的,所以在lazy时需要特判!!!

还有需要注意的点非常多,需要注意,

//本程序经过oycy0306测试正确性保障++
uses math;
const maxn=;
inf=;
type rec=record
add,mk:longint;
end;
var n,m,i,opx,opr,opl,ch,ans:longint;
f,ff,a:array[..maxn]of longint;
s:array[..maxn]of rec;
procedure build(x,l,r:longint);
var m:longint;
begin
s[x].mk:=inf;
s[x].add:=;
if l=r then begin
ff[x]:=a[l];
f[x]:=a[l];
exit;
end;
m:=(l+r)>>;
build(*x,l,m);
build(*x+,m+,r);
f[x]:=f[*x]+f[*x+];
ff[x]:=max(ff[*x],ff[*x+]);
end;
procedure down(x,l,r,m,lson,rson:longint);
begin
if s[x].mk<>inf then begin //**
s[lson].mk:=s[x].mk;
s[rson].mk:=s[x].mk;
s[lson].add:=;
s[rson].add:=;
f[lson]:=opx*(m-l+);
f[rson]:=opx*(r-m);
ff[lson]:=opx;
ff[rson]:=opx;
s[x].mk:=inf;
s[x].add:=;
exit;
end;
s[lson].mk:=inf;
s[rson].mk:=inf;
s[lson].add:=s[lson].add+s[x].add;
s[rson].add:=s[rson].add+s[x].add;
f[lson]:=f[lson]+s[x].add*(m-l+);
f[rson]:=f[rson]+s[x].add*(r-m);
ff[lson]:=ff[lson]+s[x].add;
ff[rson]:=ff[lson]+s[x].add;
s[x].add:=;
s[x].mk:=inf; //**
end;
procedure calc(x,l,r:longint);
var m:longint;
begin
if (opl<=l)and(opr>=r) then begin
case ch of
:begin s[x].mk:=opx; s[x].add:=; f[x]:=opx*(r-l+); ff[x]:=opx; end;
:begin s[x].add:=s[x].add+opx; f[x]:=f[x]+opx*(r-l+); ff[x]:=ff[x]+opx; end;
:begin ans:=ans+f[x]; end; //f:sum
:begin ans:=max(ff[x],ans)end; //ff:mk
end;
exit;
end;
m:=(l+r)>>;
if (s[x].mk<>inf)or(s[x].add>)then down(x,l,r,m,*x,*x+);
if opl<=m then calc(*x,l,m);
if opr>m then calc(*x+,m+,r);
if (ch=)or(ch=) then begin
f[x]:=f[*x]+f[*x+];
ff[x]:=max(ff[*x],ff[*x+])
end;
end;
begin
writeln('input nodenum n=??'); readln(n);
writeln('input a num(n) sequence called a[]==??');
for i:= to n do read(a[i]);
write('the strat sequence a[]==');
for i:= to n do write(a[i],' ');writeln;
writeln('---------------------------');
writeln('start to build XD tree.');
writeln('---------------------------');
build(,,n);
writeln('---------------------------');
writeln('XD tree is already built.');
writeln('---------------------------');
writeln('input your instructions number!'); //build ok!
readln(m); writeln('OK.');
writeln('---------------------------');
writeln('1=modify');
writeln('2=ADD');
writeln('3=question sum');
writeln('4=max in the a[]');
writeln('a line a word.');
writeln('instruction+ +minl+ +maxr+ (+valuable)');
writeln('---------------------------');
for i:= to m do begin
writeln('instruction input ',i,' :');
read(ch,opl,opr);
if (ch=)or(ch=) then readln else readln(opx);
case ch of
,:begin writeln('instruction output ',i,':'); writeln('Nothing except the instruction is running over.');calc(,,n); end;
,:begin ans:=; calc(,,n); writeln('instruction output ',i,' :'); writeln(ans); end;
end;
end;
end.

 例子2:求线段树维护一个区间,支持如下操作:区间修改为同一个值(更改1),区间加一个数(更改2),区间乘一个数(更该3),区间和(运算1),区间最大值(运算2):、

对于例子一又多了一个区间乘法,所以就有5个lazy标记了...

所以这个down函数比较的长; 具体不解释了,就是在例子1上增加,看代码:

uses math;
const maxn=;
inf=;
type rec=record
add,mk,mp:longint;
end;
var n,m,i,opx,opr,opl,ch,ans:longint;
f,ff,a:array[..maxn]of longint;
s:array[..maxn]of rec;
procedure build(x,l,r:longint);
var m:longint;
begin
s[x].mk:=inf;
s[x].add:=;
s[x].mp:=;
if l=r then begin
ff[x]:=a[l];
f[x]:=a[l];
// s[x].mx:=a[l];
exit;
end;
m:=(l+r)>>;
build(*x,l,m);
build(*x+,m+,r);
f[x]:=f[*x]+f[*x+];
ff[x]:=max(ff[*x],ff[*x+]);
end;
procedure down(x,l,r,m,lson,rson:longint);
begin
if s[x].mk<>inf then begin //**
s[lson].mk:=s[x].mk;
s[rson].mk:=s[x].mk;
s[lson].add:=;
s[rson].add:=;
s[lson].mp:=;
s[rson].mp:=;
f[lson]:=opx*(m-l+);
f[rson]:=opx*(r-m);
ff[lson]:=opx;
ff[rson]:=opx;
s[x].mk:=inf;
s[x].add:=;
s[x].mp:=;
exit;
end;
s[lson].mp:=s[lson].mp*s[x].mp;
s[rson].mp:=s[rson].mp*s[x].mp;
s[lson].add:=s[lson].add*s[x].mp;
s[rson].add:=s[rson].add*s[x].mp;
f[lson]:=f[lson]*s[x].mp;
f[rson]:=f[rson]*s[x].mp;
ff[lson]:=ff[lson]*s[x].mp;
ff[rson]:=ff[lson]*s[x].mp;
s[x].mp:=;
s[lson].mk:=inf;
s[rson].mk:=inf;
s[lson].add:=s[lson].add+s[x].add;
s[rson].add:=s[rson].add+s[x].add;
f[lson]:=f[lson]+s[x].add*(m-l+);
f[rson]:=f[rson]+s[x].add*(r-m);
ff[lson]:=ff[lson]+s[x].add;
ff[rson]:=ff[lson]+s[x].add;
s[x].add:=;
s[x].mk:=inf; //**
end;
procedure calc(x,l,r:longint);
var m:longint;
begin
if (opl<=l)and(opr>=r) then begin
case ch of
:begin s[x].mk:=opx; s[x].add:=; f[x]:=opx*(r-l+); ff[x]:=opx; end;
:begin s[x].add:=s[x].add+opx; f[x]:=f[x]+opx*(r-l+); ff[x]:=ff[x]+opx; end;
:begin ans:=ans+f[x]; end; //f:sum
:begin ans:=max(ff[x],ans)end; //ff:mk
:begin s[x].mp:=s[x].mp*opx; s[x].add:=s[x].add*opx; f[x]:=f[x]*opx; ff[x]:=ff[x]*opx; end;
end;
exit;
end;
m:=(l+r)>>;
if (s[x].mk<>inf)or(s[x].add>)or(s[x].mp<>)then down(x,l,r,m,*x,*x+);
if opl<=m then calc(*x,l,m);
if opr>m then calc(*x+,m+,r);
if (ch=)or(ch=) then begin
f[x]:=f[*x]+f[*x+];
ff[x]:=max(ff[*x],ff[*x+])
end;
end;
begin
writeln('input nodenum n=??'); readln(n);
writeln('input a num(n) sequence called a[]==??');
for i:= to n do read(a[i]);
write('the strat sequence a[]==');
for i:= to n do write(a[i],' ');writeln;
writeln('---------------------------');
writeln('start to build XD tree.');
writeln('---------------------------');
build(,,n);
writeln('---------------------------');
writeln('XD tree is already built.');
writeln('---------------------------');
writeln('input your instructions number!'); //build ok!
readln(m); writeln('OK.');
writeln('---------------------------');
writeln('1=modify');
writeln('2=ADD');
writeln('3=question sum');
writeln('4=max in the a[]');
writeln('5=multiplication');
writeln('a line a word.');
writeln('instruction+ +minl+ +maxr+ (+valuable)');
writeln('---------------------------');
for i:= to m do begin
writeln('instruction input ',i,' :');
read(ch,opl,opr);
if (ch=)or(ch=) then readln else readln(opx);
case ch of
,,:begin calc(,,n); writeln('instruction output ',i,':'); writeln('Nothing except the instruction is running over.'); end;
,:begin ans:=; calc(,,n); writeln('instruction output ',i,' :'); writeln(ans); end;
end;
end;
end.
 
 

线段树初步——转载自ljc20020730的更多相关文章

  1. 线段树初步&&lazy标记

    线段树 一.概述: 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点. 对于线段树中的每一个非叶子节点[a,b],它的左儿子表示的区间为[a, ...

  2. 线段树初步__ZERO__.

    线段树,顾名思义,就是指一个个线段组成的树. 线段树的定义就是: 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点.使用线段树可以快速的查找某 ...

  3. 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)

    转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...

  4. [转载]完全版线段树 by notonlysuccess大牛

    原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等 ...

  5. 线段树详解 (原理,实现与应用)(转载自:http://blog.csdn.net/zearot/article/details/48299459)

    原文地址:http://blog.csdn.net/zearot/article/details/48299459(如有侵权,请联系博主,立即删除.) 线段树详解    By 岩之痕 目录: 一:综述 ...

  6. 一步一步理解线段树——转载自JustDoIT

    一步一步理解线段树   目录 一.概述 二.从一个例子理解线段树 创建线段树 线段树区间查询 单节点更新 区间更新 三.线段树实战 -------------------------- 一 概述 线段 ...

  7. 【转载】完全版线段树 by notonlysuccess大牛

    原文出处:http://www.notonlysuccess.com/ 今晚上比赛就考到了 排兵布阵啊,难受. [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时 ...

  8. Tido c++线段树知识讲解(转载)

    线段树知识讲解 定义.建树.单点修改.区间查询         特别声明:如上的讲解说的是区间最大值 如果想要查询区间和 只需要改变一下建树和查询的代码就行了,如下 其他根据自己的需要进行修改即可

  9. 线段树离散化 unique + 二分查找 模板 (转载)

    离散化,把无限空间中有限的个体映射到有限的空间中去,以此提高算法的时空效率. 通俗的说,离散化是在不改变数据相对大小的条件下,对数据进行相应的缩小.例如: 原数据:1,999,100000,15:处理 ...

随机推荐

  1. PostgreSQL查看表、表索引、视图、表结构以及参数设置

    -- 表索引select * from pg_indexes where tablename='person_wechat_label';select * from pg_statio_all_ind ...

  2. .Net面试题二

    谈谈创建线程的方式 1.列举.Net页面之间传值的方式 2..Net中aspx页面从客户端浏览器开始请求到服务器返回响应所经历的过程 CLR主要运行过程 ASP.NET运行管道所有事件 3.如何理解委 ...

  3. qt 静态编译配置项

    configure -confirm-license -opensource -platform win32-msvc2013 -debug-and-release -static -prefix & ...

  4. UDP通信简单 小结

    Android手机版和电脑版 效果图: 通过WiFi局域网 电脑和手机连接通信. 电脑版本和手机版本使用了相同的消息发送头协议, 可以相互接收消息. 若有做的不好的地方还希望大家指导一下. 1. 手机 ...

  5. MFC + XToolKit的使用 ( 亲自实践 )

    1. 变量声明: 在Dlg.h    的public下 CXTPButton m_Button2; 2. 变量交换并设置按钮风格: Dlg.cpp下 void CXT_VS2010Dlg::DoDat ...

  6. 【ExtJs】在Ext.grid.Panel中,两列的值相乘作为第三列的值的实现

    如: 商品总价=商品单价*商品数量 方法: 商品总价列,使用其renderer属性,为期定义一个方法,该方法将当前record中的另外两列中2个数据相乘后渲染到该商品总价列.

  7. vue 超大 table

    https://github.com/ColdDay/vue-fast-table https://coldday.github.io/vue-fast-table/dist/index.html h ...

  8. CentOS 7.6 64位安装docker并设置开机启动

    步骤如下 安装docker.docker-compose yum -y install docker-io docker-compose 启动docker service docker start 设 ...

  9. tensorboardX使用中 AttributeError: 'function' object has no attribute 'graph'

    最近在使用tensorboardX可视化网络结构,因为tensorboardX并非pytorch原生的可视化工具,所以版本之间并不兼容 在使用的过程中会遇到,AttributeError: 'func ...

  10. odoo 字段组件

    每个字段类型都会使用相应的默认组件在表单中显示.但还有一些替代组件可以使用.对于文本字段,有如下组件: email用于让 email 文本成为可操作的”mail-to”地址 url用于将文本格式化为可 ...