P2894 [usaco 08feb] Hotel, p2894usaco 08feb

Source: Internet
Author: User

P2894 [usaco 08feb] Hotel, p2894usaco 08feb
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 has N (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 size Di (1 ≤ Di ≤ N) and approach the front desk to check in. each group I requests a set of Di contiguous rooms from Canmuu, the moose staffing the counter. he assigns them some set of consecutive room numbers r .. r + Di-1 if they are available or, if no contiguous set of rooms is available, politely suggests alternate lodging. canmuu always chooses the value of r to be the smallest possible.

Visitors also depart the hotel from groups of contiguous rooms. checkout I has the parameters Xi and Di which specify the vacating of rooms Xi .. xi + Di-1 (1 ≤ Xi ≤ N-Di + 1 ). some (or all) of those rooms might be empty before the checkout.

Your job is to assist Canmuu by processing M (1 ≤ M <50,000) checkin/checkout requests. The hotel is initially unoccupied.

For reference, input n, m, and n in the first line indicates that there are n rooms, numbered 1 --- n, and all rooms are available at the beginning. m indicates that there are m rows below, input a number I in each of the following lines, indicating an operation:

If I is 1, it indicates querying the room, and then entering a number of x, it indicates that the continuous vacant room with the length of x is found in Room 1 -- n, output the number of the left-side room in x consecutive rooms. Try to minimize the number of the room. If no consecutive vacant room with the length of x is found, 0 is output.

If I is 2, it indicates check-out, then enter two numbers x, y indicates the room number x --- x + Y-1 check-out, that is, leave the room empty.

Input/Output Format

Input Format:

 

  • Line 1: Two space-separated integers: N and M

  • Lines 2 .. M + 1: Line I + 1 contains request expressed as one of two possible formats: (a) Two space separated integers representing a check-in request: 1 and Di (B) three space-separated integers representing a check-out: 2, Xi, and Di

 

Output Format:

 

  • Lines 1 .....: For each check-in request, output a single line with a single integer r, the first room in the contiguous sequence of rooms to be occupied. if the request cannot be satisfied, output 0.

 

Input and Output sample input sample #1:
10 61 31 31 31 32 5 51 6
Output example #
4705


At first, I thought that the line segment tree of the question in the classroom was very similar, but I found that the two questions were not at the same level after careful consideration...
The general idea of this question is,
Set no rent to 1 and rent to 0
For each interval l, r
1. Save the maximum length from l to 0
2. Save the longest length from r to 0
3. Save the maximum length of 0 in the entire interval
4. Length of the storage Interval
For each update, we also consider the original idle state of the left child, the original idle state of the right child, and the new idle state of the middle part when they are combined.

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # define lli long int 6 # define ls k <1 7 # define rs k <1 | 1 8 using namespace std; 9 const int MAXN = 100001; 10 inline void read (int & n) 11 {12 char c = '+'; int x = 0; bool flag = 0; 13 while (c <'0' | c> '9') {c = getchar (); if (c = '-') flag = 1 ;} 14 while (c> = '0' & c <= '9') {x = x * 10 + c-48; c = getchar ();} 15 flag = 1? N =-x: n = x; 16} 17 struct node 18 {19 lli l, r, w, f, lfree, rfree, allfree, chang; 20} tree [MAXN <2]; 21 int n, m; 22 inline void update (int k) 23 {24 if (tree [ls]. allfree = tree [ls]. chang) 25 tree [k]. lfree = (tree [ls]. chang + tree [rs]. lfree); 26 else 27 tree [k]. lfree = (tree [ls]. lfree); 28 29 if (tree [rs]. allfree = tree [rs]. chang) 30 tree [k]. rfree = (tree [rs]. chang + tree [ls]. rfree); 31 else 32 tree [k]. rfree = tree [rs]. rfree; 33 34 tree [k]. allfree = max (tree [ls]. allfree, tree [rs]. allfree); 35 tree [k]. allfree = max (tree [k]. allfree, tree [ls]. rfree + tree [rs]. lfree); 36 37 return; 38} 39 inline void build_tree (int ll, int rr, int k) 40 {41 tree [k]. l = ll; tree [k]. r = rr; 42 tree [k]. chang = (tree [k]. r-tree [k]. l + 1); 43 if (ll = rr) 44 {tree [k]. lfree = tree [k]. rfree = tree [k]. allfree = (tree [k]. r-tree [k]. l + 1); return;} 45 lli mid = (tree [k]. l + tree [k]. r)> 1; 46 build_tree (ll, mid, ls); 47 build_tree (mid + 1, rr, rs); 48 update (k ); 49} 50 inline void pushdown (int k, int how) 51 {52 if (how = 1) 53 {54 tree [ls]. lfree = tree [ls]. rfree = tree [ls]. allfree = 0; 55 tree [rs]. lfree = tree [rs]. rfree = tree [rs]. allfree = 0; 56 tree [ls]. f = tree [k]. f; 57 tree [rs]. f = tree [k]. f; 58 tree [k]. f = 0; 59} 60 else 61 {62 tree [ls]. lfree = tree [ls]. rfree = tree [ls]. allfree = tree [ls]. chang; 63 tree [rs]. lfree = tree [rs]. rfree = tree [rs]. allfree = tree [rs]. chang; 64 tree [ls]. f = tree [k]. f; 65 tree [rs]. f = tree [k]. f; 66 tree [k]. f = 0; 67} 68 return; 69} 70 inline int query (int k, int num) 71 {72 if (tree [k]. f) 73 pushdown (k, tree [k]. f); 74 if (tree [k]. r = tree [k]. l) 75 return tree [k]. l; 76 if (tree [ls]. allfree> = num) 77 return query (ls, num); 78 if (tree [ls]. rfree + tree [rs]. lfree> = num) 79 return tree [ls]. r-tree [ls]. rfree + 1; 80 else 81 return query (rs, num); 82} 83 inline void change (int k, int ll, int rr, int how) 84 {85 if (ll <= tree [k]. l & tree [k]. r <= rr) 86 {87 if (how = 1) 88 tree [k]. allfree = tree [k]. lfree = tree [k]. rfree = 0; 89 else 90 tree [k]. allfree = tree [k]. lfree = tree [k]. rfree = tree [k]. chang; 91 92 tree [k]. f = how; 93 return; 94} 95 int mid = (tree [k]. l + tree [k]. r)> 1; 96 if (tree [k]. f) 97 pushdown (k, tree [k]. f); 98 if (ll <= mid) 99 change (ls, ll, rr, how); 100 if (rr> mid) 101 change (rs, ll, rr, how); 102 update (k); 103} 104 int main () 105 {106 read (n); read (m); 107 build_tree (1, n, 1 ); 108 for (int I = 1; I <= m; I ++) 109 {110 int how; 111 read (how); 112 if (how = 1) 113 {114 int num; 115 read (num); 116 if (tree [1]. allfree <num) 117 {118 printf ("0 \ n"); 119 continue; 120} 121 int pos = query (1, num); 122 change (1, pos, pos + num-1, 1); // place 123 printf ("% d \ n", pos); 124} 125 else126 {127 int xx, yy; 128 read (xx ); read (yy); 129 change (1, xx, xx + yy-1, 2); // clear 130} 131} 132 return 0; 133}

 



Related Article

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.