【POJ1823】【线段树】Hotel
Description
Write a program that should efficiently respond to these 3 types of instructions:
type 1: the arrival of a new group of tourists
A group of M tourists wants to occupy M free consecutive rooms. The program will receive the number i which represents the start room of the sequence of the rooms that the group wants to occupy and the number M representing the number of members of the group. It is guaranteed that all the rooms i,i+1,..,i+M-1 are free at that moment.
type 2: the departure of a group of tourists
The tourists leave in groups (not necessarilly those groups in which they came). A group with M members leaves M occupied and consecutive rooms. The program will receive the number i representing the start room of the sequence of the released rooms and the number M representing the number of members of the group. It is guaranteed that all the rooms i,i+1,..,i+M-1 are occupied.
type 3: the owner's question
The owner of the hotel may ask from time to time which is the maximal length of a sequence of free consecutive rooms. He needs this number to know which is the maximal number of tourists that could arrive to the hotel. You can assume that each room may be occupied by no more than one tourist.
Input
The next P lines will contain the number c representing the type of the instruction:
- if c is 1 then it will be followed (on the same line) by 2 other numbers, i and M, representing the number of the first room distributed to the group and the number of the members
- if c is 2 then it will be followed (on the same line) by 2 other numbers, i and M, representing the number of the first room that will be released and the number of the members of the group that is leaving
- if c is 3 then it will not be followed by any number on that line, but the program should output in the output file the maximal length of a sequence of free and consecutive rooms
Output
Sample Input
12 10
3
1 2 3
1 9 4
3
2 2 1
3
2 9 2
3
2 3 2
3
Sample Output
12
4
4
6
10
Source
/*
唐代许浑
《咸阳城东楼 / 咸阳城西楼晚眺 / 西门》 一上高城万里愁,蒹葭杨柳似汀洲。
溪云初起日沉阁,山雨欲来风满楼。
鸟下绿芜秦苑夕,蝉鸣黄叶汉宫秋。
行人莫问当年事,故国东来渭水流。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#define LOCAL
const int MAXN = + ;
const int MAXM = + ;
const int INF = ;
const int SIZE = ;
const int maxnode = 0x7fffffff + ;
using namespace std;
int i;
struct SEGTREE{
struct Node{
int l, r;
int rmax, mmax, lmax;//分别代表从左边开始的最长,从右边开始的最长和中间的最长
int delta; /*void Count(){
rmax = lmax = mmax = 0;
for (int i = l; i <= r; i++) if (data[i] == 0){lmax = i - l;break;}
for (int i = r; i >= l; i--) if (data[i] == 0){rmax = r - i;break;}
int t = 0;
for (int i = l; i <= r; i++){ }
}*/
}tree[MAXN * ]; void pushdown(int t){
if (tree[t].delta != -){
if (tree[t].delta == ) {//全1
tree[(t<<)].lmax = tree[(t<<)].rmax = tree[(t<<)].mmax = tree[(t<<)].r - tree[(t<<)].l + ;tree[(t<<)].delta = ;
tree[(t<<) | ].lmax = tree[(t<<) | ].rmax = tree[(t<<) | ].mmax = tree[(t<<) | ].r - tree[(t<<) | ].l + ;tree[(t<<) | ].delta = ;
tree[t].delta = -;
}else{
tree[(t<<)].lmax = tree[(t<<)].rmax = tree[(t<<)].mmax = ;tree[(t<<)].delta = ;
tree[(t<<) | ].lmax = tree[(t<<) | ].rmax = tree[(t<<) | ].mmax = ;tree[(t<<) | ].delta = ;
tree[t].delta = -;
}
}
}
//更新
void update(int t){
tree[t].mmax = max(tree[t<<].mmax, max(tree[(t<<)|].mmax, tree[t<<].rmax + tree[(t<<)|].lmax));
//更新tree[t]的lmax
if (tree[t<<].lmax == tree[t<<].r - tree[t<<].l + ) tree[t].lmax = tree[t<<].lmax + tree[(t<<)|].lmax;
else tree[t].lmax = tree[t<<].lmax; //同理
if (tree[(t<<)|].rmax == tree[(t<<)|].r - tree[(t<<)|].l + ) tree[t].rmax = tree[t<<].rmax + tree[(t<<)|].rmax;
else tree[t].rmax = tree[(t<<)|].rmax;
}
void build(int t, int l, int r){
tree[t].l = l;
tree[t].r = r;
tree[t].lmax = tree[t].mmax = tree[t].rmax = tree[t].r - tree[t].l + ;
tree[t].delta = -;
if (l == r) return;
int mid = (l + r) >> ;
build(t << , l, mid);
build((t << )|, mid + , r);
}
void insert(int t, int l, int r, int val){//t为节点编号,val为权值
pushdown(t);
if (l <= tree[t].l && tree[t].r <= r){
if (val == ) {tree[t].rmax = tree[t].lmax = tree[t].mmax = tree[t].r - tree[t].l + ;tree[t].delta = ;}
else {tree[t].rmax = tree[t].lmax = tree[t].mmax = ;tree[t].delta = ;}
return;
}
int mid = (tree[t].l + tree[t].r)>>;
//if (i == 3 && tree[t].l == 10 && tree[t].r == 11)
//printf("");
if (l <= mid) insert(t << , l, r , val);
if (r > mid) insert((t << ) | , l, r, val); update(t);
}
}A;
int n, p; void init(){
scanf("%d%d", &n, &p);
A.build(, , n);
}
void work(){
for (i = ; i <= p; i++){
int t;
scanf("%d", &t);
if (t == ) printf("%d\n", A.tree[].mmax);
else if (t == ){
int l, r;
scanf("%d%d", &l, &r);
A.insert(, l, l + r - , );
}else if (t == ){
int l, r;
scanf("%d%d", &l, &r);
A.insert(, l, l + r - , );
}
}
} int main(){ init();
work();
return ;
}
【POJ1823】【线段树】Hotel的更多相关文章
- POJ 3667 Hotel(线段树 区间合并)
Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...
- ACM: Hotel 解题报告 - 线段树-区间合并
Hotel Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description The ...
- 线段树(区间合并) POJ 3667 Hotel
题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...
- poj 3667 Hotel (线段树)
http://poj.org/problem?id=3667 Hotel Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 94 ...
- PKU 3667 Hotel(线段树)
Hotel The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...
- Hotel(线段树合并)
Hotel Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 14958 Accepted: 6450 Descriptio ...
- 线段树||BZOJ1593: [Usaco2008 Feb]Hotel 旅馆||Luogu P2894 [USACO08FEB]酒店Hotel
题面:P2894 [USACO08FEB]酒店Hotel 题解:和基础的线段树操作差别不是很大,就是在传统的线段树基础上多维护一段区间最长的合法前驱(h_),最长合法后驱(t_),一段中最长的合法区间 ...
- poj3667 Hotel (线段树 区间合并)
poj3667 HotelTime Limit: 3000MS Memory Limit: 65536KTotal Submissions: 18925 Accepted: 8242Descripti ...
- 洛谷P2894 [USACO08FEB]酒店Hotel [线段树]
题目传送门 酒店 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and ...
- Poj 3667——hotel——————【线段树区间合并】
Hotel Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 13124 Accepted: 5664 Descriptio ...
随机推荐
- 【转】VSync Count 垂直同步
原文:http://blog.csdn.net/yesy10/article/details/7794556 Unity3D中新建一个场景空的时候,帧速率(FPS总是很低),大概在60~70之间.一直 ...
- 低压差稳压器AMS1585
(1)高效线性稳压. (2)输出高达4.6A,最高输入电压15V,推荐最低压差1.5V(最低1.35V),最大压差12V. (3)两种封装:TO220(直插式),TO230(贴片). 典型电路如下图所 ...
- 工作总结之动画与VR
一.Unity5的动画新特性: 1.animator controller默认就包含Any State.Entry.Exit三个状态 2.animator可以给每个动画片段添加脚本,该脚本继承 Sta ...
- ASP.NET MVC 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction差别
使用方法:@Html.Action(action, controller)加载局部页面.例如在模板页中使用:@Html.Action("Contact", "Compan ...
- bzoj 1096 [ZJOI2007]仓库建设(关于斜率优化问题的总结)
1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3234 Solved: 1388[Submit][Stat ...
- mac下的改装人生——关于mc700安装双硬盘以后的win重装问题
宝贝到家~玩了几天,感觉神清气爽~今天正式开始准备工作了哈. 今天早上开了mac的win7系统发现了几个小问题.好吧我承认这个真的是小问题,就是我没有办法能调整屏幕的亮度了.重装了bootcamp依然 ...
- Cogs 1672. [SPOJ375 QTREE]难存的情缘 LCT,树链剖分,填坑计划
题目:http://cojs.tk/cogs/problem/problem.php?pid=1672 1672. [SPOJ375 QTREE]难存的情缘 ★★★☆ 输入文件:qtree.in ...
- CPP数组
数组为函数参数,求出一组数中的最大者;
- Django路由系统
django路由系统 简而言之,django的路由系统作用就是使views里面处理数据的函数与请求的url建立映射关系.使请求到来之后,根据urls.py里的关系条目,去查找到与请求对应的处理方法,从 ...
- 简单粗暴地理解 JavaScript 原型链
尼玛!你特么也是够了! Don’t BB! Show me the code! function Person (name) { this.name = name; } function Mother ...