• Hotel


    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 ≤ XiN-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
    


    题解:尽管知道是线段树区间合并。可是在查询时还的注意。

    由于左边优先权高。所以左边能住就訪问左边。否则中间能就訪问中间,此时直接得到结果,由于左孩子的右端点值知道。该点往前连续空位个数知道,相减+1得到最左端位置,否则右孩子找。自己绘图能解决一切。


    #include <iostream>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    struct Node
    {
    	int l,r;
    	int ls,rs,ms;
    	int lazy;
    };
    
    Node arr[2000006];
    
    void pushUp(int k)
    {
    	int li = k << 1;
    	int ri = (k << 1) | 1;
    	arr[k].ls = arr[li].ls;
    	arr[k].rs = arr[ri].rs;
    	arr[k].ms = max(arr[k << 1].ms,arr[(k << 1) | 1].ms);
    	if(arr[li].rs != 0 && arr[ri].ls != 0)
    	{
    		if(arr[li].ls == arr[li].r - arr[li].l + 1)
    		{
    			arr[k].ls += arr[ri].ls;
    		}
    		if(arr[ri].rs == arr[ri].r - arr[ri].l + 1)
    		{
    			arr[k].rs += arr[li].rs;
    		}
    		arr[k].ms = max(arr[k].ms,arr[li].rs + arr[ri].ls);
    	}
    }
    
    void pushDown(int k)
    {
    	if(arr[k].lazy != -1)
    	{
    		int li = k << 1;
    		int ri = (k << 1) | 1;
    		int llen = arr[li].r - arr[li].l + 1;
    		int rlen = arr[ri].r - arr[ri].l + 1;
    		arr[li].lazy = arr[k].lazy;
    		arr[li].ls = llen * arr[k].lazy;
    		arr[li].rs = llen * arr[k].lazy;
    		arr[li].ms = llen * arr[k].lazy;
    		arr[ri].lazy = arr[k].lazy;
    		arr[ri].ls = rlen * arr[k].lazy;
    		arr[ri].rs = rlen * arr[k].lazy;
    		arr[ri].ms = rlen * arr[k].lazy;
    		arr[k].lazy = -1;
    	}
    }
    
    void segTree(int k,int l,int r)
    {
    	arr[k].l = l;
    	arr[k].r = r;
    	arr[k].lazy = -1;
    	if(l == r)
    	{
    		arr[k].ls = arr[k].rs = arr[k].ms = 1;
    		return;
    	}
    	int mid = (l + r) >> 1;
    	segTree(k << 1,l,mid);
    	segTree((k << 1) | 1,mid + 1,r);
    	pushUp(k);
    }
    
    void update(int k,int x,int y,int flag)
    {
    	if(arr[k].l == x && arr[k].r == y)
    	{
    		arr[k].lazy = flag;
    		arr[k].ls = (y - x + 1) * flag;
    		arr[k].rs = (y - x + 1) * flag;
    		arr[k].ms = (y - x + 1) * flag;
    		return;
    	}
    	pushDown(k);
    	int mid = (arr[k].r + arr[k].l) >> 1;
    	if(x > mid)
    	{
    		update((k << 1) | 1,x,y,flag);
    	}
    	else if(y <= mid)
    	{
    		update(k << 1,x,y,flag);
    	}
    	else
    	{
    		update(k << 1,x,mid,flag);
    		update((k << 1) | 1,mid + 1,y,flag);
    	}
    	pushUp(k);
    }
    
    int query(int k,int x)
    {
    	if(arr[k].l == arr[k].r)
    	{
    		return arr[k].l;
    	}
    	int mid = (arr[k].l + arr[k].r) >> 1;
    	pushDown(k);
    	if(arr[k << 1].ms >= x)
    	{
    		return query(k << 1,x);
    	}
    	if(arr[k << 1].rs + arr[(k << 1) | 1].ls >= x)
    	{
    		return arr[k << 1].r - arr[k << 1].rs + 1;
    	}
    	if(arr[(k << 1) | 1].ms >= x)
    	{
    		return query((k << 1) | 1,x);
    	}
    }
    
    int main()
    {
    	int n,m;
    	while(scanf("%d%d",&n,&m) != EOF)
    	{
    		segTree(1,1,n);
    		int flag;
    		int x,y;
    		while(m--)
    		{
    			scanf("%d",&flag);
    			if(flag == 1)
    			{
    				scanf("%d",&x);
    				if(arr[1].ms < x)
    				{
    					printf("0
    ");
    					continue;
    				}
    				int t = query(1,x);
    				printf("%d
    ",t);
    				update(1,t,t + x - 1,0);
    			}
    			else
    			{
    				scanf("%d%d",&x,&y);
    				update(1,x,x + y - 1,1);
    			}
    		 } 
    	}
    	
    	
    	return 0;
    }


  • 相关阅读:
    mysql 版本查看
    js 中文乱码
    浏览器内核
    Squid 代理服务器
    minicygwin
    firefox 插件开发
    ocx c++
    NetBeans
    android 虚拟机
    ExpandableListView
  • 原文地址:https://www.cnblogs.com/llguanli/p/6950758.html
Copyright © 2020-2023  润新知