POJ 3667 Hotel "segment tree interval merging + lazy-tag"

Source: Internet
Author: User

Hotel

Time Limit: 3000MS

Memory Limit: 65536K

Links: POJ 3667

Description

the cows is journeying north to Thunderbay in Canada to gain cultural enrichment and enjoy a vacation on The sunnyshores of Lake Superior. Bessie, ever the competent travel agent, had named Thebullmoose Hotel on famed Cumberland Street as their vacation Residen Ce. Thisimmense Hotel has  n   (1≤  n  ≤50,000) Roomsall located on the same side of a ext remely Long hallway (all the better tosee the lake, of course).

The cows and other visitors arrive ingroups of size Di (1≤ di ≤n) and approach the front desk to check In. Each group i Requestsa set of Di contiguous rooms from Canmuu, the moosestaffing the counter. He assigns them some set of consecutive the numbers R.. R+Di-1if They be available or, if no contiguous set of rooms is available, politely suggestsalternate l Odging. Canmuu always chooses the value of R to Bethe smallest possible.

Visitors also depart the hotel from groupsof contiguous rooms. Checkout i has the parameters XI and Di whichspecify The vacating of rooms XI . XI +Di-1 (1≤ xiN-Di+ 1). Some (or all) of those rooms might is empty before the checkout.

Your job is to assist Canmuu by processing m (1≤ m < 50,000) Checkin/checkout requests. The hotel isinitially unoccupied.

Input

* Line 1:two space-separated integers: N and M
* Lines 2. M+1:line i+1 contains request expressed as Oneof II possible formats: (a) space separated integers Representing Acheck-in request:1 and Di (b) threespace-separated integers representing a check-out:2, X I, and Di

Output

* Lines 1 ...: For each check-in request,output a single line with a single integer R, the first class in Thecont Iguous sequence of rooms to be occupied. If the request cannot besatisfied, output 0.

Sample Input

10 6

1 3

1 3

1 3

1 3

2 5 5

1 6

Sample Output

1

4

7

0

5


Test Instructions:

There is a hotel, there are n consecutive rooms, the beginning of the time no one live, below the M-time ' 1 ' or ' 2 ' operation. The operation "1 D" means that there is a D person in the room, they must be living in the room, if not meet this condition, output "0", otherwise, the number of their rooms to output the smallest one; the operation "2 X D" means that the hotel starts from room number X and the D room is emptied.

Analysis:

In fact, the operation ' 1 ' can be seen as two steps, first of all, I have to find whether the [1,n] interval contains an unoccupied room with a length of D continuous, and returns the first element of the interval.

My method is the line segment tree interval merging, because in my other blog "hdu 1540/poj 2892 tunnel Warfare" segment tree interval merger "in the similar practice, I do not have a lot of cumbersome here, but this topic is relatively complex, The main idea is to have more than one lazy-tag.

The types of segment tree topics can be broadly divided into four categories: single point update, segment increment or update, interval merge and Scan line

The Lazy-tag thought is needed for both the segment updating and the interval merging.

The scanning line is the problem of finding rectangular area and perimeter, which needs to be discretized.

This article explains the interval merger, the interval merger is certainly from the child node upward can use the merger, this kind of question all is to seek the longest continuous interval, mainly in the pushup time needs to the right and left son's interval to merge.

For each node RT, it is governed by [l,r], where I use a similar pair of ln,rn,mn for each node to represent the continuous unoccupied interval length in [L,r] with L, continuous and unoccupied interval lengths ending with R, and [L,r] The maximum contiguous length of the interval that is not occupied. Notice, I mean here is not occupied Oh ~ "The specific interval merger explanation, please see"hdu 1540/poj 2892 tunnel Warfare "segment tree interval merger""in the explanation ~ ~ ~

This topic, need to update the interval, interval update <--> delay update (LAZY-TAG); then my tag array here is used to delay the update, tag[rt]=-1 means the state when the mark is cleared, Tag[rt] =0 indicates the state when the interval is occupied, TAG[RT] = 1 indicates the state of the interval is empty, generally speaking, the state of the update has I, corresponding to the value of Tag[rt] has i+1 species, because one of them is used to clear the Mark O (∩_∩) o haha ~

