poj1823 - hotel

http://poj.org/problem?id=1823
Hotel
Time Limit: 5000MS Memory Limit: 30000K
Total Submissions: 2389 Accepted: 1044
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

Romania OI 2002

题意:
题意:一个hotel,有n个连续的房间,开始时均无人住宿

共有3种操作

1 a b 从a开始连续b个房间全部旅客住宿 [a,a+b-1];

2 a b 从a开始连续b个房间全部旅客离开 [a,a+b-1];

3 查询最长连续空房间

题解:
线段树的经典区间修改问题
区间维护三个值:连续的最大值,从左端点连续的最大值,从右端点连续的最大值
另外有一点要注意的,不知为什么g++我tle了,c++就ac了,tle的朋友们注意一下

华丽的程序

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>

using namespace std;
const int maxn=16005;
struct node{
    int sum, ld, rd, setv;
    node() {setv=-1;}
}f[maxn*4];
int a, b, v;
void build(int o, int l, int r){
    int mid=(l+r)>>1, lc=(o<<1), rc=(o<<1)+1;
    f[o].sum=f[o].ld=f[o].rd=r-l+1; 
    if (l==r) return;
    build(lc, l, mid); build(rc, mid+1, r);
}

void pushdown(int o){
    int lc=(o<<1), rc=(o<<1)+1;
    if (f[o].setv!=-1){
        f[lc].setv=f[rc].setv=f[o].setv;
        f[o].setv=-1;
    }
}

void maintain(int o, int l, int r){
    int lc=(o<<1), rc=(o<<1)+1, mid=(l+r)>>1;
    if (l<r){
        f[o].sum=max(f[lc].sum, f[rc].sum);
        f[o].sum=max(f[o].sum, f[lc].rd+f[rc].ld);
        f[o].ld=f[lc].ld; if (f[lc].sum==(mid-l+1)) f[o].ld=max(f[o].ld, f[lc].sum+f[rc].ld);
        f[o].rd=f[rc].rd; if (f[rc].sum==(r-(mid+1)+1)) f[o].rd=max(f[o].rd, f[rc].sum+f[lc].rd);
    }
    if (f[o].setv==0) f[o].sum=f[o].ld=f[o].rd=r-l+1; 
    if (f[o].setv==1) f[o].sum=f[o].ld=f[o].rd=0; 
}

void update(int o, int l, int r){
    int mid=(l+r)>>1, lc=(o<<1), rc=(o<<1)+1;
    if (a<=l && r<=b) f[o].setv=v; else{
        pushdown(o);
        if (a<=mid) update(lc, l, mid); else maintain(lc, l, mid);
        if (b>mid) update(rc, mid+1, r); else maintain(rc, mid+1, r);
    }
    maintain(o, l, r);
}

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    build(1, 1, n);
    for (int i=1; i<=m; i++)
    {
        int x;
        scanf("%d", &x);
        if (x==1) {
            scanf("%d%d", &a, &b); v=1;
            b+=a-1;
            update(1, 1, n);
        }
        if (x==2){
            scanf("%d%d", &a, &b); v=0;
            b+=a-1;
            update(1, 1, n);
        }
        if (x==3) printf("%d\n", f[1].sum);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值