Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 10571 | Accepted: 4561 |
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-n,开始时每个房间都是空的。有m个操作,每个操作可以有两种:
1 a :有a个人要入住房号连续的房间,若有足够的连续的房间,输出这些房间中最小的房间编号x,且编号x到x+a-1的房间被入住。
2 a b : 编号为a到a+b-1的房间变为空房。
思路:线段树。每个结点要设三个数据域sum, lsum, rsum,分别表示该区间的最长连续未被覆盖点数、该区间从左端点开始的最长连续未被覆盖点数、该区间从右端点开始的最长连续未被覆盖点数。
AC代码:
#include <iostream> #include <cmath> #include <cstdlib> #include <cstring> #include <cstdio> #include <queue> #include <stack> #include <ctime> #include <vector> #include <algorithm> #define ll long long #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) #define eps 1e-6; using namespace std; const int INF = INF; const int maxn = 50005; struct node{ int l, r, lsum, rsum, sum, cover; }tree[maxn * 4]; //lsum:该区间从左端点开始的最长连续未被覆盖点数 //rsum:该区间从右端点开始的最长连续未被覆盖点数 //sum:该区间的最长连续未被覆盖点数 //cover = 0:该区间的点未被覆盖; cover = 1:该区间的点全被覆盖 int n, m; void pushdown(int rt, int len){ //更新儿子节点 if(tree[rt].cover != -1){ tree[L(rt)].sum = tree[L(rt)].lsum = tree[L(rt)].rsum = tree[rt].cover ? 0 : len - (len >> 1); tree[R(rt)].sum = tree[R(rt)].lsum = tree[R(rt)].rsum = tree[rt].cover ? 0 : (len >> 1); tree[L(rt)].cover = tree[R(rt)].cover = tree[rt].cover; tree[rt].cover = -1; } } void pushup(int rt, int len){ //更新父节点 tree[rt].lsum = tree[L(rt)].lsum; tree[rt].rsum = tree[R(rt)].rsum; if(tree[rt].lsum == len - (len >> 1)) tree[rt].lsum += tree[R(rt)].lsum; if(tree[rt].rsum == (len >> 1)) tree[rt].rsum += tree[L(rt)].rsum; tree[rt].sum = max(tree[L(rt)].rsum + tree[R(rt)].lsum, max(tree[L(rt)].sum, tree[R(rt)].sum)); //该区间最长连续未被覆盖点数=max(全在左,全在右,左的一部分+右的一部分) } void build(int l, int r, int rt){ tree[rt].l = l, tree[rt].r = r; tree[rt].sum = tree[rt].lsum = tree[rt].rsum = r - l + 1; tree[rt].cover = -1; if(l == r) return; int mid = (l + r) >> 1; build(l, mid, L(rt)); build(mid + 1, r, R(rt)); } void update(int l, int r, int c, int rt){ if(tree[rt].l >= l && tree[rt].r <= r) { tree[rt].sum = tree[rt].lsum = tree[rt].rsum = c ? 0 : tree[rt].r - tree[rt].l + 1; tree[rt].cover = c; return; } pushdown(rt, tree[rt].r - tree[rt].l + 1); if(r <= tree[L(rt)].r) update(l, r, c, L(rt)); else if(l >= tree[R(rt)].l) update(l, r, c, R(rt)); else { update(l, tree[L(rt)].r, c, L(rt)); update(tree[R(rt)].l, r, c, R(rt)); } pushup(rt, tree[rt].r - tree[rt].l + 1); } int query(int val, int rt){ if(tree[rt].l == tree[rt].r) return tree[rt].l; pushdown(rt, tree[rt].r - tree[rt].l + 1); int mid = (tree[rt].l + tree[rt].r) >> 1; if(tree[L(rt)].sum >= val) return query(val, L(rt)); else if(tree[L(rt)].rsum + tree[R(rt)].lsum >= val) return mid - tree[L(rt)].rsum + 1; else return query(val, R(rt)); } int main() { int 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(tree[1].sum < a) puts("0"); else { int ans = query(a, 1); printf("%d\n", ans); update(ans, ans + a - 1, 1, 1); } } else { scanf("%d%d", &a, &b); update(a, a + b - 1, 0, 1); } } } return 0; }