poj 1823 Hotel(线段树·区间更新·lazy tag)
来源:互联网 发布:2am 2pm 知乎 编辑:程序博客网 时间:2024/04/26 22:38
题目:http://poj.org/problem?id=1823
Hotel
Time Limit: 5000MS Memory Limit: 30000KTotal Submissions: 2398 Accepted: 1051
Description
The "Informatics" hotel is one of the most luxurious hotels from Galaciuc. A lot of tourists arrive or leave this hotel in one year. So it is pretty difficult to keep the evidence of the occupied rooms. But this year the owner of the hotel decided to do some changes. That's why he engaged you to write an efficient program that should respond to all his needs.
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.
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
On the first line of input, there will be the numbers N (3 <= N <= 16 000) representing the number of the rooms and P (3 <= P <= 200 000) representing the number of the instructions.
The next P lines will contain the number c representing the type of the instruction:
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
In the output you will print for each instruction of type 3, on separated lines, the maximal length of a sequence of free and consecutive rooms. Before the first instruction all the rooms are free.
Sample Input
12 1031 2 31 9 432 2 132 9 232 3 23
Sample Output
1244610分析:线段树的区间更新,任意一段区间全部填充或者全部清空,问最大的连续的空区间长度。为了节省时间,用懒惰标记来更新区间(这是个重点),不必每次都访问到叶子节点,询问最长的连续子区间,则需要我们每个线段都得更新lsum左连续区间,rsum右连续区间,msum中间连续区间。
#include <iostream>#include <cstdio>#include <algorithm>using namespace std; // release --> 1(+1) occupy --> 0(-1)#define lt 2*root#define rt 2*root+1const int maxn=20000;struct node{ int l,r,tag,lsum,rsum,msum; int mid(){ return (l+r)/2; } int length(){ return (r-l+1); }}tree[maxn<<2];void build(int root,int ll,int rr){ tree[root].l=ll; tree[root].r=rr; tree[root].tag=-1; tree[root].lsum=tree[root].rsum=tree[root].msum=tree[root].length(); if(ll==rr) return; int m=tree[root].mid(); build(lt,ll,m); build(rt,m+1,rr);}void update(int root,int ll,int rr,int tg){ if(ll==tree[root].l&&rr==tree[root].r){ tree[root].tag=tg; tree[root].lsum=tree[root].rsum=tree[root].msum=tree[root].length()*tg; return ; } if(tree[root].tag!=-1){ tree[lt].tag=tree[rt].tag=tree[root].tag; //等于tree[root].tag 不是tg! tree[lt].lsum=tree[lt].rsum=tree[lt].msum=tree[lt].length()*tree[lt].tag; //乘以tree[root].tag 不是tg tree[rt].lsum=tree[rt].rsum=tree[rt].msum=tree[rt].length()*tree[rt].tag; tree[root].tag=-1; } int m=tree[root].mid(); if(ll>m)update(rt,ll,rr,tg); else if(rr<=m) update(lt,ll,rr,tg); else { update(lt,ll,m,tg); update(rt,m+1,rr,tg); } tree[root].lsum=tree[lt].lsum; if(tree[lt].lsum==tree[lt].length()) tree[root].lsum=tree[lt].lsum+tree[rt].lsum; tree[root].rsum=tree[rt].rsum; if(tree[rt].rsum==tree[rt].length()) tree[root].rsum=tree[rt].rsum+tree[lt].rsum; tree[root].msum=max(max(tree[lt].msum,tree[rt].msum),tree[lt].rsum+tree[rt].lsum);}int main(){ //freopen("cin.txt","r",stdin); int n,p; while(cin>>n>>p){ build(1,1,n); int c,a,b; while(p--){ scanf("%d",&c); if(c==1){ scanf("%d%d",&a,&b); update(1,a,a+b-1,0); } else if(c==2){ scanf("%d%d",&a,&b); update(1,a,a+b-1,1); } else { int res=max(tree[1].lsum,tree[1].rsum); res=max(res,tree[1].msum); printf("%d\n",res); } } } return 0;}
0 0
- poj 1823 Hotel(线段树·区间更新·lazy tag)
- POJ 3667 Hotel 【线段树 区间合并 + Lazy-tag】
- poj 3667 Hotel(线段树,成段更新,区间合并,Lazy思想)
- poj 3667 Hotel(线段树区间更新)
- POJ 1823 Hotel 线段树 + lazy标签
- poj 3667 Hotel 【线段树lazy + 区间合并】
- poj 1823 Hotel(数据结构:线段树--区间更新)
- POJ 1823 Hotel(线段树区间更新)
- poj 3468 线段树区间更新lazy
- poj 2777 Count Color(线段树 Lazy-Tag思想 成段更新+区间统计)
- POJ 2777 Count Color (线段树的区间更新+lazy tag)
- POJ 3667 Hotel 线段树 区间更新 区间合并
- [POJ 1823] Hotel 线段树区间合并
- POJ 3667 Hotel(线段树的合并+lazy tag)【很详细!!】
- POJ 3667 Hotel 线段树 区间合并(成段更新)
- poj 3667 Hotel(线段树区间更新合并)
- POJ 3667 Hotel (线段树 区间更新查找)
- POJ 3667 Hotel [线段树+区间更新]【数据结构】
- java基础笔记
- Java中的异常(1)
- ZOJ 3886 Nico number(线段树)
- 如何拉伸有透明区域的png图片来做UIButton的背景图片
- poll&&epoll实现分析(二)——epoll实现
- poj 1823 Hotel(线段树·区间更新·lazy tag)
- 32位机和64位机编程时应该注意些什么
- HDU2.2.5 三角形
- 不要死性不改
- Django之第一个app<15>
- UVa 10635 - Prince and Princess(LCS转LIS)
- listview实现自动向上滚动,快速滚动,和滚动条的一些设置
- 简单工厂方法,工厂方法,抽象工厂方法
- web开发的几道笔试题