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

    Hotel
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 20468   Accepted: 8924

    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

    题目链接:
    http://poj.org/problem?id=3667

    题目大意:

    在一条直线上有1..n间旅社,有m个操作。操作分两种,一是找最靠前的连续d间旅社安排旅客入住,一是从某间旅社开始清空连续的d间旅社。这n件旅社在初始时都是空的。对第一种操作输出连续的d间旅社的第一间的坐标。

    线段树上区间合并的经典题。

    我比较喜欢用结构体实现线段树,把几种操作称呼为pushup, pushdown, build, modify, query。

    区间合并的题,pushup和query和普通的线段树不同。query还是个难点。

    拿这道题说吧。首先结构体中应该存有lm, rm, m,分别是左端连续空房间,右端连续空房间,区间中最长连续空房间。比如某节点管理区间为1..5,对应序列为 01001,那么lm,rm,m分别是1,0,2(把0视为连续)。这样pushup就比较好操作了。而query 则比较难想^_^,主要思路是逐步判断:x的lm -> x*2的m -> x*2的rm和x*2+1的lm -> x*2+1。主要实现都在代码中,可以看一看。

    区间合并还有一道经典题:hdu1540,同样是维护lm,rm,m三个值。query的实现有所不同,区间合并的线段树query还是个难点啊,我还要多做点题体会体会。

    //1表示住人 0表示空间 而lm,rm,m存储的是连续的0的个数
    
    #include<cstdio>
    #include<algorithm>
    
    using namespace std;
    
    const int maxn=80000;
    
    struct ttree
    {
        int l,r;
        int lm,rm,m;
        int tag;//-1没有标记 0标记为0 1标记为1
        inline int len()
        {
            return r-l+1;
        }
    };
    ttree tree[maxn*4+10];
    
    void pushup(int x)
    {
        if(tree[x].l==tree[x].r)
            return;
        tree[x].lm=tree[x*2].lm;
        tree[x].rm=tree[x*2+1].rm;
        tree[x].m=max(max(tree[x*2].m,tree[x*2+1].m),tree[x*2].rm+tree[x*2+1].lm);
        if(tree[x].lm==tree[x*2].len())
            tree[x].lm+=tree[x*2+1].lm;
        if(tree[x].rm==tree[x*2+1].len())
            tree[x].rm+=tree[x*2].rm;
    }
    
    void pushdown(int x)
    {
        if(tree[x].tag==0)
        {
            tree[x*2].lm=tree[x*2].rm=tree[x*2].m=tree[x*2].len();
            tree[x*2+1].lm=tree[x*2+1].rm=tree[x*2+1].m=tree[x*2+1].len();
            tree[x*2].tag=tree[x*2+1].tag=0;
            tree[x].tag=-1;
        }
        else if(tree[x].tag==1)
        {
            tree[x*2].lm=tree[x*2].rm=tree[x*2].m=0;
            tree[x*2+1].lm=tree[x*2+1].rm=tree[x*2+1].m=0;
            tree[x*2].tag=tree[x*2+1].tag=1;
            tree[x].tag=-1;
        }
    }
    
    void build(int x,int l,int r)
    {
        tree[x].l=l;
        tree[x].r=r;
        tree[x].tag=-1;
        tree[x].lm=tree[x].rm=tree[x].m=r-l+1;
        if(l<r)
        {
            int mid=(l+r)/2;
            build(x*2,l,mid);
            build(x*2+1,mid+1,r);
        }
    }
    
    void modify(int x,int l,int r,int op)//op为1表示入住 0表示离宿
    {
        if(l<=tree[x].l&&r>=tree[x].r)
        {
            //printf("%d %d
    ",tree[x].l,tree[x].r);
            tree[x].tag=op;
            if(op==0)
                tree[x].lm=tree[x].rm=tree[x].m=tree[x].len();
            else
                tree[x].lm=tree[x].rm=tree[x].m=0;
        }
        else
        {
            pushdown(x);
            int mid=(tree[x].l+tree[x].r)/2;
            if(l<=mid)
                modify(x*2,l,r,op);
            if(r>mid)
                modify(x*2+1,l,r,op);
            pushup(x);
        }
    }
    
    int query(int x,int d)//d表示所求的区间长度
    {
        if(tree[x].m<d)
            return 0;
        if(tree[x].lm>=d)
            return tree[x].l;
        pushdown(x);
        if(tree[x*2].m>=d)
            return query(x*2,d);
        if(tree[x*2].rm+tree[x*2+1].lm>=d)
            return tree[x*2].r-tree[x*2].rm+1;
        return query(x*2+1,d);
    }
    
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        build(1,1,n);
        while(m--)
        {
            int a;
            scanf("%d",&a);
            if(a==1)
            {
                int b;
                scanf("%d",&b);
                int c=query(1,b);
                printf("%d
    ",c);
                if(c!=0)
                    modify(1,c,c+b-1,1);
                //printf("%d %d %d
    ",tree[1].lm,tree[1].m,tree[1].rm);
            }
            else
            {
                int b,c;
                scanf("%d%d",&b,&c);
                modify(1,b,b+c-1,0);
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    基本语法-函数
    基本语法-变量
    python+selenium2自动化------quit()和close()区别
    docker搭建bwapp漏洞测试环境
    软件测试---登录功能的测试用例
    Faker开源库构造测试数据
    selenium自动化简易测试框架总结
    ddt+unittest+HTMLTestRunner,生成测试报告时,报告中没有输出测试用例函数的描述信息
    python+selenium2自动化---PageObject模式+unittest结合实现自动化
    python+selenium2自动化---关键字驱动+unittest结合实现自动化
  • 原文地址:https://www.cnblogs.com/acboyty/p/9527267.html
Copyright © 2011-2022 走看看