Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 19502 | Accepted: 8491 |
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
做这题的时候发现 就是维护一个区间内有多少连续空房间 我们用线段树去做
发现和以前一道计蒜客的题目黑白石头很像 换了一个风格写 这个风格在结构体里操作 应该是HH大神早期的作品
对于这题来说 有利于简化代码量 所以学习了
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAX_N= 50050;
struct node {
int l,r;
int len;
int len_l;
int len_r;
int mark;
int len_true() {
return r-l+1;
}
void update(){
len_l=len_r=len=(mark?0:len_true());
}
}s[MAX_N<<2];
void build(int p,int l,int r){
s[p].l = l;
s[p].r = r;
s[p].len=s[p].len_l=s[p].len_r = s[p].len_true();
s[p].mark=0;
if(l==r) return;
int mid = (l+r)>>1;
build(p*2,l,mid);
build(p*2+1,mid+1,r);
return;
}
void change(int p,int l,int r,int x,int y,int v){
if(s[p].l==l&&s[p].r==r){
s[p].mark = v;
s[p].update();
return ;
}
if(s[p].mark!=-1){
s[p*2].mark=s[p*2+1].mark=s[p].mark;
s[p*2].update();
s[p*2+1].update();
s[p].mark = -1;
}
int mid = (l+r)>>1;
if(x<=mid) change(p*2,l,mid,x,y,v);
if(y>mid) change(p*2+1,mid+1,r,x,y,v);
int tmp = max(s[p*2].len,s[p*2+1].len);
s[p].len=max(tmp,s[p*2].len_r+s[p*2+1].len_l);
s[p].len_l = s[p*2].len_l;
s[p].len_r = s[p*2+1].len_r;
if(s[p*2].len==s[p*2].len_true()){
s[p].len_l+=s[p*2+1].len_l;
}
if(s[p*2+1].len==s[p*2+1].len_true()){
s[p].len_r+=s[p*2].len_r;
}
return ;
}
int query(int p,int w){
if(s[p].l==s[p].r&&w==1){
return s[p].l;
}
if(s[p].mark!=-1){
s[p*2].mark = s[p*2+1].mark = s[p].mark;
s[p].mark = -1;
s[p*2].update();
s[p*2+1].update();
}
if(s[p*2].len>=w){
return query(p*2,w);
}
else if(s[p*2].len_r+s[p*2+1].len_l>=w){
return (s[p*2].r-s[p*2].len_r+1);
}
else if(s[p*2+1].len>=w){
return query(p*2+1,w);
}
else {
return 0;
}
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
build(1,1,n);
while(m--){
int choose;
scanf("%d",&choose);
if(choose==1){
int w;
scanf("%d",&w);
int index = query(1,w);
printf("%d\n",index);
if(index)
change(1,1,n,index,index+w-1,1);
}
else if(choose==2){
int a,b;
scanf("%d%d",&a,&b);
change(1,1,n,a,a+b-1,0);
}
}
return 0;
}