题目:
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 15276 | Accepted: 6609 |
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
Source
满 | 空 | 满 | 满 | 空 | 空 | 空 | 满 | 满 |
0 | 1 | 0 | 0 | 3 | 0 | 0 | 0 | 0 |
对应线段树为下方,即 以该点为起点,最长能满足的连续空间。线段树的父节点存储两个子节点的max,在搜索时,先搜左侧,若左侧没有再搜右侧,能在(log)复杂度内搜索到结果。
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <algorithm>
#define MAXN 100001
struct data{
int empty_room;
int seg_room;
int max_con;
}t[2 * MAXN];
int a[2 * MAXN];
int lazy1[2 * MAXN], lazy2[2 * MAXN], lazy3[2 * MAXN];
using namespace std;
void BuildTree(int l, int r, int x, char i){
if (i == 'e')
{
if (l == r)
{
t[x].empty_room = a[l];
return;
}
int m = (l + r) >> 1;
BuildTree(l, m, x << 1, 'e');
BuildTree(m + 1, r, x << 1 | 1, 'e');
t[x].empty_room = t[x << 1].empty_room + t[x << 1 | 1].empty_room;
return;
}
if (i == 's')
{
if (l == r)
{
t[x].seg_room = a[l];
return;
}
int m = (l + r) >> 1;
BuildTree(l, m, x << 1, 's');
BuildTree(m + 1, r, x << 1 | 1, 's');
t[x].seg_room = t[x << 1].seg_room + t[x << 1 | 1].seg_room;
return;
}
if (i == 'm')
{
if (l == r)
{
t[x].max_con = a[l];
return;
}
int m = (l + r) >> 1;
BuildTree(l, m, x << 1, 'm');
BuildTree(m + 1, r, x << 1 | 1, 'm');
t[x].max_con = max(t[x << 1].max_con, t[x << 1 | 1].max_con);
return;
}
}
void PushDown(int l, int r, int x, char i){
int * auto_lazy = NULL;
switch (i)
{
case 'e':
auto_lazy = lazy1;
break;
case 's':
auto_lazy = lazy2;
break;
case 'm':
auto_lazy = lazy3;
break;
}
int m = (l + r) >> 1;
if (auto_lazy[x] != -1){
switch (i)
{
case 'e':
t[x << 1].empty_room = auto_lazy[x] * (m - l + 1);
t[x << 1 | 1].empty_room = auto_lazy[x] * (r - m);
break;
case 's':
t[x << 1].seg_room = auto_lazy[x] * (m - l + 1);
t[x << 1 | 1].seg_room = auto_lazy[x] * (r - m);
break;
case 'm':
t[x << 1].max_con = auto_lazy[x];
t[x << 1 | 1].max_con = auto_lazy[x];
break;
}
auto_lazy[x << 1 | 1] = auto_lazy[x];
auto_lazy[x << 1] = auto_lazy[x];
auto_lazy[x] = -1;
}
return;
}
int Query(int L, int R, int l, int r, int x, char i){
if (i == 'm')
{
if (l == r)
{
return l;
}
PushDown(l, r, x, 'm');
if (t[x << 1].max_con >= L) return Query(L, R, l, (l + r) / 2, x << 1, i);
else if (t[x << 1 | 1].max_con >= L) return Query(L, R, (l + r) / 2 + 1, r, x << 1 | 1, i);
else return 0;
}
if (L == l && R == r)
{
switch (i)
{
case 'e':
return t[x].empty_room;
break;
case 's':
return t[x].seg_room;
break;
}
}
PushDown(l, r, x, i);
int m = (l + r) >> 1;
if (R <= m)return Query(L, R, l, m, x << 1, i);
else if (L > m)return Query(L, R, m + 1, r, x << 1 | 1, i);
else switch (i)
{
case 'e':
return Query(L, m, l, m, x << 1, i) + Query(m + 1, R, m + 1, r, x << 1 | 1, i);
break;
case 's':
return Query(L, m, l, m, x << 1, i) + Query(m + 1, R, m + 1, r, x << 1 | 1, i);
break;
}
}
void SegModify(int L, int R, int val, int l, int r, int x, char i){
int * auto_lazy = NULL;
switch (i)
{
case 'e':
auto_lazy = lazy1;
break;
case 's':
auto_lazy = lazy2;
break;
case 'm':
auto_lazy = lazy3;
break;
}
int m;
switch (i)
{
case 'e':
if (l == L&&r == R){
t[x].empty_room = (R - L + 1)*val;
auto_lazy[x] = val;
return;
}
PushDown(l, r, x, i);
m = (l + r) >> 1;
if (R <= m)SegModify(L, R, val, l, m, x << 1, i);
else if (L > m)SegModify(L, R, val, m + 1, r, x << 1 | 1, i);
else {
SegModify(L, m, val, l, m, x << 1, i);
SegModify(m + 1, R, val, m + 1, r, x << 1 | 1, i);
}
t[x].empty_room = t[x << 1].empty_room + t[x << 1 | 1].empty_room;
return;
case 's':
if (l == L&&r == R){
t[x].seg_room = (R - L + 1)*val;
auto_lazy[x] = val;
return;
}
PushDown(l, r, x, i);
m = (l + r) >> 1;
if (R <= m)SegModify(L, R, val, l, m, x << 1, i);
else if (L > m)SegModify(L, R, val, m + 1, r, x << 1 | 1, i);
else {
SegModify(L, m, val, l, m, x << 1, i);
SegModify(m + 1, R, val, m + 1, r, x << 1 | 1, i);
}
t[x].seg_room = t[x << 1].seg_room + t[x << 1 | 1].seg_room;
return;
case 'm':
if (l == L&&r == R){
t[x].max_con = val;
auto_lazy[x] = val;
return;
}
PushDown(l, r, x, i);
m = (l + r) >> 1;
if (R <= m)SegModify(L, R, val, l, m, x << 1, i);
else if (L > m)SegModify(L, R, val, m + 1, r, x << 1 | 1, i);
else {
SegModify(L, m, val, l, m, x << 1, i);
SegModify(m + 1, R, val, m + 1, r, x << 1 | 1, i);
}
t[x].max_con = max(t[x << 1].max_con, t[x << 1 | 1].max_con);
return;
}
}
int finds(int pos, int l, int r, int x)
{
if (t[x].empty_room == (r - l) + 1)
{
return 0;
}
if (l >= pos)
{
return 0;
}
if (l == r)
{
return l;
}
int m = (l + r) / 2;
PushDown(l, r, x, 'e');
int k = finds(pos, m + 1, r, x << 1 | 1);
if (!k)
{
return finds(pos, l, m, x << 1);
}
else
{
return k;
}
}
int main()
{
//ifstream fin("3out.txt");
//freopen("3.txt", "r", stdin);
int nn, mm;
while (cin >> nn >> mm)
{
int n;
n = 1;
while (n <= nn)
{
n *= 2;
}
memset(t, 0, sizeof(t));
memset(a, 0, sizeof(a));
memset(lazy1, -1, sizeof(lazy1));
memset(lazy2, -1, sizeof(lazy2));
memset(lazy3, -1, sizeof(lazy3));
a[1] = nn;
BuildTree(1, n, 1, 's');
BuildTree(1, n, 1, 'm');
for (size_t i = 1; i <= nn; i++)
{
a[i] = 1;
}
BuildTree(1, n, 1, 'e');
for (size_t i = 0; i < mm; i++)
{
int op;
scanf("%d", &op);
if (op == 1)
{
int people;
scanf("%d", &people);
int pos = Query(people, 0, 1, n, 1, 'm');
if (!pos)
{
//int check;
//fin >> check;
//if (check!=0)
//{
// system("pause");
// printf("0\n");
//}
printf("0\n");
}
else
{
SegModify(pos, pos + people - 1, 0, 1, n, 1, 'e');
SegModify(pos + people, pos + people, t[n - 1 + pos].max_con - people, 1, n, 1, 'm');
SegModify(pos + people, pos + people, t[n - 1 + pos].max_con - people, 1, n, 1, 's');
SegModify(pos, pos, 0, 1, n, 1, 'm');
SegModify(pos, pos, 0, 1, n, 1, 's');
//int check;
//fin >> check;
//if (check != pos)
//{
// system("pause");
// printf("%d\n",pos);
//}
printf("%d\n", pos);
}
}
if (op == 2)
{
int pos, people;
scanf("%d%d", &pos, &people);
if (pos != 1 && Query(pos - 1, pos - 1, 1, n, 1, 'e') == 1)
{
int x;
x = finds(pos, 1, n, 1);
x++;
people += pos - x;
pos = x;
}
people = min(people, nn - pos + 1);
int to_empty = -1 * Query(pos, pos + people - 1, 1, n, 1, 'e');
to_empty += people;
to_empty += Query(pos, pos + people, 1, n, 1, 's');
if (to_empty == 0)
{
continue;
}
SegModify(pos, pos + to_empty - 1, 1, 1, n, 1, 'e');
SegModify(pos, pos + to_empty - 1, 0, 1, n, 1, 's');
SegModify(pos, pos + to_empty - 1, 0, 1, n, 1, 'm');
SegModify(pos, pos, to_empty, 1, n, 1, 's');
SegModify(pos, pos, to_empty, 1, n, 1, 'm');
}
}
int bes = 0;
}
return 0;
}