线段树专辑——pku 3667 Hotel

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

哈哈,经典中的经典题啊。利用线段树求最大连续空闲区间,并返回空闲区间的起点坐标。

View Code
  1 #include<iostream>
2 #include<string>
3 #include<algorithm>
4 using namespace std;
5
6 struct node
7 {
8 int l;
9 int r;
10 int l_val; //从左节点数起的最大连续空闲区间
11 int r_val; //从右节点起的最大连续空闲区间
12 int max_val; //整个区间的最大连续空闲区间
13 int cover; //当前区间的占用情况,1表示占用,0表示不占用,-1表示既有占用又有不占用
14 };
15
16 node tree[250000];
17 int n,m;
18
19 int max(int a,int b)
20 {
21 return a>b?a:b;
22 }
23
24 void build(int i,int l,int r)
25 {
26 tree[i].l=l;
27 tree[i].r=r;
28 tree[i].cover=0;
29 tree[i].l_val=tree[i].r_val=tree[i].max_val=r-l+1; //开始所有区间都未使用
30 if(l==r)
31 return;
32 int mid=(l+r)/2;
33 build(2*i,l,mid);
34 build(2*i+1,mid+1,r);
35 }
36
37 void fun(int i) //跟新区间
38 {
39 if(tree[i].cover==0)
40 {
41 tree[i].l_val=tree[i].r_val=tree[i].max_val=tree[i].r-tree[i].l+1;
42 }
43 else if(tree[i].cover==1)
44 {
45 tree[i].l_val=tree[i].r_val=tree[i].max_val=0;
46 }
47 }
48
49 void updata(int i,int l,int r,int w)
50 {
51 if(tree[i].l>r || tree[i].r<l)
52 return;
53 if(tree[i].l>=l && tree[i].r<=r)
54 {
55 tree[i].cover=w; //跟新该段情况
56 fun(i); //并根据cover跟新其他域
57 return;
58 }
59 if(tree[i].cover!=-1)
60 {
61 tree[2*i].cover=tree[2*i+1].cover=tree[i].cover;
62 fun(2*i); fun(2*i+1);
63 tree[i].cover=-1;
64 }
65 updata(2*i,l,r,w);
66 updata(2*i+1,l,r,w);
67 if(tree[2*i].cover==tree[2*i+1].cover) //由下而上跟新所有信息
68 tree[i].cover=tree[2*i].cover;
69 else
70 tree[i].cover=-1;
71 tree[i].l_val=tree[2*i].l_val+(tree[2*i].cover==0 ? tree[2*i+1].l_val:0);
72 tree[i].r_val=tree[2*i+1].r_val+(tree[2*i+1].cover==0 ? tree[2*i].r_val:0);
73 tree[i].max_val=max(max(tree[2*i].max_val,tree[2*i+1].max_val),(tree[2*i].r_val+tree[2*i+1].l_val));
74 }
75
76 int find(int i,int w)
77 {
78 if(tree[i].cover==0 && tree[i].l_val>=w) //从左边起能找到
79 {
80 return tree[i].l;
81 }
82 if(tree[i].cover==1)
83 {
84 return 0;
85 }
86 if(tree[i].cover==-1 && tree[i].max_val>=w)
87 {
88 if(tree[2*i].max_val>=w) //左边还有足够空间,向左递归
89 return find(2*i,w);
90 else if(tree[2*i].r_val+tree[2*i+1].l_val>=w) //如果中间有足够空间,直接计算
91 {
92 return tree[2*i].r-tree[2*i].r_val+1;
93 }
94 else
95 return find(2*i+1,w); //向右递归
96 }
97 return 0;
98 }
99
100 int main()
101 {
102 freopen("D:\\in.txt","r",stdin);
103 int i,a,b,c;
104 while(scanf("%d%d",&n,&m)!=EOF)
105 {
106 build(1,1,n);
107 for(i=0;i<m;i++)
108 {
109 scanf("%d",&c);
110 if(c==1)
111 {
112 scanf("%d",&a);
113 b=find(1,a);
114 printf("%d\n",b);
115 if(b)
116 {
117 updata(1,b,b+a-1,1);
118 }
119 }
120 else
121 {
122 scanf("%d%d",&a,&b);
123 updata(1,a,a+b-1,0);
124 }
125 }
126 }
127 return 0;
128 }

 

 

转载于:https://www.cnblogs.com/ka200812/archive/2011/11/10/2244966.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/275177.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

houseparty不流畅_重新设计Houseparty –用户体验案例研究

houseparty不流畅Houseparty has become very popular during the COVID-19 period because it helps you connect with others in a fun way. The concept is simple, you open the app and jump on a video call with your friends. You can even play online games with the…

你不知道的 Node.js 工具函数

从类型判断说起在 JavaScript 中&#xff0c;进行变量的类型校验是一个非常令人头疼的事&#xff0c;如果只是简单的使用 typeof 会到各种各样的问题。举几个简单的&#x1f330;&#xff1a;console.log(typeof null) // object console.log(typeof new Array) // object cons…

Java应用集群下的定时任务处理方案(mysql)

今天来说一个Java多机部署下定时任务的处理方案。 需求: 有两台服务器同时部署了同一套代码&#xff0c; 代码中写有spring自带的定时任务&#xff0c;但是每次执行定时任务时只需要一台机器去执行。 当拿到这个需求时我脑子中立马出现了两个简单的解决方案&#xff1a; 利用ip…

概念验证_设置成功的UX概念验证

概念验证用户体验/概念证明/第1部分 (USER EXPERIENCE / PROOF OF CONCEPT / PART 1) This is the first article of a four-part series. Please read Part 2 and Part 3.这是由四个部分组成的系列文章的第一篇。 请阅读 第2 部分 和 第3部分 。 How do today’s top UX desi…

