• [USACO08FEB]Hotel G「线段树」


    (来自洛谷数据结构题表)

    题目描述

    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.

    参考样例,第一行输入n,m ,n代表有n个房间,编号为1---n,开始都为空房,m表示以下有m行操作,以下 每行先输入一个数 i ,表示一种操作:

    若i为1,表示查询房间,再输入一个数x,表示在1--n 房间中找到长度为x的连续空房,输出连续x个房间中左端的房间号,尽量让这个房间号最小,若找不到长度为x的连续空房,输出0,并且在这x个空房间中住上人。

    若i为2,表示退房,再输入两个数 x,y 代表 房间号 x---x+y-1 退房,即让房间为空。

    输入格式

    • 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

    输出格式

    • 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.

    输入输出样例

    输入 #1

    10 6
    1 3
    1 3
    1 3
    1 3
    2 5 5
    1 6
    

    输出 #1

    1
    4
    7
    0
    5
    

    思路分析

    敲着敲着就敲成了洛谷最优解

    • 这查询和修改上去一看就很线段树,但是这两个都有一些棘手(以前好像做过类似的)
    • 首先线段树需要记录一下区间内最大的连续空房间的长度,但这显然并不够,因为修改的时候需要合并,这时候是显然不能直接加和的,因为这样无法保证其连续,所以我们还需要在线段树中记录以左端点为起点的连续空房间的长度和以右端点为终点的连续空房间的长度,这时候在 pushup 合并的时候就可以分情况讨论了
    • 查询的时候好像也不太好搞,秉持一个原则就好:能向左就向左,不能就看能否作为中间点而不是直接向右

    (Code)

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #define R register
    #define N 50010
    using namespace std;
    inline int read(){
    	int x = 0,f = 1;
    	char ch = getchar();
    	while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    	return x*f;
    }
    int n,m,tag[N<<2];
    #define ls rt<<1
    #define rs rt<<1|1
    struct segment_tree{
    	int sum,len,lmax,rmax; //最长连续空房间长度,线段树对应区间长度,左(右)端点的连续空房间长度
    }tr[N<<2];
    void build(int rt,int l,int r){
    	tr[rt].sum = tr[rt].lmax = tr[rt].rmax = tr[rt].len = (r-l+1);//初始全为空
    	if(l==r)return;
    	int mid = (l+r)>>1;
    	build(ls,l,mid);
    	build(rs,mid+1,r);
    }
    void pushdown(int rt,int l,int r){//分情况,看是check in还是check out
    	if(!tag[rt])return;
    	int mid = (l+r)>>1;
    	if(tag[rt]==1){
    		tr[ls].sum = tr[ls].lmax = tr[ls].rmax = mid-l+1;
    		tr[rs].sum = tr[rs].lmax = tr[rs].rmax = r-mid;
    	}
    	else{
    		tr[ls].sum = tr[ls].lmax = tr[ls].rmax = 0;
    		tr[rs].sum = tr[rs].lmax = tr[rs].rmax = 0;
    	}
    	tag[ls] = tag[rs] = tag[rt];
    	tag[rt] = 0;
    }
    void pushup(int rt){//分情况合并
    	if(tr[ls].sum==tr[ls].len)tr[rt].lmax = tr[ls].len + tr[rs].lmax;
    	else tr[rt].lmax = tr[ls].lmax;
    	if(tr[rs].sum==tr[rs].len)tr[rt].rmax = tr[rs].len + tr[ls].rmax;
    	else tr[rt].rmax = tr[rs].rmax;
    	tr[rt].sum = max(tr[ls].rmax+tr[rs].lmax,max(tr[ls].sum,tr[rs].sum));
    }
    void modify(int rt,int l,int r,int s,int t,int flag){//同样要分类
    	if(s<=l&&t>=r){
    		if(flag==1)tr[rt].sum = tr[rt].lmax = tr[rt].rmax = r-l+1;
    		else tr[rt].sum = tr[rt].lmax = tr[rt].rmax = 0;
    		tag[rt] = flag;
    		return;
    	}
    	pushdown(rt,l,r);
    	int mid = (l+r)>>1;
    	if(s<=mid)modify(ls,l,mid,s,t,flag);
    	if(t>mid)modify(rs,mid+1,r,s,t,flag);
    	pushup(rt);
    }
    int query(int rt,int l,int r,int aim){
    	if(l==r)return l;//一直向左也得有个头
    	pushdown(rt,l,r);
    	int mid = (l+r)>>1;
    	if(tr[ls].sum>=aim)return query(ls,l,mid,aim);
    	if(tr[ls].rmax+tr[rs].lmax>=aim)return mid-tr[ls].rmax+1;
    	return query(rs,mid+1,r,aim);
    }
    int main(){
    	n = read(),m = read();
    	build(1,1,n);
    	for(R int i = 1;i <= m;i++){
    		int opt = read();
    		if(opt==1){
    			int x = read();
    			if(tr[1].sum<x){puts("0");continue;}
    			int ans = query(1,1,n,x);
    			modify(1,1,n,ans,ans+x-1,-1);
    			printf("%d
    ",ans);
    		}
    		else{
    			int x = read(),y = read();
    			modify(1,1,n,x,x+y-1,1);
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    那是什么进程 —— svchost.exe是什么? 它为何运行?
    共享一下我的博客皮肤
    C#3.0亮点 —— 关键字var和匿名类型
    改善代码设计 —— 优化物件之间的特性(Moving Features Between Objects)
    C#3.0亮点 —— 分部方法
    解决C#中一个"异步方法却同步执行"的问题
    改善代码设计 —— 简化函数调用(Making Method Calls Simpler)
    改善代码设计 —— 总结篇(Summary)
    理解A*寻路算法具体过程
    改善代码设计 —— 组织好你的数据(Composing Data)
  • 原文地址:https://www.cnblogs.com/hhhhalo/p/13774065.html
Copyright © 2020-2023  润新知