The third Henan Province Program design Competition-nyoj-250-room assignation

Source: Internet
Author: User

The assignation
Time limit: Ms | Memory Limit: 65535 KB
Difficulty: 4
Describe
The "cows" is journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny Sho Res 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 d_i (1 <= d_i <= N) and approach the front desk to check in. Each group I requests a set of d_i contiguous rooms from Canmuu, the moose staffing the counter. He assigns them some set of consecutive the numbers R.. R+d_i-1 if they is 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 x_i and d_i which specify the vacating of rooms x_i ... X_i+d_i-1 (1 <= x_i <= n-d_i+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
There is multi test cases. EOF would terminate the 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) space separated integers representing a check-in request:1 and d_i
(b) Three space-separated integers representing a check-out:2, x_i, and D_i
Output
Lines 1 ...: For each check-in request, output a single line with a single integer r, the
First contiguous sequence of rooms to be occupied. If the request
cannot be satisfied, 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

More complex line-segment trees
Unable to write ideas, indicating more comments than code

#include <stdio.h>#include <iostream>#include <algorithm>#include <string.h>using namespace STD;Const intmaxn=50005;//Maximum number of roomsintN//Number of roomsintM//Number of instructionsstructnode{intLeft//Left point    intRight//Right endpoint    intIs_free;//Record room status in the current interval    intNum//The maximum number of consecutive rooms in the range    intLeft_num;//range from left end to right maximum number of rooms    intRight_num;//range from right end to left maximum number of rooms} tree[maxn*4];//Segment Tree arrayvoidBuild (intRootintLeftintRight//To the left and right intervals of the root node and root node of the achievement{tree[root].left=left;    Tree[root].right=right; Tree[root].is_free=0;//Initialize the current range with free roomstree[root].num=tree[root].left_num=tree[root].right_num=right-left+1;//These three numeric values are initialized identically    if(Left<right)//If this is not the lowest level, proceed to the achievement{intMid= (left+right) >>1;//Take the left and right end of the current intervalBuild (root<<1, Left,mid);//Establish left cotyledonsBuild (root<<1|1, mid+1, right);//Build right cotyledons}}intQuery (intRootintNum_num)//Incoming root node and number of people required to stay, return to the first room number{if(Tree[root].num<num_num)//If the maximum number of consecutive empty rooms in the current interval is insufficient        return 0;//Direct return 0    if(Tree[root].left_num>=num_num)//If the left end of the current interval starts with a continuous number of vacant rooms, sufficient        returnTree[root].left;//Return left endpoint coordinates    if(tree[root<<1].num>=num_num)//If there is a number of consecutive rooms satisfying the conditions within the left dial hand leaf range        returnQuery (root<<1, Num_num);//Recursive query left sub-leaf    Else if(tree[root<<1].right_num+tree[root<<1|1].left_num>=num_num)//If the number of consecutive rooms Zo from right to left, plus the number of consecutive rooms with right cotyledons from left to right, can meet the number of consecutive room occupancy        returntree[root<<1].right-tree[root<<1].right_num+1;//Return Zo right end number minus, Zo the number of consecutive rooms from the right endpoint to the left and add a    Else        returnQuery (root<<1|1, Num_num);//Go to right interval to find}voidUpdate (intRootintLeftintRightintFlag//Incoming root node, to be updated interval, flag==1? Update to Checked-in: Update to check out{if(Tree[root].left==left&&tree[root].right==right) {Tree[root].is_free=flag;//Record the current interval room status        if(flag==1)//Update interval is checked intree[root].left_num=tree[root].right_num=tree[root].num=0;//Update current interval is checked in        Elsetree[root].left_num=tree[root].right_num=tree[root].right-tree[root].left+1;//update current interval for check out}Else if(Tree[root].left<tree[root].right)//Not at the lowest level, continue to update recursively{///Next is data sinking        if(tree[root].is_free==1)//If the current interval is checked in{tree[root<<1].is_free=tree[root<<1|1].is_free=1;//That its left and right sub-leaves must also have been stayingtree[root<<1].left_num=tree[root<<1].right_num=tree[root<<1].num=0;///left and right sub-leaves free room number clear 0tree[root<<1|1].left_num=tree[root<<1|1].right_num=tree[root<<1|1].num=0;//This is the right sub-lobe}if(tree[root].is_free==0)//If the current interval is updated to checked out{tree[root<<1].is_free=tree[root<<1|1].is_free=0;//That its left and right cotyledons must also have checked outtree[root<<1].left_num=tree[root<<1].right_num=tree[root<<1].num=tree[root<<1].right-tree[root<<1].left+1;the number of consecutive rooms starting at the left and right of the left and right lobes is the total room in the intervaltree[root<<1|1].left_num=tree[root<<1|1].right_num=tree[root<<1|1].num=tree[root<<1|1].right-tree[root<<1|1].left+1;//Notes Ibid .}intMid= (tree[root].left+tree[root].right) >>1;//Take interval midpoint        if(Mid>=right)//If the interval to be updated is completely in the left sub-leaf portion of the current intervalUpdate (root<<1, Left,right,flag);//Update left cotyledons        Else if(Mid<left)//If the interval to be updated is completely in the right sub-lobe section of the current intervalUpdate (root<<1|1, Left,right,flag);//This step may be wrong        Else//If the interval to be updated spans the current section{Update (root<<1, Left,mid,flag);//Update left cotyledonsUpdate (root<<1|1, mid+1, Right,flag);//Update right sub-lobe}///Next is the data on the pushtree[root].left_num=tree[root<<1].left_num; tree[root].right_num=tree[root<<1|1].right_num;if(tree[root<<1].is_free==0)//If the left dial hand leaf interval is checked out, the entire room of the left cotyledons is emptytree[root].left_num+=tree[root<<1|1].left_num;///current interval left end to right continuous empty room number, plus right interval left end to right consecutive empty room number        if(tree[root<<1|1].is_free==0)//If the right sub-leaf interval is checked out, the entire range of the right cotyledons is emptytree[root].right_num+=tree[root<<1].right_num;///current interval right end to left consecutive empty room number, plus left interval right end to left consecutive empty room numberTree[root].num=max (tree[root<<1].num,tree[root<<1|1].num);///The maximum number of consecutive vacant rooms in the current interval takes maximum from left and right sub-leavesTree[root].num=max (tree[root].num,tree[root<<1].right_num+tree[root<<1|1].left_num);the maximum number of consecutive vacant rooms in the current interval, taking self and Zo from the right to the left continuous room number plus the right cotyledons from left to right continuous availability        if(tree[root<<1].is_free==tree[root<<1|1].is_free)//If the left and right sub-lobes have the same statetree[root].is_free=tree[root<<1].is_free;Else///left and right sub-leaf interval states are differenttree[root].is_free=-1;//Current interval status marked as-1}}intMain () { while(~scanf("%d%d", &n,&m)) {intFlag//For 1 o'clock, check-in, 2 o'clock, check-out        intStar_num;intNum_num; Build (1,1, N);//Create line segment tree         while(m--)//m Group Input{scanf("%d", &flag);if(flag==1)//check-in left end room number{scanf("%d", &num_num);intPoint_num=query (1, Num_num);printf("%d\n", Point_num);if(point_num!=0)//Update check -inUpdate (1, point_num,point_num+num_num-1,1);///Last 1, representing this interval needs to be accommodated                //There is still a missing update}Else//Update check-out{scanf("%d%d", &star_num,&num_num); Update (1, star_num,star_num+num_num-1,0);///Last 0, representing this interval needs to be emptied}        }    }return 0;}

The third Henan Province Program design Competition-nyoj-250-room assignation

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.