Poj 3667 Hotel (segment tree interval allocation)

Source: Internet
Author: User
Hotel
Time limit:3000 Ms   Memory limit:65536 K
Total submissions:9550   Accepted:4089

Description

The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. bessie, ever the competent travel agent, has named the bullmoose Hotel on famed Cumberland street as their vacation
Residence. This immense hotel hasN(1 ≤N≤ 50,000) rooms all located on the same side of an extremely long hallway (all the better to see the lake, of course ).

The cows and other visitors arrive in groups of sizeDi(1 ≤Di≤ N) and approach the front desk to check in. Each groupIRequests a setDiContiguous rooms from canmuu, the moose staffing
The counter. He assigns them some set of consecutive room numbersR..R+Di-1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. canmuu always chooses the valueRTo
Be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. CheckoutIHas the parametersXIAndDiWhich specify the vacating of roomsXI..XI+Di-1
(1 ≤XIN-Di+ 1). Some (or all) of those rooms might be empty before the checkout.

Your job is to assist canmuu by processingM(1 ≤M<50,000) checkin/Checkout requests. The hotel is initially unoccupied.

Input

* Line 1: two space-separated integers:NAndM
* Lines 2 ..M+ 1: LineI+ 1 contains request expressed as one of two possible formats: (a) two space separated integers representing a check-in request: 1 andDi(B) Three space-separated integers representing a check-out:
2,XI, AndDi

Output

* Lines 1...: For each check-in request, output a single line with a single integerR, The first room in the contiguous sequence of rooms to be occupied. If the request cannot be satisfied, output 0.

Sample Input

10 61 31 31 31 32 5 51 6

Sample output

14705

Source

Usaco 2008 February gold

Start writing with your own ideas. It took me one afternoon. Finally, all the test examples have passed. But it's wa. Instead, you only need to go online to refer to the online code and find that the idea is almost the same as your own. The method for updating the parent node information is different. Because of the time relationship, you don't have to worry about your own code. Changed according to the online update method. That's.

Ideas:

Since we want to query whether there is a continuous null interval with a length of D, we should at least record the maximum continuous null interval in each interval, this is represented by Ma [] (Max all. At the same time, there are three forms of continuous null intervals in the search process: Left, right, or two. Of course, we need to first find the left subtree and then find the right subtree. What should we do with this cross-tree in the middle? So we can introduce ml [] (Max left) to indicate the maximum continuous interval from the left, Mr [] (Max right) indicates the maximum continuous interval from the right side of an interval.
Son] + ml [right son]> = D, it indicates that there is such an interval spanning two Subtrees. Because we know the coordinate X of the rightmost vertex of the Left subtree, the first position of this continuous null interval is naturally X-Mr [left son] + 1.
After the search function is implemented, the remaining part is dyeing. If it is checked in, it will be dyed as 1. If it is moved out, it will be dyed as-1.
In addition, after dyeing, we only need to update the status of the parent node ma [], ML [], and Mr []. In this case, for the parent node, in addition to the longest continuous interval in the left subtree and the longest continuous interval in the right subtree, the longest continuous interval across the two subtree is also considered.

During the update, the lazy mark is added directly to the matching interval. You can just take it next time you pass.

For details, see the code:

# Include <iostream> # include <string. h> # include <stdio. h> # define max (A, B) (a)> (B )? (A) :( B) # define min (A, B) (a) <(B )? (A) :( B) # define positive (A) (a)> 0? (A):-(a) using namespace STD; const int maxn = 50100; int ml [maxn <2], Mr [maxn <2], ma [maxn <2]; int st [maxn <2]; int n, m; void btree (int l, int R, int K) // build {int ls, RS, mid; MA [k] = ML [k] = Mr [k] = (R-L + 1); // initialize. Ma indicates the maximum number of consecutive rooms in the interval. ML indicates the maximum number of consecutive rooms on the left end of the interval. Mr is the right side .. St [k] = 0; // 0 indicates no mark. 1 indicates that all rooms are occupied in the interval. -1 indicates that all rooms in the interval are idle if (L = r) return; LS = k <1; RS = k <1 | 1; mid = (L + r)> 1; btree (L, mid, ls); btree (Mid + 1, R, RS);} void change (INT l, int R, int K) // modify the node information based on the Status {If (ST [k] = 1) // occupy ml [k] = Mr [k] = ma [k] = 0; else ml [k] = Mr [k] = ma [k] = R-L + 1;} void Pushdown (int l, int R, int K) // mark {int ls, RS, mid; LS = k <1; RS = k <1 | 1; Mid = (L + r)> 1; if (ST [k] = 1) // occupies {st [ls] = sT [RS] = 1; ML [ls] = Mr [ls] = ma [ls] = 0; ML [RS] = Mr [RS] = ma [RS] = 0 ;} else {st [ls] = s T [RS] =-1; ML [ls] = Mr [ls] = ma [ls] = mid-L + 1; ML [RS] = Mr [RS] = ma [RS] = r-mid;} st [k] = 0;} void check (int l, int R, int l, int R, int K, int s) {int ls, RS, mid; If (L = L & R = r) {st [k] = s; change (L, R, k); return;} ls = k <1; RS = k <1 | 1; Mid = (L + r)> 1; if (ST [k]) Pushdown (L, R, k); If (L> mid) Check (Mid + 1, R, L, R, RS, S ); else if (r <= mid) Check (L, mid, L, R, ls, S); else {check (L, mid, L, mid, ls, S ); check (Mid + 1, R, Mid + 1, R, RS, S);} Ma [k] = ma X (MA [ls], ma [RS]); MA [k] = max (MA [K], Mr [ls] + ml [RS]); // find the maximum interval ml [k] = ML [ls]; // The most basic value Mr [k] = Mr [RS]; if (MA [ls] = mid-L + 1) // if the right range is full ml [k] + = ML [RS]; // possible value: If (MA [RS] = r-mid) Mr [k] + = Mr [ls]; // printf ("% d-> % d ma: % d ml: % d Mr: % d \ n", l, R, ma [K], ML [K], Mr [k]);} int Qu (int l, int R, int K, int cost) {int ls, RS, mid; if (cost> ma [k]) return 0; If (L = r) return l; LS = k <1; RS = k <1 | 1; mid = (L + r)> 1; if (ST [k]) Pushdown (L, R, k); If (Co St <= ma [ls]) // The return Qu (L, mid, ls, cost) on the left is given priority ); else if (cost <= Mr [ls] + ml [RS]) return mid-Mr [ls] + 1; else return Qu (Mid + 1, R, RS, cost) ;}int main () {int COM, I, ANS, x, D; while (~ Scanf ("% d", & N, & M) {btree (1, n, 1); for (I = 1; I <= m; I ++) {scanf ("% d", & Com); If (COM = 1) {scanf ("% d", & D ); ans = Qu (1, n, 1, D); printf ("% d \ n", ANS); If (ANS) Check (1, n, ANS, ans + D-1, 1, 1);} else {scanf ("% d", & X, & D); check (1, n, x, x + D-1, 1, -1); }}return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.