又来练线段树了……

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. [PHP]set_time_limit — 设置脚本最大执行时间

    (PHP 4, PHP 5) set_time_limit — 设置脚本最大执行时间 说明 void set_time_limit ( int $seconds ) 设置允许脚本运行的时间,单位为秒. ...

  2. YII千万级PV架构经验分享--俯瞰篇--性能介绍

    一张图,啥也不说了.直接上图,大图真难画. 呃,非得写满二百个字,其实本来想画均衡负债,一些服务器假设列子的,突然发现,没有业务要求,画不出来.写了这么久了,天天熬夜,得休息几天再继续.其实还有非常重 ...

  3. SQL Server 2000 “用户XX已经存在” 处理方法

    -- 目前遇到这个问题都是在切换服务器时发生的. 旧服务器备份的数据库还原到新服务器,都会遇到这种问题 --切决方案如下: -- 查找孤立用户列表 EXECUTE sp_change_users_lo ...

  4. LAMP(Ubuntu+apache+mysql+php)+Zend Studio 新手の PHP的开发环境搭建

    因为工作需要,就从c#转型过来研究PHP.可是没想到从一开始就遇上了问题,环境配置方面的问题足足令我头疼了两天.因为博主本人对于linux的接触非常少,所以在解决这个问题的时候也学到了不少东西, 非常 ...

  5. Ubuntu安装google Gtest

    (1) 下载源码:http://code.google.com/p/googletest/gtest-1.7.0 (2013)gtest-1.7.0 (2010) (2) README编译指南126 ...

  6. .NET基础之深度复制和浅度复制

    之前一直没有搞清楚深度复制和浅度复制的区别到底在哪里,今天彻底把这个东西弄懂了,写出来与到家共勉. 如果大家不懂值类型和引用类型的区别,请先看http://www.cnblogs.com/Autumo ...

  7. SQL Server数据库备份(异机)

    简单的远程异机备份数据库功能,通过这个存储过程,讲远程其他机器上的数据库备份到本地.其主要原理为: 1.通过XP_CMDSHELL执行Windows命令,将本机的共享目录映射为远程机器的网络驱动器. ...

  8. python学习笔记17(动态类型)

    动态类型 在我们接触的对象中,有一类特殊的对象,是用于存储数据的,常见的该类对象包括各种数字,字符串,表,词典.在C语言中,我们称这样一些数据结构为变量,而在Python中,这些是对象. 对象是储存在 ...

  9. centos7安装chrome及加载poatman开发插件

    为什么要安装chrome?因为centos7的默认浏览器firefox的实在是不习惯,上面占了太多,本来显示器就不大... 好了,首先下载chome的rpm安装包(如果需要的可以留言,我有备份) 然后 ...

  10. Extjs4 treePanel异步加载菜单(后台从数据库读取)

    运行环境:springMVC+mybatis 一.建表 说明:0表示此节点为非叶子节点,即此节点还包括了子节点:1表示此节点为叶子节点,即此节点没有子节点.:关于图标iconCls是从Extjs的文件 ...