POJ 1823 Hotel

Hotel
Time Limit: 5000MS Memory Limit: 30000K
Total Submissions: 2059 Accepted: 882

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. 

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: 
  • 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 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<cstring>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define Max 20000
using namespace std;
struct
{
	int lsum;
	int rsum;
	int msum;
	int cover;
}tree[Max<<2];
void push_up(int rt,int len)
{
	tree[rt].lsum=tree[rt<<1].lsum;
	tree[rt].rsum=tree[rt<<1|1].rsum;
	if(tree[rt].lsum==len-(len>>1))
		tree[rt].lsum+=tree[rt<<1|1].lsum;
	if(tree[rt].rsum==(len>>1))
		tree[rt].rsum+=tree[rt<<1].rsum;
	tree[rt].msum=max(tree[rt<<1].rsum+tree[rt<<1|1].lsum,max(tree[rt<<1].msum,tree[rt<<1|1].msum));
}
void push_down(int rt,int len)
{
	if(tree[rt].cover)
	{
		tree[rt<<1].lsum=tree[rt<<1].rsum=tree[rt<<1].msum=tree[rt].cover==1?0:len-(len>>1);
		tree[rt<<1|1].lsum=tree[rt<<1|1].rsum=tree[rt<<1|1].msum=tree[rt].cover==1?0:(len>>1);
		tree[rt<<1].cover=tree[rt<<1|1].cover=tree[rt].cover;
		tree[rt].cover=0;
	}
}
void update(int L,int R,int c,int l,int r,int rt)
{
	if(L<=l&&R>=r)
	{
		tree[rt].lsum=tree[rt].rsum=tree[rt].msum=c==1?0:r-l+1;
		tree[rt].cover=c;
		return ;
	}
	push_down(rt,r-l+1);
	int m=(l+r)>>1;
	if(L<=m)
		update(L,R,c,lson);
	if(R>m)
		update(L,R,c,rson);
	push_up(rt,r-l+1);
}
void build(int l,int r,int rt)
{
	if(l==r)
	{
		tree[rt].lsum=tree[rt].rsum=tree[rt].msum=1;
		tree[rt].cover=0;
		return ;
	}
	int m=(l+r)>>1;
	build(lson);
	build(rson);
	push_up(rt,r-l+1);
}
int main()
{
	int m,n,q,a,b;
	while(~scanf("%d%d",&n,&m))
	{
		build(1,n,1);
		while(m--)
		{
			scanf("%d",&q);
			switch(q)
			{
			case 1:
				scanf("%d%d",&a,&b);
				b+=a-1;
				update(a,b,1,1,n,1);
				break;
			case 2:
				scanf("%d%d",&a,&b);
				b+=a-1;
				update(a,b,2,1,n,1);
				break;
			case 3:
				printf("%d\n",tree[1].msum);
				break;
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值