POJ 1823 Hotel

来源:互联网 发布:象棋小鹤求败是软件 编辑:程序博客网 时间:2024/06/14 06:19
Hotel
Time Limit: 5000MS Memory Limit: 30000KTotal 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 1031 2 31 9 432 2 132 9 232 3 23 

Sample Output

1244610

Source

Romania OI 2002
解题思路:线段树区间合并
#include<iostream>#include<cstdio>#include<cstring>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define Max 20000using 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