又来练线段树了……

poj1823题意很简单(明显的数据结构题),区间修改和统计最长连续空区间;

有了poj3468的基础,区间修改不是什么问题了,重点是求最长连续空区间;(弱弱的我纠结了好久)

在每个节点上增加3个域,lmax,rmax,maxx,分别表示从左起最长,右起最长,区间内最长,然后稍稍动点脑筋即可……

 type node=record
       lmax,rmax,maxx:longint;
       l,r,lazy:integer;   //lazy表示区间三种情况,-表示区间内有人住但未满,表示无人住,表示全住满
     end;
var tree:array[..] of node;
    i,n,m,a,b,c,j:longint;
procedure updata(i:longint);     //更新
  var p:longint;
  begin
    if tree[i].lazy<>- then
    begin
      if tree[i].lazy= then p:=tree[i].r-tree[i].l+ else p:=;
      tree[i].lmax:=p;
      tree[i].rmax:=p;
      tree[i].maxx:=p;
    end
    else begin        //看起来很繁琐,实际上仔细想想就明白了
      tree[i].lmax:=tree[i*].lmax;
      if tree[i].lmax=tree[i*].r-tree[i*].l+ then tree[i].lmax:=tree[i].lmax+tree[i*+].lmax;
      tree[i].rmax:=tree[i*+].rmax;
      if tree[i].rmax=tree[i*+].r-tree[i*+].l+ then tree[i].rmax:=tree[i].rmax+tree[i*].rmax;
      p:=max(tree[i].lmax,tree[i].rmax);
      p:=max(p,max(tree[i*].maxx,tree[i*+].maxx));
      tree[i].maxx:=max(p,tree[i*].rmax+tree[i*+].lmax);
    end;
  end; procedure pushdown(i:longint);  //lazy思想,注意标记下移的时候不忘更新,因为某个子区间不一定会被访问
  begin
    tree[i*].lazy:=tree[i].lazy;
    updata(i*);
    tree[i*+].lazy:=tree[i].lazy;
    updata(i*+);
    tree[i].lazy:=-;    //需要标记下移的时候当前区间一定不会是全满或全空(想想为什么)
  end; procedure build(i,l,r:longint);  //初始化线段树
  var m:longint;
  begin
    tree[i].l:=l; tree[i].r:=r;
    tree[i].lazy:=;
    updata(i);
    if l<>r then
    begin
      m:=(l+r) div ;
      build(i*,l,m);
      build(i*+,m+,r);
    end;
  end; procedure work(i,l,r,t:longint);
  var m:longint;
  begin
    if (l>=a) and (r<=b) then
    begin
      tree[i].lazy:=t;
      updata(i);
    end
    else if l<>r then begin
      if tree[i].lazy<>- then pushdown(i); 
      m:=(l+r) div ;
      if (a<=m) then work(i*,l,m,t);
      if (b>=m+) then work(i*+,m+,r,t);
      updata(i);   // 左右子区间访问过后更新当前区间
    end;
  end;
begin
  readln(n,m);
  build(,,n);
  for i:= to m do
  begin
    read(c);
    if c<> then
    begin
      readln(a,b);
      b:=a+b-;
      if c= then work(,,n,) else work(,,n,);  //代表入住,代表退房
    end
    else if c= then writeln(tree[].maxx);
  end;
end.

poj1823

话说线段树查起来真累(难道还是我太渣了?),耗了5次才AC

poj3667在poj1823基础上多了个查找但并不难,对于当前区间,按照从左区间到右区间的优先顺序找即可,一次AC

 type node=record
l,r,lmax,rmax,maxx:longint;
lazy:integer;
end; var tree:array[..] of node;
i,n,m,a,b,c,j,l:longint;
function max(a,b:longint):longint;
begin
if a>b then max:=a else max:=b;
end; function min(a,b:longint):longint;
begin
if a= then exit(b);
if b= then exit(a);
if a>b then exit(b) else exit(a);
end;
procedure updata(i:longint);
var p:longint;
begin
if tree[i].lazy<>- then
begin
if tree[i].lazy= then p:=tree[i].r-tree[i].l+ else p:=;
tree[i].lmax:=p;
tree[i].rmax:=p;
tree[i].maxx:=p;
end
else begin
tree[i].lmax:=tree[i*].lmax;
if tree[i].lmax=tree[i*].r-tree[i*].l+ then tree[i].lmax:=tree[i].lmax+tree[i*+].lmax;
tree[i].rmax:=tree[i*+].rmax;
if tree[i].rmax=tree[i*+].r-tree[i*+].l+ then tree[i].rmax:=tree[i].rmax+tree[i*].rmax;
p:=max(tree[i].lmax,tree[i].rmax);
p:=max(p,max(tree[i*].maxx,tree[i*+].maxx));
tree[i].maxx:=max(p,tree[i*].rmax+tree[i*+].lmax);
end;
end; procedure pushdown(i:longint);
begin
tree[i*].lazy:=tree[i].lazy;
updata(i*);
tree[i*+].lazy:=tree[i].lazy;
updata(i*+);
tree[i].lazy:=-;
end; procedure find(i:longint);
begin
if tree[i].maxx<l then exit;
if (tree[i].lmax>=l) then a:=tree[i].l
else if tree[*i].maxx>=l then
find(*i)
else if tree[i*].rmax+tree[i*+].lmax>=l then
begin
a:=tree[i*].r-tree[i*].rmax+;
end
else if tree[i*+].maxx>=l then find(*i+);
if tree[i].rmax>=l then a:=min(a,tree[i].r-tree[i].rmax+);
end; procedure build(i,l,r:longint);
var m:longint;
begin
tree[i].l:=l; tree[i].r:=r;
tree[i].lazy:=;
updata(i);
if l<>r then
begin
m:=(l+r) div ;
build(i*,l,m);
build(i*+,m+,r);
end;
end; procedure work(i,l,r,t:longint);
var m:longint;
begin
if (l>=a) and (r<=b) then
begin
tree[i].lazy:=t;
updata(i);
end
else if l<>r then begin
if tree[i].lazy<>- then pushdown(i);
m:=(l+r) div ;
if (a<=m) then work(i*,l,m,t);
if (b>=m+) then work(i*+,m+,r,t);
updata(i);
end;
end; begin
readln(n,m);
build(,,n);
for i:= to m do
begin
read(c);
if c= then
begin
readln(l);
a:=;
find();
writeln(a);
b:=a+l-;
if a<> then work(,,n,);
end
else if c= then
begin
readln(a,b);
b:=a+b-;
work(,,n,);
end;
end;
end.