/****************************>>>>headfiles<<<<****************************/#include <set> #include <map> #include <list> #include <cmath> #include <queue> #include <vector > #include <cstdio> #include <string> #include <cstring> #include <iomanip> #include < iostream> #include <sstream> #include <algorithm>using namespace std;/****************************             >>>>>define<<<<<*****************************/#define FST First#define snd Second#define root 1,n,1#define lson l,mid,rt<<1#define Rson mid+1,r,rt<& Lt;1|1#define PB (a) push_back (a) #define MP (A, B) Make_pair (A, b) #define CASE (T) for (scanf ("%d", &am P T); t--;) #define FIN freopen ("Input.txt", "R", stdin) #define FOUT freopen ("Output.txt", "w", stdout)//#prag MA COMMENT (linker, "/stack:1024000000,1024000000") typedef __inT64 ll;const int INF = 0x3f3f3f3f;/****************************>>>>separator<<<<********** /const int maxn = 50000 + 5;int N, m;struct node{Int LN, RN, MN;} segtree[maxn << 2];//have n OT been occupiedint tag[maxn << 2];//-1 without tag 0 full 1 emptyinline void pushup (const int& RT,    Const int& L, const int& r) {segtree[rt].ln = Segtree[rt << 1].ln;    Segtree[rt].rn = segtree[rt << 1 | 1].rn; SEGTREE[RT].MN = max (segtree[rt << 1].rn + segtree[rt << 1 | 1].ln, max (Segtree[rt &lt    ;< 1].mn, segtree[rt << 1 | 1].mn));    int mid = (L + r) >> 1;    if (segtree[rt << 1].mn = = mid-l + 1) segtree[rt].ln + = Segtree[rt << 1 | 1].ln; if (segtree[rt << 1 | 1].mn = = R-(mid + 1) + 1) segtree[rt].rn + = Segtree[rt << 1].rn;}   inline void pushdown (const int& RT, const int& L, const int& R) { if (TAG[RT]! =-1) {tag[rt << 1] = tag[rt << 1 | 1] = TAG[RT];        int mid = (L + r) >> 1;        Segtree[rt << 1].ln = segtree[rt << 1].rn = segtree[rt << 1].mn = tag[rt] * (mid-l + 1);  Segtree[rt << 1 | 1].ln = segtree[rt << 1 | 1].rn = segtree[rt << 1 | 1].mn = tag[rt] * (R-(mid + 1) +        1);    TAG[RT] =-1;    }}void Build (int l, int r, int rt) {Segtree[rt].ln = Segtree[rt].rn = segtree[rt].mn = r-l + 1;    TAG[RT] = 1;    if (L = = r) return;    int mid = (L + r) >> 1;    Build (Lson); Build (Rson);}        void Update (int l, int r, const bool& isclear, int l, int r, int rt) {if (L <= l && R <= R) {            if (isclear) {segtree[rt].ln = Segtree[rt].rn = segtree[rt].mn = r-l + 1;        TAG[RT] = 1;            } else {segtree[rt].ln = Segtree[rt].rn = segtree[rt].mn = 0;        TAG[RT] = 0;    } return; } PushDOwn (RT, L, R);    int mid = (L + r) >> 1;    if (l <= mid) Update (L, R, Isclear, Lson);    if (Mid < R) Update (L, R, Isclear, Rson); Pushup (RT, L, R);}    int Query (const int& len, int l, int r, int rt) {if (segtree[rt].ln >= len) return l;    int mid = (L + r) >> 1;    if (segtree[rt << 1].mn >= len) return Query (Len, Lson); else if (segtree[rt << 1].rn + segtree[rt << 1 | 1].ln >= Len) return mid-segtree[rt << 1].    RN + 1; else return Query (Len, Rson);}    int Op, X, D, Ans;int Main () {//FIN;        while (~SCANF ("%d%d", &n, &m)) {Build (root);            while (m--) {scanf ("%d", &op);                if (Op & 1) {scanf ("%d", &d);                    if (Segtree[1].mn < D) {printf ("0\n");                Continue                } ans = Query (D, Root); printf ("%d\N ", ans);            Update (ans, ans + D-1, false, root);                } else {scanf ("%d%d", &x, &d);            Update (x, X + D-1, true, root); }        }    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 3667 Hotel "segment tree interval merging + lazy-tag"

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.