Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 14559 | Accepted: 6286 |
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 rto 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 05
思路:好难啊!有 n 个房间都未标记, m 次操作,每次操作如果第一个数 op 是1,则第二个数 a 表示从左往右连续 a 个为被标记的房间都被标记,输出被标记的最左的房间号,否则输出 0 。如果第一个数 op 是2,则第二个数 a 与第三个数 b 表示从 a 到 a+b-1 房间取消标记。
代码:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define M 50010 int tsum[M*4]; int lsum[M*4]; int rsum[M*4]; int cover[M*4]; void pushdown(int node,int m) { if(cover[node]!=-1) { cover[node*2]=cover[node*2+1]=cover[node]; tsum[node*2]=lsum[node*2]=rsum[node*2]=cover[node]?0:m-(m/2); tsum[node*2+1]=lsum[node*2+1]=rsum[node*2+1]=cover[node]?0:(m/2); //左右端点区间更新; cover[node]=-1; } } void pushup(int node,int m) { lsum[node]=lsum[node*2]; rsum[node]=rsum[node*2+1]; if(lsum[node]==m-(m/2))//还可以继续容纳,向右子树延伸; lsum[node]+=lsum[node*2+1]; if(rsum[node]==m/2)//还可以继续容纳,向左子树延伸; rsum[node]+=rsum[node*2]; tsum[node]=max(lsum[node*2+1]+rsum[node*2],max(tsum[node*2],tsum[node*2+1])); //更新最值 max(左子树右端点 + 右子树左端点,左子树最值,右子树最值) } void build(int l,int r,int node) { tsum[node]=lsum[node]=rsum[node]=r-l+1; cover[node]=-1; if(l==r) return ; int mid=(l+r)/2; build(l,mid,node*2); build(mid+1,r,node*2+1); } void update(int L,int R,int c,int l,int r,int node) { if(L<=l&&R>=r) { tsum[node]=lsum[node]=rsum[node]=c?0:r-l+1; cover[node]=c; return ; } pushdown(node,r-l+1); int mid=(l+r)/2; if(L<=mid) update(L,R,c,l,mid,node*2); if(mid<R) update(L,R,c,mid+1,r,node*2+1); pushup(node,r-l+1); } int query(int w,int l,int r,int node) { if(l==r) return 1; pushdown(node,r-l+1); int mid=(l+r)/2; //下面查询顺序不能反,题目要求尽量选靠左的区间; if(tsum[node*2]>=w)//左子树可以容纳; return query(w,l,mid,node*2); else if(rsum[node*2]+lsum[node*2+1]>=w)//左子树连续右端点 + 右子树连续左端点可以容纳; return mid-rsum[node*2]+1; //右子树可以容纳; return query(w,mid+1,r,node*2+1); } int main() { int i,p,n,m,op,a,b; while(~scanf("%d%d",&n,&m)) { build(1,n,1); while(m--) { scanf("%d",&op); if(op==1) { scanf("%d",&a); if(tsum[1]<a) printf("0\n"); else { p=query(a,1,n,1); printf("%d\n",p); update(p,p+a-1,1,1,n,1); } } else { scanf("%d%d",&a,&b); update(a,a+b-1,0,1,n,1); } } } return 0; }