zoukankan      html  css  js  c++  java
  • BZOJ1593: [Usaco2008 Feb]Hotel 旅馆

    1593: [Usaco2008 Feb]Hotel 旅馆

    Time Limit: 10 Sec  Memory Limit: 64 MB
    Submit: 381  Solved: 220
    [Submit][Status]

    Description

    奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光。作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿。这个巨大的旅馆一共有N (1 <= N <= 50,000)间客房,它们在同一层楼中顺次一字排开,在任何一个房间里,只需要拉开窗帘,就能见到波光粼粼的湖面。 贝茜一行,以及其他慕名而来的旅游者,都是一批批地来到旅馆的服务台,希望能订到D_i (1 <= D_i <= N)间连续的房间。服务台的接待工作也很简单:如果存在r满足编号为r..r+D_i-1的房间均空着,他就将这一批顾客安排到这些房间入住;如果没有满足条件的r,他会道歉说没有足够的空房间,请顾客们另找一家宾馆。如果有多个满足条件的r,服务员会选择其中最小的一个。 旅馆中的退房服务也是批量进行的。每一个退房请求由2个数字X_i、D_i 描述,表示编号为X_i..X_i+D_i-1 (1 <= X_i <= N-D_i+1)房间中的客人全部离开。退房前,请求退掉的房间中的一些,甚至是所有,可能本来就无人入住。 而你的工作,就是写一个程序,帮服务员为旅客安排房间。你的程序一共需要处理M (1 <= M < 50,000)个按输入次序到来的住店或退房的请求。第一个请求到来前,旅店中所有房间都是空闲的。

    Input

    * 第1行: 2个用空格隔开的整数:N、M

    * 第2..M+1行: 第i+1描述了第i个请求,如果它是一个订房请求,则用2个数字 1、D_i描述,数字间用空格隔开;如果它是一个退房请求,用3 个以空格隔开的数字2、X_i、D_i描述

    Output

    * 第1..??行: 对于每个订房请求,输出1个独占1行的数字:如果请求能被满足 ,输出满足条件的最小的r;如果请求无法被满足,输出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

    HINT

     

    Source

    题解:
    其实这题想到线段树就可以随便虐了。
    我不会说我因为query的时候没写pushdown查了1h+T_T
    代码:
      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
    ");continue;}
     90             y=query(1,x);printf("%d
    ",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
  • 相关阅读:
    use paramiko to connect remote server and execute command
    protect golang source code
    adjust jedi vim to python2 and python3
    install vim plugin local file offline
    add swap file if you only have 1G RAM
    datatables hyperlink in td
    django rest framework custom json format
    【JAVA基础】网络编程
    【JAVA基础】多线程
    【JAVA基础】String类的概述和使用
  • 原文地址:https://www.cnblogs.com/zyfzyf/p/4052154.html
Copyright © 2011-2022 走看看