호텔 맨 왼쪽 연속 구간 찾기 >=need의 구간 트리

Description
The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a vacation on the sunny shores of Lake Superior. Bessie, ever the competent travel agent, has named the Bullmoose Hotel on famed Cumberland Street as their vacation residence. This immense hotel has N (1 ≤ N ≤ 50,000) rooms all located on the same side of an 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 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 room numbers r..r+Di-1 if they are 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 be 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 two possible formats: (a) Two space separated integers representing a check-in request: 1 and Di (b) Three space-separated integers representing a check-out: 2, Xi, and Di
Output
* Lines 1.....: For each check-in request, output a single line with a single integer r, the first room in the 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

//
#include #include #include #include using namespace std; const int maxn=151000; struct Node {int left,right;int cval;//현재 단락의 최장 연속 구간 길이 int lval;//현재 단락이 왼쪽 단락에서 시작하는 최장 연속 구간 길이 int rval;//현재 단락이 오른쪽 단락에서 시작하는 최장 연속 구간 길이};Node tree[maxn]; inline int getLen (int id)//현재 세그먼트의 총 길이 {return tree [id].right-tree [id].left+1;}voidbuildtree(int id, int l, int r)//트리 만들기 {tree[id].left=l,tree[id].right=r;tree[id].lval=tree[id].rval=getLen(id);if(l!=r) {intmid=(l+r)>1;buildtree(id<1,l,mid);buildtree((id<1)|1,mid+1,r);}//연속 길이가 need인 구간이 있는지 확인하십시오. 왼쪽 단점으로 되돌아오는 구간이 있다면,그렇지 않으면 0 int search(int id, int need) {if(tree[id].cval=need)return tree[id].left;if(tree[id<1].cval>=need)return search(id<1,need);elseif(tree[id<1].rval+tree[(id<1) | 1].lval>=need)return tree[id<> 1].right-tree[id<1].rval+1;else return search(((id<1)|1,need);}//채우기 또는 삭제 구간:state:0 채우기,state:1void update(int id,int l,int r,int state) {if(tree[id].left>=l&tree[id].right<=r)//현재 구간은 수정할 구간에서 {if(!state)tree[id].cval=tree[id].rval=0;else tree[id].cval=tree[id].l=tree[id].l=tree[id].l=tree[id].l=tree[id]. l=getLen(id);return;}//현재 단락을 뜯어서 이전 작업인 if(tree[id].cval==getLen(id))//방치된 물건이 없습니다 {tree[id<1].cval=tree[id<1].rval=getLen(id<1);tree[(id<1)|1].lval=tree[(id<1)|1].rval=getLen(id<1)|1);} if(tree[id].cval==0)//물건이 현재 단락 {tree[id<1].cval=tree[id<1].rval=0;tree[(id<1)|1].cval=tree[(id<1)|1].lval=tree[(id<1)|1].rval=0;    }     int mid=(tree[id].left+tree[id].right)>>1;     if(r<=mid) update(id<<1,l,r,state);     else if(l>=mid+1) update((id<<1)|1,l,r,state);     else     {         update(id<<1,l,mid,state);         update((id<<1)|1,mid+1,r,state); }//해당 세그먼트 트리 [id]를 업데이트합니다.lval=tree[id<<1].lval;     if(tree[id].lval==getLen(id<<1)) tree[id].lval+=tree[(id<<1)|1].lval;     tree[id].rval=tree[(id<<1)|1].rval;     if(tree[id].rval==getLen((id<<1)|1)) tree[id].rval+=tree[id<<1].rval;     tree[id].cval=tree[id<<1].rval+tree[(id<<1)|1].lval;     tree[id].cval=max(tree[id<<1].cval,tree[id].cval);     tree[id].cval=max(tree[(id<<1)|1].cval,tree[id].cval); } intmain() {intn,ci;scanf('%d%d', &n, &ci);buildtree(1,1,n);while(ci--) {intq;scanf('%d', &q);if(q=1)//조회 {intneed;scanf('%d', &need);intr=search(1,need);printf('%d', r);if(r)update(1,r,r+need-1,0);}else//방출 구간 {intl,len;scanf('%d%d','&l,&len);intr=l+len-1;update(1,l,r,1);        }     }     return 0; }

좋은 웹페이지 즐겨찾기