| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 18289 | Accepted: 7937 |
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
题意:给出一段长为n的区间,有两种操作:1、查询最靠左并且长度为d的连续区间,并输出其左端点下标;2、清空以l为左端点
长度为d的连续区间
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int N = 50000 + 10; int n, m; struct xx{ int l, r, lm, rm, m, lazy; } T[N<<2]; void Pushdown(int k){ T[k<<1].lazy = T[k<<1|1].lazy = T[k].lazy; T[k<<1].lm = T[k<<1].rm = T[k<<1].m = T[k].lazy ? 0 : T[k<<1].r-T[k<<1].l+1; T[k<<1|1].lm = T[k<<1|1].rm = T[k<<1|1].m = T[k].lazy ? 0 : T[k<<1|1].r-T[k<<1|1].l+1; T[k].lazy = -1; } void Pushup(int k){ T[k].lm = T[k<<1].lm; T[k].rm = T[k<<1|1].rm; if(T[k].lm == T[k<<1].r-T[k<<1].l+1) T[k].lm += T[k<<1|1].lm; if(T[k].rm == T[k<<1|1].r-T[k<<1|1].l+1) T[k].rm += T[k<<1].rm; T[k].m = max(max(T[k<<1].m, T[k<<1|1].m), T[k<<1].rm+T[k<<1|1].lm); } void Build(int l, int r, int k){ T[k].l = l, T[k].r = r, T[k].lazy = -1; T[k].lm = T[k].rm = T[k].m = r-l+1; if(l == r) return; int mid = (l+r)>>1; Build(l, mid, k<<1); Build(mid+1, r, k<<1|1); Pushup(k); } void Update(int l, int r, int v, int k){ if(l == T[k].l && r == T[k].r){ T[k].lm = T[k].rm = T[k].m = v ? 0 : T[k].r-T[k].l+1; T[k].lazy = v; return; } if(T[k].lazy != -1) Pushdown(k); int mid = (T[k].l+T[k].r)>>1; if(l <= mid) Update(l, min(mid, r), v, k<<1); if(r > mid) Update(max(mid+1, l), r, v, k<<1|1); Pushup(k); } int Find(int x, int k){ if(T[k].l == T[k].r) return T[k].l; if(T[k].lazy != -1) Pushdown(k); int mid = (T[k].l+T[k].r)>>1; if(T[k<<1].m >= x) return Find(x, k<<1); else if(T[k<<1].rm+T[k<<1|1].lm >= x) return mid-T[k<<1].rm+1; else if(T[k<<1|1].m >= x) return Find(x, k<<1|1); } int main(){ while(scanf("%d%d", &n, &m) == 2){ Build(1, n, 1); int op, x, y;; for(int i = 0; i < m; i++){ scanf("%d", &op); if(op&1){ scanf("%d", &x); if(T[1].m < x){ printf("0\n"); continue; } y = Find(x, 1); printf("%d\n", y); if(y) Update(y, x+y-1, 1, 1); } else{ scanf("%d%d", &x, &y); Update(x, x+y-1, 0, 1); } } } }
本文介绍了一个酒店预订系统的实现方法,该系统能够处理房间的预订与退订请求,支持查找可用的连续房间并进行分配,同时提供了高效的区间更新和查询功能。
1180

被折叠的 条评论
为什么被折叠?



