Description

The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Street as their vacation residence. This immense hotel has N (1 ≤ N ≤ 50,000) rooms all located on the same side of an extremely long hallway (all the better to see the lake, of course).

The cows and other visitors arrive in groups of size Di (1 ≤ Di ≤ N) and approach the front desk to check in. Each group i requests a set of Di contiguous rooms from Canmuu, the moose staffing the counter. He assigns them some set of consecutive room numbers r..r+Di-1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. Canmuu always chooses the value of r to be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. Checkout i has the parameters Xi and Di which specify the vacating of rooms Xi ..Xi +Di-1 (1 ≤ Xi ≤ N-Di+1). Some (or all) of those rooms might be empty before the checkout.

Your job is to assist Canmuu by processing M (1 ≤ M < 50,000) checkin/checkout requests. The hotel is initially unoccupied.

                              --by POJ

http://poj.org/problem?id=3667



给定初始值全0的区间[1,n],支持两种操作:

1 w //查询整个区间最左端长度为w的连续0区间,输出其左端点,并把她全变成1,不存在,则输出0
2 l len //把以l为左端点长度为len的区间清零
 
考虑线段树维护区间:
max:区间最长连续0;
lmax:区间左起最长连续0;
rmax:区间右起最长连续0;
然后把1操作拆成先查询再在此基础上修改,这样修改可与2操作共用函数
查询时
优先递归左半区间,
左区间max非法,则查询左区间rmax+右区间lmax,
再非法,则递归右半区间,
再非法,则返回0,
修改则走线段树正常区间修改流程,打标记什么的;
注意如果查询失败,记得不能进行修改;
然后,如果查到了单点,记得特判一下;
一开始,我想当然的想为1操作定义一个函数,为2操作定义一个函数;
这样第一个函数一边查询一边修改
然而这样就需要打各种各样修改位置不同的标记——因为查询的递归与修改的递归流程很不相似
废了好久,
然后才知道可以这样定函数,
重构,然后清晰了许多;
代码能力不够啊;
代码如下:
  1. #include<cstdio>
  2. using namespace std;
  3. int n,m,L,R;
  4. struct tree{
  5. int lmax,rmax,max,lz;
  6. }line[];
  7. void builine(int ,int ,int );
  8. void up(int ,int ,int );
  9. void down(int ,int ,int );
  10. int search(int ,int ,int ,int );
  11. void change(int ,int ,int ,int );
  12. int main()
  13. {
  14. int i,j,k,ans;
  15. scanf("%d%d",&n,&m);
  16. builine(,n,);
  17. for(i=;i<=m;i++){
  18. scanf("%d",&j);
  19. if(j==){
  20. scanf("%d",&k);
  21. ans=search(,n,,k);
  22. printf("%d\n",ans);
  23. L=ans;R=L+k-;
  24. if(L)
  25. change(,n,,);
  26. }
  27. else{
  28. scanf("%d%d",&L,&k);
  29. R=L+k-;
  30. change(,n,,);
  31. }
  32. }
  33. }
  34. void builine(int l,int r,int nu){
  35. if(l==r){
  36. line[nu].lmax=line[nu].rmax=line[nu].max=;
  37. return ;
  38. }
  39. int mid=(l+r)>>;
  40. builine(l,mid,nu<<);
  41. builine(mid+,r,nu<<|);
  42. line[nu].lmax=line[nu].rmax=line[nu].max=line[nu<<].max+line[nu<<|].max;
  43. }
  44. void up(int l,int r,int nu){
  45. int mid=(l+r)>>;
  46. if(line[nu<<].max>line[nu<<|].max)
  47. line[nu].max=line[nu<<].max;
  48. else
  49. line[nu].max=line[nu<<|].max;
  50. if(line[nu].max<line[nu<<].rmax+line[nu<<|].lmax)
  51. line[nu].max=line[nu<<].rmax+line[nu<<|].lmax;
  52. line[nu].lmax=line[nu<<].lmax==(mid-l+)?line[nu<<].lmax+line[nu<<|].lmax:line[nu<<].lmax;
  53. line[nu].rmax=line[nu<<|].rmax==(r-mid)?line[nu<<|].rmax+line[nu<<].rmax:line[nu<<|].rmax;
  54. }
  55. void down(int l,int r,int nu){
  56. int mid=(l+r)>>;
  57. if(line[nu].lz){
  58. line[nu<<].lmax=line[nu<<].max=line[nu<<].rmax=((line[nu].lz-)^)*(mid-l+);
  59. line[nu<<|].lmax=line[nu<<|].max=line[nu<<|].rmax=((line[nu].lz-)^)*(r-mid);
  60. line[nu<<].lz=line[nu<<|].lz=line[nu].lz;
  61. line[nu].lz=;
  62. }
  63. }
  64. int search(int l,int r,int nu,int len){
  65. int mid=(l+r)>>,ans;
  66. down(l,r,nu);
  67. if(l==r)
  68. return line[nu].max*l;
  69. if(line[nu<<].max>=len){
  70. ans=search(l,mid,nu<<,len);
  71. return ans;
  72. }
  73. if(line[nu<<].rmax+line[nu<<|].lmax>=len){
  74. ans=mid-line[nu<<].rmax+;
  75. return ans;
  76. }
  77. if(line[nu<<|].max>=len){
  78. ans=search(mid+,r,nu<<|,len);
  79. return ans;
  80. }
  81. return ;
  82. }
  83. void change(int l,int r,int nu,int x){
  84. if(L<=l&&r<=R){
  85. line[nu].lmax=line[nu].rmax=line[nu].max=(r-l+)*(x^);
  86. line[nu].lz=x+;
  87. return ;
  88. }
  89. down(l,r,nu);
  90. int mid=(l+r)>>;
  91. if(L<=mid)
  92. change(l,mid,nu<<,x);
  93. if(R>mid)
  94. change(mid+,r,nu<<|,x);
  95. up(l,r,nu);
  96. }

