"POJ 3667" Hotel

Source: Internet
Author: User

Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 14238 Accepted: 6181

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 hallwa Y (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

Source

Usaco February Gold segment tree seconds. The segment tree maintains 3 variables: The value of which room can be extended back to, which room can be extended forward, and the longest continuous room in the range. First of all, say to find room operation, two points find + line tree O (log2n*log2n), each interval of two points, if the maximum value of the left interval to meet, recursive left interval, otherwise the right interval to meet on the recursion, otherwise, there is no room to meet. Into the room, into the r~r+d-1 is obviously we want to find r+d finally can extend to which room, this period is to be updated! Check out this operation is more complicated, find x-1 this room of the most can extend to which room and x+d this room finally to which room, update this section can. Although the words say so, but the details are more. The code is so wretched that you can't look straight.
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMAXN =50005;intPREV[MAXN <<2], SUFV[MAXN <<2], MSV[MAXN <<2], _last, _MSV, _fr, Y1, y2;intN, M, D;voidMaintain (intOintLintR) {    intMid = (L + r) >>1; intLC = O <<1, rc = LC +1; if(L = =R) {Msv[o]= Sufv[o]-L +1; }    Else    {        if(Sufv[o]) msv[o] = Sufv[o]-L +1; ElseMsv[o] =Max (MSV[LC], MSV[RC]); }}voidPushdown (into) {    intLC = O <<1, rc = LC +1; if(Sufv[o]) {SUFV[LC]= SUFV[RC] =Sufv[o]; Sufv[o]=0; }    if(Prev[o]) {PREV[LC]= PREV[RC] =Prev[o]; Prev[o]=0; }}voidUpdateintOintLintRintStinted) {    if(Y1 <= l && R <=y2) {Prev[o]=St; Sufv[o]=Ed; Msv[o]= Ed-l +1; }    Else{pushdown (o); intMid = (L + r) >>1; intLC = O <<1, rc = LC +1; if(Mid >=y1) Update (LC, L, Mid, St, ed); ElseMaintain (LC, L, mid); if(Mid +1<= y2) update (RC, Mid +1, R, St, ed); ElseMaintain (RC, Mid +1, R); } Maintain (O, L, r);}voidQueryintOintLintR) {    if(Sufv[o] | |Prev[o]) {_last=Sufv[o]; _fr=Prev[o]; _MSV=Max (_MSV, Msv[o]); return; }    if((Y1 <= L) && (R <=y2)) {_last=Sufv[o]; _fr=Prev[o]; _MSV=Max (_MSV, Msv[o]); }    Else    {        intMid = (L + r) >>1; intLC = O <<1, rc = LC +1; if(Mid >=y1) query (LC, L, mid); if(Mid +1<= y2) query (RC, Mid +1, R); }}intBinarySearch (intLintR) {    if(L = =r) {returnl; }    intMid = (L + r) >>1; _MSV=0; Y1=l; Y2=mid; Query (1,1, N); if(_MSV >= D)returnBinarySearch (L, mid); Else{y1= Mid +1; Y2=R; _MSV=0; Query (1,1, N); if(_MSV >= D)returnBinarySearch (Mid +1, R); }    return 0;}intMain () {scanf ("%d%d", &n, &m); memset (prev,0,sizeof(prev)); memset (SUFV,0,sizeof(SUFV)); memset (MSV,0,sizeof(msv)); Y1=1; Y2=N; Update (1,1N1, N);  for(inti =0; I < m; ++i) {intcmd; scanf ("%d%d", &cmd, &d); if(cmd = =1)        {            intR = BinarySearch (1, N); printf ("%d\n", R); if(r) {y1=R; Y2= R + D-1; Update (1,1, N,-1, -1); Y1= y2 = r +D; Query (1,1, N); if(_last >0) {y1= R +D; Y2=_last; Update (1,1, N, R +d, _last); }            }        }        Else        {            intx, ML, Mr; scanf ("%d", &x); Y1= y2 = d-1; _fr=0; if(D >1) Query (1,1, N); if(_fr >0) ml =_fr; Elseml =D; Y1= y2 = d +x; _last=0; if(d + x <= N) query (1,1, N); if(_last >0) Mr =_last; ElseMr = D + X-1; Y1=ml; Y2=Mr; Update (1,1, N, y1, y2); }    }    return 0;}

"POJ 3667" Hotel

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.