poj--3667 Hotel (segment tree + interval merge)

Source: Internet
Author: User

Description

The cows is journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shore S of Lake Superior. Bessie, ever the competent travel agent, had named the Bullmoose Hotel on famed Cumberland Street as their vacation reside nCE. This immense hotel have n (1≤ n≤50,000) rooms all located on the same side of a 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 Chec K 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 the numbers R.. R+Di-1 if they is available or, if the 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≤ 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 is initially unoccupied.

Input

* Line 1:two space-separated integers: N and M
* Lines 2. M+1:line i+1 contains request expressed as one of the possible formats: (a) II space separated integer S representing a check-in request:1 and Di (b) Three space-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 the CO Ntiguous 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
Test instructions: Hotel n rooms, 1 is a group stay, 2 is the team left. If there is a continuous room enough for the team to stay in the smallest room, print 0.
idea: First of all to use 5 functions are: achievements, lazy tag, the child node to merge, insert new data, search the starting room number A lazy 1 indicates that the node's child node has been updated, 0 o'clock indicates that the node is empty room, 1 o'clock that the node room full
AC Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5 using namespacestd;6 Const intmaxn=50000+Ten;7 structNote8 {9     intL,r,lm,rm,m,lazy;Ten} a[maxn<<2]; One intBuildintLintRintk) A { -a[k].l=l,a[k].r=r,a[k].lm=a[k].rm=a[k].m=r-l+1, a[k].lazy=-1; -     if(l==R) the         return 0; -     intMid= (L+R)/2; -Build (l,mid,k*2); -Build (mid+1, r,k*2+1); +     return 0; - } + intPushdown (intKintd) A { at     if(a[k].lazy!=-1) -     { -a[k*2].lazy=a[k*2+1].lazy=A[k].lazy; -a[k*2].rm=a[k*2].lm=a[k*2].m=a[k].lazy?0:d-d/2; -a[k*2+1].rm=a[k*2+1].lm=a[k*2+1].m=a[k].lazy?0:D/2; -a[k].lazy=-1; in     } -     return 0; to } + intPushup (intKintd) - { thea[k].lm=a[k*2].lm; *a[k].rm=a[k*2+1].rm; $     if(a[k].lm==d-d/2)Panax Notoginsenga[k].lm+=a[k*2+1].lm; -     if(a[k].rm==d/2) thea[k].rm+=a[k*2].rm; +A[k].m=max (a[k*2+1].m,max (a[k*2].m,a[k*2].rm+a[k*2+1].lm)); A     //Note Max (the maximum value of the left node, the maximum value of the right node, the left node's maximum value from the right side, and the maximum value from the left side of the right node) the     return 0; + } - intInsintLintRintNintk) $ { $     if(l<=a[k].l&&a[k].r<=R) -     { -A[k].lm=a[k].rm=a[k].m=n?0:(a[k].r-a[k].l+1); thea[k].lazy=N; -         return 0;Wuyi     } thePushdown (k,a[k].r-a[k].l+1); -     if(l<=a[k*2].R) Ins (l,r,n,k*2); Wu     if(r>a[k*2].R) Ins (l,r,n,k*2+1); -Pushup (k,a[k].r-a[k].l+1); About     return 0; $ } - intQueryintDintk) - { -     if(a[k].l==A[K].R) A         return 1; +Pushdown (k,a[k].r-a[k].l+1); the     if(a[k*2].m>=d) -         returnQuery (d,k*2); $     Else if(a[k*2].rm+a[k*2+1].lm>=d) the         returna[k*2].r-a[k*2].rm+1; the     Else theQuery (d,k*2+1); the } - intMain () in { the     intn,m,b,c,d; thescanf"%d%d",&n,&m); About     { theBuild1N1); the          while(m--) the         { +scanf"%d",&b); -  the             if(b==1)Bayi             { thescanf"%d",&c); the                 if(a[1].m<c) -printf"0\n"); -                 Else the                 { the                     intAns=query (c,1); theprintf"%d\n", ans); theINS (ans,ans+c-1,1,1); -                 } the             } the             Else the             {94scanf"%d%d",&c,&d); theINS (c,c+d-1,0,1); the             } the         }98     } About     return 0; -}
View Code

Experience: Although the code is not in accordance with their own ideas to come out, but the feeling is happy for the line segment tree and have a deeper understanding, whether it is achievements, lazy mark. In a word, we need to use line tree to perform various operations (although this is a silly ^_^)



poj--3667 Hotel (segment tree + interval merge)

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.