Hotel (segment tree merge)

Source: Internet
Author: User

Hotel
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 14958 Accepted: 6450

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 an extre Mely 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 Check 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, Numbers  R .. R + Di -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  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 Reques Ts. 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 chec K-out:2, Xi, and Di

Output

* Lines 1 ...: For each check-in request, output a single line with a single integer r, the first class in th e contiguous 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
Exercises
N ≤50,000) between rooms, and all rooms are arranged consecutively on the same side, groups need check in room,
The number of rooms required is continuous R. R+Di-1 and R is the smallest; Visitors may also check out,
And they each check out is numbered as Xi . XI +diXIN-di+ 1) of the room, the input of the topic has two styles:
    1. 1 a:groups need check in a room number continuous
    2. 2 A b:visitors check out room, where room number is a...a+b-1

Requires a room number with the smallest number in a room with a number assigned to groups for each request


The model is established by the segment tree to maintain the maximum continuous interval length, where the interval length is the corresponding number of rooms, and the leftmost breakpoint in the corresponding interval is answer,
At the same time, because the maximum length of the continuous interval needs to be required, the left and right intervals are merged at each pushup.
Lsum maintain the maximum length of the left interval, rsum maintain the maximum length of the right interval, sum maintenance interval 1 ... Maximum continuous interval length in n, lazy delay mark;
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Cmath> #include <vector>using namespace std;const int inf=0x3f3f3f3f; #define MEM (x, y) memset (x,y,sizeof (×)) # Define SI (x) scanf ("%d", &x) #define PI (x) printf ("%d", x) #define SD (y) scanf ("%lf%lf", &x,&y) #define P_ printf ("") #define LL Root<<1#define RR root<<1|1#define lson ll,l,mid#define Rson rr,mid+1,rtypedef Long Lon G Ll;const int Maxn=50010<<2;int sum[maxn],lsum[maxn],rsum[maxn];int lazy[maxn];void pushdown (int root,int v) {if (Lazy[root]!=-1) {lazy[ll]=lazy[rr]=lazy[root];sum[ll]=lsum[ll]=rsum[ll]=lazy[root]?0:v-(v>>1); sum[rr]=lsum[rr]=rsum[rr]= Lazy[root]?0:v>>1;lazy[root]=-1;}} void pushup (int root,int v) {lsum[root]=lsum[ll];rsum[root]=rsum[rr];if (lsum[root]==v-(v>>1)) lsum[root]+= Lsum[rr];if (rsum[root]== (v>>1)) Rsum[root]+=rsum[ll];sum[root]=max (Lsum[rr]+rsum[ll],max (SUM[LL],SUM[RR)) );} void build (int root,int l,int r) {int Mid= (l+r) >>1;sum[root]=lsum[root]=rsum[root]=r-l+1;lazy[maxn]=-1;if (l==r) return;build (Lson); build (Rson); }void Update (int root,int l,int r,int a,int b,int c) {if (l>=a&&r<=b) {lazy[root]=c;sum[root]=lsum[root]= Rsum[root]=c?0:r-l+1;return;} Pushdown (root,r-l+1); int mid= (L+R) >>1;if (mid>=a) Update (LSON,A,B,C), if (mid<b) update (RSON,A,B,C); Pushup (root,r-l+1);} int query (int root,int l,int R,int v) {if (l==r) return L;pushdown (root,r-l+1); int mid= (L+R) >>1;if (SUM[LL]&GT;=V) return query (LSON,V), else if (lsum[rr]+rsum[ll]>=v) return mid-rsum[ll]+1;else return query (RSON,V);} int main () {int n,m;while (~scanf ("%d%d", &n,&m)) {build (1,1,n), int a,b,c;while (m--) {si (a), si (b), if (a==1) {if ( Sum[1]<b) {puts ("0"); continue;} int Ans=query (1,1,N,B);p rintf ("%d\n", ans); update (1,1,n,ans,ans+b-1,1);//Inquiry completed need to be updated;}else{si (c); Update (1,1,n,b,b+ c-1,0);}}}  return 0;}

  

Hotel (segment tree merge)

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.