Seating

发布于 2021-10-04  157 次阅读


Seating

时间限制: 1 Sec  内存限制: 64 MB

题目描述

To earn some extra money, the cows have opened a restaurant in their barn specializing in milkshakes.  The restaurant has N seats (1 <= N <= 500,000) in a row. Initially, they are all empty.

Throughout the day, there are M different events that happen in sequence at the restaurant (1 <= M <= 300,000).  The two types of events that can happen are:

1. A party of size p arrives (1 <= p <= N). Bessie wants to seat the party in a contiguous block of p empty seats. If this is possible, she does so in the lowest position possible in the list of seats.  If it is impossible, the party is turned away.

2. A range [a,b] is given (1 <= a <= b <= N), and everybody in that range of seats leaves.

Please help Bessie count the total number of parties that are turned away over the course of the day.

输入

* Line 1: Two space-separated integers, N and M.
* Lines 2..M+1: Each line describes a single event.  It is either a line of the form "A p" (meaning a party of size p arrives) or  "L a b" (meaning that all cows in the range [a, b] leave).

输出

* Line 1: The number of parties that are turned away.

样例输入

 

样例输出

提示

There are 10 seats, and 4 events.  First, a party of 6 cows arrives.  Then all cows in seats 2..4 depart.  Next, a party of 5 arrives, followed by a party of 2.Party #3 is turned away.  All other parties are seated.

题解

主要是通过线段树求是否存在某个区间是否有连续的p个1,如果有,找出最靠左的这样的一个长度为p的区间。

比较复杂的线段树,类似小白逛公园。这里的做法可能比较麻烦,线段树维护四个值,每个区间左边连续1的个数lval,中间连续1的个数midval,右边连续1的个数rval,及整个区间中最多的连续的1的个数max。查询时如果当前区间max<p直接就是找不到符合要求的区间,返回-1;如果当前区间的max>=p,则这个>p的值可能在左边子区间,中间子区间,或右子区间。如果当前区间的lval>=p,则直接返回当前区间的左端点即可;如果左子区间的max>=p则在左子区间,继续向左子区间查询;若不在左子区间,当前区间的midval>=p的话,最靠左的符合要求的区间的左端点为midval-左子区间rval+1;否则继续向右子区间查询。

代码

 


In case I don't see you, good afternoon, good evening and good night.