• 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;
    }
    





  • 相关阅读:
    Android ConstraintLayout详解
    Android开发屏幕适配解决方
    高并发场景下的一种JVM GC优化配置【CMS】
    Runnable 和 Callable的区别
    理解对象实例化顺序
    mysql 优化原理【转】
    使用@Scheduled注解编写spring定时任务
    Spring加载resource时classpath*:与classpath:的区别
    Java 8 中的 Streams API 详解
    java 8 函数式接口
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226371.html
Copyright © 2020-2023  润新知