zoukankan      html  css  js  c++  java
  • POJ 3667 Hotel

    Hotel

    Time Limit: 3000ms
    Memory Limit: 65536KB
    This problem will be judged on PKU. Original ID: 3667
    64-bit integer IO format: %lld      Java class name: Main

    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

     
    解题:线段树的区间合并
     
     1 #include <iostream>
     2 #include <cstdio>
     3 using namespace std;
     4 const int maxn = 50010;
     5 struct node {
     6     int sum,lsum,rsum,cover;
     7 } tree[maxn<<2];
     8 void build(int L,int R,int v) {
     9     tree[v].sum = R - L + 1;
    10     tree[v].lsum = R - L + 1;
    11     tree[v].rsum = R - L + 1;
    12     tree[v].cover = -1;
    13     if(L == R) return;
    14     int mid = (L + R)>>1;
    15     build(L,mid,v<<1);
    16     build(mid+1,R,v<<1|1);
    17 }
    18 void pushdown(int v,int k) {
    19     if(tree[v].cover == -1) return;
    20     tree[v<<1].cover = tree[v].cover;
    21     tree[v<<1|1].cover = tree[v].cover;
    22     tree[v<<1].lsum = tree[v<<1].rsum = tree[v<<1].sum = tree[v].cover?0:k-(k>>1);
    23     tree[v<<1|1].lsum = tree[v<<1|1].rsum = tree[v<<1|1].sum = tree[v].cover?0:k>>1;
    24     tree[v].cover = -1;
    25 }
    26 void pushup(int v,int k) {
    27     tree[v].sum = max(tree[v<<1].sum,tree[v<<1|1].sum);
    28     tree[v].sum = max(tree[v].sum,tree[v<<1].rsum + tree[v<<1|1].lsum);
    29     tree[v].lsum = tree[v<<1].lsum;
    30     tree[v].rsum = tree[v<<1|1].rsum;
    31     if(tree[v].lsum == (k - (k>>1))) tree[v].lsum += tree[v<<1|1].lsum;
    32     if(tree[v].rsum == (k>>1)) tree[v].rsum += tree[v<<1].rsum;
    33 }
    34 int query(int L,int R,int p,int v) {
    35     if(L == R) return L;
    36     pushdown(v,R - L + 1);
    37     int mid = (L + R)>>1,ret;
    38     if(tree[v<<1].sum >= p) ret = query(L,mid,p,v<<1);
    39     else if(tree[v<<1].rsum + tree[v<<1|1].lsum >= p)
    40         ret = mid - tree[v<<1].rsum + 1;
    41     else ret = query(mid+1,R,p,v<<1|1);
    42     return ret;
    43 }
    44 void update(int L,int R,int lt,int rt,int val,int v) {
    45     if(lt <= L && rt >= R) {
    46         tree[v].lsum = tree[v].rsum = tree[v].sum = val?0:R - L + 1;
    47         tree[v].cover = val;
    48         return;
    49     }
    50     pushdown(v,R - L + 1);
    51     int mid = (L + R)>>1;
    52     if(lt <= mid) update(L,mid,lt,rt,val,v<<1);
    53     if(rt > mid) update(mid+1,R,lt,rt,val,v<<1|1);
    54     pushup(v,R - L + 1);
    55 }
    56 int main() {
    57     int n,m,op,x,y;
    58     while(~scanf("%d %d",&n,&m)) {
    59         build(1,n,1);
    60         while(m--) {
    61             scanf("%d %d",&op,&x);
    62             if(op&1) {
    63                 if(tree[1].sum < x) puts("0");
    64                 else {
    65                     int tmp = query(1,n,x,1);
    66                     printf("%d
    ",tmp);
    67                     update(1,n,tmp,tmp + x - 1,1,1);
    68                 }
    69             } else {
    70                 scanf("%d",&y);
    71                 update(1,n,x,x+y-1,0,1);
    72             }
    73         }
    74     }
    75     return 0;
    76 }
    View Code
  • 相关阅读:
    USACO Broken Necklace 通过了
    USACO Broken Necklace
    推荐顺序ACM
    usaco暂时无法访问
    格式
    稳定排序
    归并排序
    浅析Struts1和Struts2的Action线程安全问题
    判别式模型与生成式模型的区别
    远景能源一面
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4456443.html
Copyright © 2011-2022 走看看