poj3667

poj1823,3667的更多相关文章

  1. 线段树(区间合并) POJ 3667 Hotel

    题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...

  2. POJ 3667 Hotel (线段树区间合并)

    题目链接:http://poj.org/problem?id=3667 最初给你n间空房,m个操作: 操作1 a 表示检查是否有连续的a间空房,输出最左边的空房编号,并入住a间房间. 操作2 a b ...

  3. poj 3667 Hotel (线段树)

    http://poj.org/problem?id=3667 Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 94 ...

  4. HDU 3667.Transportation 最小费用流

    Transportation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. BZOJ.3667.Rabin-Miller算法(MillerRabin PollardRho)

    题目链接 Pollard_Rho:http://blog.csdn.net/thy_asdf/article/details/51347390 #include<cstdio> #incl ...

  6. POJ 3667 Hotel(线段树+区间合并)

    http://poj.org/problem?id=3667 题意: 有N个房间,M次操作.有两种操作(1)"1a",表示找到连续的长度为a的空房间,如果有多解,优先左边的,即表示 ...

  7. 【刷题】BZOJ 3667 Rabin-Miller算法

    Input 第一行:CAS,代表数据组数(不大于350),以下CAS行,每行一个数字,保证在64位长整形范围内,并且没有负数.你需要对于每个数字:第一,检验是否是质数,是质数就输出Prime 第二,如 ...

  8. HDU 3667

    http://acm.hdu.edu.cn/showproblem.php?pid=3667 最小费用最大流 本题流量和费用不是线性关系,fee=a*flow*flow,所以常规套模板spfa无法得到 ...

  9. HDU 3667 Transportation(网络流之费用流)

    题目地址:HDU 3667 这题的建图真是巧妙...为了保证流量正好达到k.须要让每一次增广到的流量都是1,这就须要把每一条边的流量都是1才行.可是每条边的流量并非1,该怎么办呢.这个时候能够拆边,反 ...

随机推荐

  1. foxtable使用笔记

    1.设置关联表窗口的列的顺序:Tables("award.people").SetColVisibleWidth("name|100|sex|100|job|100|ag ...

  2. RTC搭建android下三层应用程序访问服务器MsSql-服务器端

    前几天通过Ro搭建webservice,然后在android下调用,虽然已近成功,但是返回的数据库里的中文有乱码一直未得到解决!rtc6.23版本,已经支持xe5,也支持fmx的android下开发, ...

  3. c# 代理IP获取通用方法

    调用: ConcurrentQueue<string> proxyIpQueue = new ConcurrentQueue<string>(); Grab_ProxyIp(p ...

  4. Custom Control

    How to create custom control http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Cont ...

  5. 零基础学Python 3之环境准备

    一. 配置python 3 环境 1. Python 3 下载 64位 https://www.python.org/ftp/python/3.4.2/python-3.4.2.amd64.msi 3 ...

  6. 微软职位内部推荐-SDE

    微软近期Open的职位: Organization Summary:Engineering, Community & Online (ECO) is looking for a great & ...

  7. python学习笔记25(文件管理 os包)

    os包我们经常会与文件和目录打交道,对于这些操作python提供了一个os模块,里面包含了很多操作文件和目录的函数.如果你对linux基本操作了解的话,下面的一些os方法应该会很熟悉的,因为基本和li ...

  8. jmeter测试本地myeclips调试状态下的tomcat程序死锁

    在myeclipse调试状态下的tomcat程序,用jmeter测试,居然发生死锁,调试两天无果,直接运行tomcat而不通过myeclipse,无死锁,真是又好气又好笑..

  9. why you write code so slow.

    今天我们要写一个日历表,用以存储所有的节假日. 虽然这个表设计的并不是很妙.但是将就着继续了. 让小弟把该表数据初始化3-5年的,结果一上午还没有出来,着急了,自己写了一个初始化的工具. 分享出来. ...

  10. vncserver 添加用户

    1.在vncserver设置登录用户的信息   #vi /etc/sysconfig/vncservers   VNCSERVERS="1:root 2:wt" 此处添加用户   ...