Bzoj1593: [usaco 2008 Feb] Hotel

Source: Internet
Author: User

1593: [usaco Feb] Hotel time limit: 10 sec memory limit: 64 MB
Submit: 381 solved: 220
[Submit] [Status] Description

The most recent travel plan for the cows is to come to the lake of subilier and enjoy the beautiful scenery and sunshine. As the travel planner and owner, Bessie chooses to stay at a famous hotel by the lake. This huge hotel has a total of N (1 <= n <= 50,000) rooms, which are lined up on the same floor. In any room, you only need to open the curtains, you can see the sparkling lake. Bessie and his entourage, as well as other tourists, came to the hotel's service desk in batches, hoping to book a continuous room between d_ I (1 <= d_ I <= N. The reception work at the service desk is also very simple: if there is R, the number is R .. R + D_i-1 rooms are empty, and he will arrange the customers in these rooms; if there is no qualified R, he will apologize that there is not enough empty room, ask the customer to find another hotel. If there are more than one r that meets the conditions, the waiter selects the smallest one. Check-out services in the hotel are also carried out in batch. Each check-out request is described by two numbers X_ I, d_ I, indicating that all guests in the room are leaving, numbered X_ I .. X_ I + D_i-1 (1 <= x_ I <= N-D_ I + 1. Some or even all of the room to be returned before check-out may have been left empty. Your job is to write a program to help the waiter arrange a room for the passengers. Your program needs to process a total of M (1 <= m <50,000) requests for in-house or out-of-office arrival in the input order. Before the first request arrives, all the rooms in the hotel were idle.

Input

* Row 1st: two integers separated by spaces: N and m

* 2nd .. m + 1 row: I + 1 describes the I request. If it is a reservation request, it is described by two numbers: 1 and d_ I. The numbers are separated by spaces; if it is a check-out request, it is described with 3 numbers separated by spaces 2, X_ I, d_ I

Output

* 1st ..?? Row: For each reservation request, one number exclusive to one row is output: if the request can be satisfied, the minimum R meeting the condition is output; if the request cannot be met, the output 0

Sample input10 6
1 3
1 3
1 3
1 3
2 5 5
1 6

Sample output1
4
7
0
5
Hint Source

Gold

Question: in fact, the line segment tree can be abused at will. I won't say that I did not write Pushdown during query and checked the 1 H + t_t code:
  1 #include<cstdio>  2 #include<cstdlib>  3 #include<cmath>  4 #include<cstring>  5 #include<algorithm>  6 #include<iostream>  7 #include<vector>  8 #include<map>  9 #include<set> 10 #include<queue> 11 #include<string> 12 #define inf 1000000000 13 #define maxn 105000 14 #define maxm 500+100 15 #define eps 1e-10 16 #define ll long long 17 #define pa pair<int,int> 18 #define for0(i,n) for(int i=0;i<=(n);i++) 19 #define for1(i,n) for(int i=1;i<=(n);i++) 20 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 21 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 22 #define mod 1000000007 23 using namespace std; 24 inline int read() 25 { 26     int x=0,f=1;char ch=getchar(); 27     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 28     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();} 29     return x*f; 30 } 31 struct seg{int l,r,tag,lx,rx,mx;}t[4*maxn]; 32 int n,m; 33 inline void build(int k,int l,int r) 34 { 35     t[k].l=l;t[k].r=r;int mid=(l+r)>>1; 36     t[k].tag=-1; 37     t[k].lx=t[k].rx=t[k].mx=r-l+1; 38     if(l==r)return; 39     build(k<<1,l,mid);build(k<<1|1,mid+1,r); 40 } 41 inline void update(int k,int ch) 42 { 43     t[k].tag=ch; 44     t[k].lx=t[k].rx=t[k].mx=ch==0?0:t[k].r-t[k].l+1; 45 } 46 inline void pushdown(int k) 47 { 48     if(t[k].tag==-1)return; 49     update(k<<1,t[k].tag); 50     update(k<<1|1,t[k].tag); 51     t[k].tag=-1; 52 } 53 inline void pushup(int k) 54 { 55     int l=k<<1,r=k<<1|1; 56     t[k].lx=t[l].lx;if(t[k].lx==t[l].r-t[l].l+1)t[k].lx+=t[r].lx; 57     t[k].rx=t[r].rx;if(t[k].rx==t[r].r-t[r].l+1)t[k].rx+=t[l].rx; 58     t[k].mx=max(t[l].rx+t[r].lx,max(t[l].mx,t[r].mx)); 59 } 60 inline void change(int k,int x,int y,int ch) 61 { 62     int l=t[k].l,r=t[k].r,mid=(l+r)>>1; 63     if(l==x&&r==y){update(k,ch);return;} 64     pushdown(k); 65     if(y<=mid)change(k<<1,x,y,ch); 66     else if(x>mid)change(k<<1|1,x,y,ch); 67     else change(k<<1,x,mid,ch),change(k<<1|1,mid+1,y,ch); 68     pushup(k); 69 } 70 inline int query(int k,int x) 71 { 72     pushdown(k); 73     if(t[k<<1].mx>=x)return query(k<<1,x); 74     else if(t[k<<1].rx+t[k<<1|1].lx>=x)return t[k<<1].r-t[k<<1].rx+1; 75     else return query(k<<1|1,x); 76 } 77 int main() 78 { 79     freopen("input.txt","r",stdin); 80     freopen("output.txt","w",stdout); 81     n=read();m=read(); 82     build(1,1,n); 83     while(m--) 84     { 85         int ch=read(); 86         if(ch==1) 87         { 88             int x=read(),y; 89             if(t[1].mx<x){printf("0\n");continue;} 90             y=query(1,x);printf("%d\n",y); 91             change(1,y,y+x-1,0); 92  93         } 94         else 95         { 96             int x=read(),y=x+read()-1; 97             change(1,x,y,1); 98         } 99         //for1(i,n)cout<<t[i].l<<‘ ‘<<t[i].r<<‘ ‘<<t[i].lx<<‘ ‘<<t[i].rx<<‘ ‘<<t[i].mx<<endl;100     }101     return 0;102 }
View code

 

Bzoj1593: [usaco 2008 Feb] Hotel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.