从 vue3 和 vite 源码中,我学到了一行代码统一规范团队包管理器的神器

1. 前言大家好&#xff0c;我是若川。最近组织了源码共读活动&#xff0c;感兴趣的可以加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。已进行四个月了&#xff0c;很多小伙伴表示收获颇丰。想学源码&#xff0c;极力推荐之前我写…

6个高效办公的Excel小技巧,学会让你高效办公

很多人在做Excel表格的时候&#xff0c;会出现下面这种情况&#xff1a;好不容易把内容都输入好了&#xff0c;才发现文字或是数字的排列组合需要重新调整&#xff0c;这个时候头就大了&#xff0c;到底是要一个个复制黏贴&#xff0c;还是要删除后再添加&#xff1f;不管哪种方…

unity 完美像素_像素完美

unity 完美像素从Kidpix到设计系统 (From Kidpix to design systems) Did you ever create stamps in KidPix? Kidpix is bitmap drawing software that’s been around since the nineties, and I remember many happy — more like maddening — hours creating tiny pixela…

整整4个月了,尽全力组织了源码共读活动~

大家好&#xff0c;我是若川。从8月份到现在11月结束了。每周一期&#xff0c;一起读200行左右的源码&#xff0c;撰写辅助文章&#xff0c;截止到现在整整4个月了。由写有《学习源码整体架构系列》20余篇的若川【若川视野公众号号主】倾力组织&#xff0c;召集了各大厂对于源码…

字节内部前端开发手册(完整版)开放下载!

备战2022&#xff0c;准备好了吗&#xff1f;据字节HR部门发布的最新信息&#xff0c;2019年以来字节连续3年扩招&#xff0c;而即将到来的2022年春招前端岗位数不低于3000&#xff0c;虽连年扩招&#xff0c;但是报录比却从2019年的3%下降到今年的1%。BAT等一线大厂同样有类似…

EBS中Java并发程序笔记(1)

在Oracle EBS中的Java并发程序&#xff08;Java Concurrent Program&#xff09;是系统功能中的一个亮点&#xff0c;它的出现使得用户可以在ERP系统中运行自己定义的Java程序。本文为学习笔记&#xff0c;所以不会介绍太多背景知识。 使用Java并发程序的好处&#xff1a; 当遇…

figma设计_5位来自杂乱无章的设计师的Figma技巧

figma设计When starting a design project, a fast pace and multiple design iterations can easily lead to a cluttered mess. Taking the time in the beginning to build good organizational habits will save you time later. You’ll thank your past self when you do…

设计和实现一个 Chrome 插件提升登录效率

大家好&#xff0c;我是若川。最近组织了源码共读活动&#xff0c;感兴趣的可以点此加我微信ruochuan12 进群参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。已进行4个月了&#xff0c;很多小伙伴表示收获颇丰。前言在我们的工作过程中&#xff0c;每当…

qq空间网页设计_网页设计中的负空间

qq空间网页设计重点 (Top highlight)Because screens are limited, web design is also limited. It can be said that in the small box of the screen, each pixel is a piece of real estate.由于屏幕有限&#xff0c;因此网页设计也受到限制。 可以说&#xff0c;在屏幕的小…

时隔一年半,我,一个卑微的前端菜鸡,又来写面经了

大家好&#xff0c;我是若川。最近组织了源码共读活动&#xff0c;感兴趣的可以点此加我微信ruochuan12 进群参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。已进行4个月了&#xff0c;很多小伙伴表示收获颇丰。作者&#xff1a;刮涂层_赢大奖原文地址…

用户体验与可用性测试_可用性作为用户体验的原则

用户体验与可用性测试Every UX Designer has his views and best practices. We all have a guide book created through time and experience. I want to share mine with you.每个UX设计器都有他的观点和最佳实践。 我们都有一本通过时间和经验编写的指南。 我想和你分享我的…

Jenkins插件之Deploy

deploy插件&#xff1a; Deploy Plugindeploy插件支持将War/Jar部署到远程的应用服务器上&#xff0c;例如Tomcat,JBoss,Glassfish。正在寻找或开发.NET web 应用的自动发布插件。如何回滚或重新部署先前的build&#xff1a;0&#xff09; 需要被deploy的job的结果要存档&#…

受美国法律保护美国妞_为什么美国法律有效地要求所有软件设计都要响应

受美国法律保护美国妞Smashing Magazine defines “responsive design” as an approach where design responds to the user’s behavior and environment based on screen size, platform, and orientation. In responsive design, a breakpoint is the “point” at which a…

源码群友问:你这么多项目是怎么进行技术选型的?

大家好&#xff0c;我是若川。最近组织了源码共读活动&#xff0c;感兴趣的可以点此加我微信ruochuan12 进群参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。已进行4个月了&#xff0c;很多小伙伴表示收获颇丰。源码群有群友提问我是怎么找到那么多npm…

iOS开发之打包上传报错: ERROR ITMS-90087/ERROR ITMS-90125

制作好的framework在打包上传至AppStore如果出现以下错误&#xff0c;则说明这个SDK里面包含了x86_64, i386 架构&#xff0c;当然这个AppStore是不允许的&#xff0c;所以会在上传的时候报错&#xff0c;解决办法就是要这个SDK剔除掉x86_64, i386这两个架构 解决办法&#xff…

ios 动画设计_动画和讲故事在设计中的力量

ios 动画设计As human beings, we’ve always been fond of storytelling. Just think of campfire stories, Santa Claus, or that thrilling book you just finished. Here’s how you can use the power of storytelling to make your designs better.作为人类&#xff0c;我…