POJ 3667Hotel

来源:互联网 发布:mac win10触控板手势 编辑:程序博客网 时间:2024/06/01 15:01

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 Dicontiguous 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 D(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 61 31 31 31 32 5 51 6

Sample Output

1470

5

用线段树统计区间里最长连续相同为0的串的长度,询问的时候找出最左边的点,然后再覆盖上1,清空就覆盖上0

#include<set>#include<map>#include<ctime>#include<cmath>#include<stack>#include<queue>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define rep(i,j,k) for (int i = j; i <= k; i++)#define per(i,j,k) for (int i = j; i >= k; i--)#define loop(i,j,k) for (int i = j;i != -1; i = k[i])#define lson x << 1, l, mid#define rson x << 1 | 1, mid + 1, r#define fi first#define se second#define mp(i,j) make_pair(i,j)#define pii pair<int,int>using namespace std;typedef long long LL;const int low(int x) { return x&-x; }const double eps = 1e-4;const int INF = 0x7FFFFFFF;const int mod = 9973;const int N = 2e5 + 10;int n, m, x, y, z;int f[N], L[N], R[N], g[N];void build(int x, int l, int r){L[x] = R[x] = f[x] = r - l + 1;g[x] = -1;if (l == r) return;int mid = l + r >> 1;build(lson);build(rson);}void merge(int x, int l, int r){int mid = l + r >> 1;L[x] = L[x << 1] + (L[x << 1] == mid - l + 1 ? L[x << 1 | 1] : 0);R[x] = R[x << 1 | 1] + (R[x << 1 | 1] == r - mid ? R[x << 1] : 0);f[x] = max(f[x << 1], f[x << 1 | 1]);f[x] = max(f[x], R[x << 1] + L[x << 1 | 1]);}void pushdown(int x, int l, int r){if (l == r) return;int mid = l + r >> 1;g[x << 1] = g[x << 1 | 1] = g[x];if (g[x]){f[x << 1] = L[x << 1] = R[x << 1] = 0;f[x << 1 | 1] = L[x << 1 | 1] = R[x << 1 | 1] = 0;}else{f[x << 1] = L[x << 1] = R[x << 1] = mid - l + 1;f[x << 1 | 1] = L[x << 1 | 1] = R[x << 1 | 1] = r - mid;}g[x] = -1;}int find(int x, int l, int r, int len){if (f[x] < len) return 0;if (g[x] != -1) pushdown(x, l, r);if (l == r) return l;int mid = l + r >> 1;if (f[x << 1] >= len) return find(lson, len);if (R[x << 1] + L[x << 1 | 1] >= len) return mid - R[x << 1] + 1;return find(rson, len);}void make(int x, int l, int r, int ll, int rr, int v){if (g[x] != -1) pushdown(x, l, r);if (ll <= l&&r <= rr){g[x] = v;f[x] = L[x] = R[x] = v ? 0 : r - l + 1;}else{int mid = l + r >> 1;if (ll <= mid) make(lson, ll, rr, v);if (rr > mid) make(rson, ll, rr, v);merge(x, l, r);}}int main(){while (scanf("%d%d", &n, &m) != EOF){build(1, 1, n);while (m--){scanf("%d%d", &x, &y);if (x == 1) {int k = find(1, 1, n, y);printf("%d\n", k);if (k) make(1, 1, n, k, k + y - 1, 1);}else{scanf("%d", &z);make(1, 1, n, y, y + z - 1, 0);}} }return 0;}


0 0