祝AC

POJ P3667 Hotel——solution的更多相关文章

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

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

  2. POJ 3667 Hotel(线段树)

    POJ 3667 Hotel 题目链接 题意:有n个房间,如今有两个操作 1.找到连续长度a的空房间.入住,要尽量靠左边,假设有输出最左边的房间标号,假设没有输出0 2.清空[a, a + b - 1 ...

  3. poj 3667 Hotel (线段树)

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

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

    Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...

  5. poj 3667 Hotel(线段树,区间合并)

    Hotel Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 10858Accepted: 4691 Description The ...

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

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

  7. (简单) POJ 3667 Hotel,线段树+区间合并。

    Description The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and e ...

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

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

  9. Poj 3667——hotel——————【线段树区间合并】

    Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13124   Accepted: 5664 Descriptio ...

随机推荐

  1. 如何将Ajax请求从异步改为同步

    Ajax请求默认的都是异步的 如果想同步 async设置为false就可以(默认是true) var html = $.ajax({   url: "some.PHP",   as ...

  2. Python笔记之字典循环

    Python笔记之字典循环   1.问题 Python是一门比较好入门的编程语言,但是入门简单,当然坑也是有的,今天就来介绍一个我遇到的坑吧,也是很简单的一个,就是当时脑子有点转不过弯来了. 先看代码 ...

  3. SDN定义网络

    http://edu.51cto.com/course/course_id-4466.html http://edu.51cto.com/course/course_id-4497.html

  4. RunningCassandraInEclipse(转载)

    转载自:http://wiki.apache.org/cassandra/RunningCassandraInEclipse Eclipse is open source. Download Ecli ...

  5. Redis实现分布式存储Session

    前言: 在单个项目时,一般都是用HttpSession接口存储当前登录用户的信息.但是在分布式项目的情况下,session是不会共享的,那怎么实现session共享呢?往下看.... 一.准备工作(基 ...

  6. Contest Hunter 1401 兔子与兔子

    1401 兔子与兔子 0x10「基本数据结构」例题 描述 很久很久以前,森林里住着一群兔子.有一天,兔子们想要研究自己的 DNA 序列.我们首先选取一个好长好长的 DNA 序列(小兔子是外星生物,DN ...

  7. 封装 原生 fetch

    1, 简介 fetch方法是 Fetch API的一个方法,提供了一种简单.合理的方式来跨网络异步获取资源. 与原来的XMLHttpRequest比较,fetch更容易与其他的技术结合:比如servi ...

  8. 查看tomcat部署的项目名

    Myeclips的查看方法 项目名右键-->properties-->Myeclips-->deployment 这里虽然可以改这个路径的项目名 但是一般不建议更改 避免出现未知错误 ...

  9. SPSS学习系列之SPSS Statistics导入读取数据(多种格式)(图文详解)

    不多说,直接上干货! SPSS Statistics导入读取数据的步骤: 文件  ->  导入数据 成功! 欢迎大家,加入我的微信公众号:大数据躺过的坑     免费给分享       同时,大 ...

  10. python-cgi-demo

    简单的Python CGI 在linux平台实现注意:路径是以当前路径为根目录 ,Python文件一般放在/cgi-bin/目录下在linux命令行运行:python  -m  CGIHTTPServ ...