poj1823 - hotel

news/2024/10/23 7:35:07/

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;
}

http://www.ppmy.cn/news/703475.html

相关文章

hdu-1823 Luck and Love

题目链接:hdu1823二维线段树单点更新区间查询 题意 向一个100*1000的二维空间中插入点,每次查询时,查询区间最大值. 题解 身高既然是100~200,那就相当于100;活泼度相当于1000.所以建立100*1000的二维线段树. 大坑有如下几个输出-1,而不是-1.0输入的h1,h2,a1,a2,大小不一定,需要…

hdu1823

/* 分析&#xff1a; 二维线段树。 很早就明白什么是二维线段树了&#xff0c;不过没有见过二维线段树的代码&#xff0c; 想趁这次敲一个&#xff0c;不过最后发现敲的有那么一点儿点儿毛病&#xff0c;感觉没有 像二维树状数组那样发挥出二维情况下的线段树的速度&#xff0c…

poj 1823

题意&#xff1a;一个hotel&#xff0c;有n间连续的房间&#xff0c;现在有m组操作&#xff1a; type 1: 1, a, b: 第a个房间起的b个房间有旅客入住。 type 2: 2, a, b: 第a个房间起的b个房间的旅客离开。 type 3: 3: 问最长的连续空房间有多少间。 思路&#xff1a;…

codeforces 1354A 菜鸟题解

codeforces 1354A 菜鸟题解 题目链接&#xff1a;https://codeforc.es/problemset/problem/1354/A 题目解释&#xff1a; 输入为a b c d 总共要睡a分钟&#xff1b;每次休息时间为b分钟&#xff1b;闹铃每次设置后c分钟响&#xff1b;每次被闹铃吵醒需要d分钟睡着&#xff1…

Quectel EC200A-CN移植

Quectel EC200A-CN移植 一:usb转串口二:usb网卡驱动三:源码修改四:测试 一:usb转串口 usb-serial-option,USB转串口驱动&#xff0c;生产/dev/ttyUSB0-2,分别是DM&#xff0c;AT&#xff0c;PPP 需要使能内核选项如下: CONFIG_USB_SERIALy CONFIG_USB_SERIAL_WWANy CONFIG_US…

倍福TwinCAT Error starting TwinCAT System ADS 1823 / 0x71f

If ADS error 1823 (0x71f) occurs when an IP stack TcCOM object is started, the configuration of the network card is probably incorrect. Check the settings under “Adapter” for the network card in the project folder:

1823. 数组的最长前缀

1823. 数组的最长前缀 给定两个正整数X和Y&#xff0c;以及正整数数组nums。 我们需要找到一个最大的index&#xff0c;使得在nums[0], nums[1], .... , nums[index]中&#xff0c;出现X、Y的次数相等&#xff0c;且至少均出现一次&#xff0c;返回该index。 若不存在这样的ind…

Hdu-1823的题解

题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1823 Problem Description 世界上上最远的距离不是相隔天涯海角 而是我在你面前 可你却不知道我爱你 ―― 张小娴 前段日子&#xff0c;枫冰叶子给Wiskey做了个征婚启事&#xff0c;聘礼达到500万哦&#xff0c…