Hotel
Time Limit: 3000MS |
|
Memory Limit: 65536K |
Total Submissions: 13124 |
|
Accepted: 5664 |
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≤ xi ≤ N-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
The main topic: Tourists want to live hotel, hotel total room n number 1-n, have M inquiry, if the first number is 1, the second number is a to ask whether there is a continuous room a room, if there is, output this room number of the first room, if there is no output 0. If the first number is 2, and the second to third number is a, a, A/b means that other visitors check out and will return the continuous B room from a.
Problem-solving idea: using three arrays to record the continuous interval length from the left end of the node, the continuous interval length from the right end point of the node and the longest continuous interval length of the node. Cover represents the current state of each node.
/* Segment tree interval merging: query operation implementation querying the left endpoint update operation of a continuous interval update an interval pushup operation realizes merging the interval between the left and right son Pushdown operation implements delay tag move down to son/#include <stdio.h> #include <string.h> #include <algorithm>using namespace STD; #define MID (L+r)/2#define lson rt*2,l,mid#define rson rt*2+1,mid+1,rconst int maxn=60000;//record node left, The right end and the node represent the continuous interval length int l_sum[maxn*4],r_sum[maxn*4],rt_sum[maxn*4];int cover[maxn*4];//labeled node state, empty, full, initial value void build (int RT, int L,int R) {l_sum[rt]=r_sum[rt]=rt_sum[rt]= (r-l+1); Cover[rt]=-1; if (l==r) return; Build (Lson); Build (Rson);} void pushup (int rt,int len) {l_sum[rt]=l_sum[rt*2]; R_SUM[RT]=R_SUM[RT*2+1]; if (l_sum[rt]==len-(LEN/2)) {///If the left son has a continuous interval length of the left son node jurisdictional length, then the continuous interval at the left end of the node can be amplified, continuing with the right son's continuous interval from the left side l_sum[rt]+=l_sum[rt*2+1] ; if (R_SUM[RT]==LEN/2) {///If the right side of the right end of the continuous interval length is the right son node jurisdictional length, then the node at the right end of the continuous interval length can be amplified, continue to add the left son from the right end of the continuous interval r_sum[rt]+=r_sum[rt*2]; }//Update the interval length of the node with the maximum or combined maximum of the left and right son interval length Rt_sum[rt]=max (max (rt_sum[rT*2],rt_sum[rt*2+1]), r_sum[rt*2]+l_sum[rt*2+1]);} void pushdown (int rt,int len) {if (cover[rt]!=-1) {COVER[RT*2]=COVER[RT*2+1]=COVER[RT]; L_SUM[RT*2]=R_SUM[RT*2]=RT_SUM[RT*2]=COVER[RT]?0:LEN-LEN/2; L_SUM[RT*2+1]=R_SUM[RT*2+1]=RT_SUM[RT*2+1]=COVER[RT]?0:LEN/2; Cover[rt]=-1; }}void Update (int rt,int l,int r,int flg,int l_ran,int R_ran) {if (L_ran<=l&&r<=r_ran) {//The node is within the inquiry interval L_sum[rt]=r_sum[rt]=rt_sum[rt]= flg?0:r-l+1; COVER[RT]=FLG; return; } pushdown (rt,r-l+1); Delay tag Move Down if (l_ran<=mid) {update (Lson,flg,l_ran,r_ran); } if (R_ran>mid) {update (Rson,flg,l_ran,r_ran); } pushup (rt,r-l+1); Merge out a continuous interval}int query (int rt,int l,int r,int sum) {if (l==r) {return L; Returns the leftmost endpoint of the interval that satisfies the condition} pushdown (rt,r-l+1); The delay tag moves down if (rt_sum[rt*2]>=sum) {return query (lson,sum);//The longest interval length of the left son can satisfy the requirement} if (r_sum[rt*2]+l_sum[rt*2 +1]>=sum) {//left son right end continuous interval length plus right sonThe left-left continuous interval length can satisfy the request return mid-r_sum[rt*2]+1; } return query (Rson,sum); Ask the right son}int main () {int n,m; while (scanf ("%d%d", &n,&m)!=eof) {build (1,1,n); for (int i=0;i<m;i++) {int type,st,num; scanf ("%d", &type); if (type==1) {scanf ("%d", &num); if (rt_sum[1]<num) {printf ("0\n"); Continue } st=query (1,1,n,num); printf ("%d\n", ST); Update (1,1,N,1,ST,ST+NUM-1); }else{scanf ("%d%d", &st,&num); Update (1,1,N,0,ST,ST+NUM-1); }}} return 0;}
Poj 3667--hotel —————— "segment tree interval Merging"