又来练线段树了……

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. CorelDRAW 插件的安装和使用

    CorelDRAW 是一款在中国非常受欢迎的图形软件,开放的界面和编程技术,能够对它进行二次开发制作插件,插件大抵有三种gms.cpg.exe格式,下面介绍一下这三种插件的安装和使用方法. 一.gms ...

  2. jooml二次开发---添加文章组件

    在写一个joomla组件的时候需要手动添加excel表格,并把表格当做文章的内容添加到前台文章中, 开始不知道怎么下手,索性先把一个基本的组件写出来,在joomla网站上测试是可以访问这个组件的,在p ...

  3. Android类库常用类库一览

    在Android SDK中包括很多包文件,通过了解这些包的功能也有助于了解可以开发的功能. 在Android类库中,各种包写成android.*的方式,重要包的描述如下所示: android.app ...

  4. 1043. Is It a Binary Search Tree

    http://www.patest.cn/contests/pat-a-practise/1043 #include <stdio.h> #include <vector> u ...

  5. W25Q32的使用

    一.W25Q32简介 W25Q32是华邦公司推出的大容量“SPI  FLASH” 产品. 1.容量 32M-Bit/4M-byte(4,194,304) 2.存储结构 页:256-bytes 扇区:4 ...

  6. Unity3d之Socket UDP协议

    原文地址:http://blog.csdn.net/dingkun520wy/article/details/49201245 (一)Socket(套接字)UDP协议的特点 1.是基于无连接的协议,没 ...

  7. linux grep、find 命令详解

    grep1.作用grep命令可以指定文件中搜索特定的内容,并将含有这些内容的行标准输出.grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权 ...

  8. Deferred Shading(延迟渲染)

    1.简介      在计算机图形学的词典里,Shading表示“对受光物体的渲染”,这个渲染过程包括下面几步[1]: 1) 计算几何多边形(也就是Mesh).      2) 决定表面材质特性,例如法 ...

  9. 扎克伯格谈Facebook创业过程

    第一课:Facebook的产品研发 (1)不仅注重用户体验,更关注程序本身对社会和产品的是否有益,进而对产品做出调整 (2)以学校为标准作为群组来划分,就是对产品进行了思考后决定的,不管是直觉决定还是 ...

  10. Python 开源异步并发框架的未来

    http://segmentfault.com/a/1190000000471602 开源 Python 是开源的,介绍的这几个框架 Twisted.Tornado.Gevent 和 tulip 也都 ...