zoukankan      html  css  js  c++  java
  • Poj 3667 Hotel 线段树 区间合并

    Hotel
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 8968   Accepted: 3804

    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 D(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
    ----------------

    转载题解

    题意:1 a:询问是不是有连续长度为a的空房间,有的话住进最左边
    2 a b:将[a,a+b-1]的房间清空
    思路:记录区间中最长的空房间
    线段树操作:update:区间替换 query:询问满足条件的最左断点

    ---------------

    搞混01的意义错了好久...

    ---------------

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #define N 555555
    
    using namespace std;
    
    const int OO=1e9;
    
    queue<int>que;
    
    struct Tree
    {
        int l;
        int r;
        int num;
        int msum;
        int lsum;
        int rsum;
        int set;
    } tree[N*4];
    
    void push_up(int root)
    {
        tree[root].lsum=tree[root<<1].lsum;
        tree[root].rsum=tree[root<<1|1].rsum;
        if (tree[root].lsum==tree[root<<1].num)
        {
            tree[root].lsum+=tree[root<<1|1].lsum;
        }
        if (tree[root].rsum==tree[root<<1|1].num)
        {
            tree[root].rsum+=tree[root<<1].rsum;
        }
        tree[root].msum=max( tree[root<<1].rsum+tree[root<<1|1].lsum, max(tree[root<<1].msum,tree[root<<1|1].msum) );
    }
    
    void push_down(int root)
    {
        if (tree[root].set!=-1)
        {
            tree[root<<1].set=tree[root<<1|1].set=tree[root].set;
            if (tree[root].set==1)
            {
                tree[root<<1].lsum=tree[root<<1].rsum=tree[root<<1].msum=0;
                tree[root<<1|1].lsum=tree[root<<1|1].rsum=tree[root<<1|1].msum=0;
            }
            if (tree[root].set==0)
            {
                tree[root<<1].lsum=tree[root<<1].rsum=tree[root<<1].msum=tree[root<<1].num;
                tree[root<<1|1].lsum=tree[root<<1|1].rsum=tree[root<<1|1].msum=tree[root<<1|1].num;
            }
            tree[root].set=-1;
        }
    }
    
    void build(int root,int l,int r)
    {
        tree[root].l=l;
        tree[root].r=r;
        tree[root].num=r-l+1;
        tree[root].lsum=tree[root].rsum=tree[root].msum=r-l+1;
        tree[root].set=-1;
        if(tree[root].l==tree[root].r)
        {
            return;
        }
        int mid=(l+r)/2;
        build(root<<1,l,mid);
        build(root<<1|1,mid+1,r);
    }
    
    void update(int root,int L,int R,int val)
    {
        if(L<=tree[root].l&&R>=tree[root].r)
        {
            if (val==1)
            {
                tree[root].lsum=tree[root].rsum=tree[root].msum=0;
            }
            if (val==0)
            {
                tree[root].lsum=tree[root].rsum=tree[root].msum=tree[root].num;
            }
            tree[root].set=val;
            return;
        }
        push_down(root);
        int mid=(tree[root].l+tree[root].r)/2;
        if(L<=mid)
            update(root<<1,L,R,val);
        if (R>mid)
            update(root<<1|1,L,R,val);
        push_up(root);
    }
    
    int query(int root,int w)
    {
        if(tree[root].l==tree[root].r)
        {
            return tree[root].l;
        }
        push_down(root);
        int mid=(tree[root].l+tree[root].r)/2;
        if (tree[root<<1].msum>=w) return query(root<<1,w);
        else if(tree[root<<1].rsum+tree[root<<1|1].lsum>=w) return mid-tree[root<<1].rsum+1;
        return query(root<<1|1,w);
    }
    
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        build(1,1,n);
        while (m--)
        {
            int op,a,b;
            scanf("%d",&op);
            if (op==1)
            {
                scanf("%d",&a);
                if (tree[1].msum<a) puts("0");
                else
                {
                    int p=query(1,a);
                    printf("%d\n",p);
                    update(1,p,p+a-1,1);
                }
            }
            else
            {
                scanf("%d%d",&a,&b);
                update(1,a,a+b-1,0);
            }
        }
        return 0;
    }
    





  • 相关阅读:
    财务高手-资本高手
    做到顶尖看三种书-大牛的书 工具书 教材书
    拓端tecdat|R语言使用HAR-RV预测实际波动率Realized Volatility案例
    拓端tecdat|WINBUGS对随机波动率模型进行贝叶斯估计与比较
    拓端tecdat|R语言机器学习实战之多项式回归
    拓端tecdat|R语言风险价值VaR(Value at Risk)和损失期望值ES(Expected shortfall)的估计
    拓端tecdat|TensorFlow 2.0 keras开发深度学习模型实例:多层感知器(MLP),卷积神经网络(CNN)和递归神经网络(RNN)
    拓端tecdat|Python安装TensorFlow 2、tf.keras和深度学习模型的定义
    cookie绕过验证码登录
    [转]Python3 字典 items() 方法
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226371.html
Copyright © 2011-2022 走看看