POJ 3667 splay区间合并练习

Hotel

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 12446   Accepted: 5363

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

代码:

/* ***********************************************
Author :rabbit
Created Time :2014/11/1 12:57:54
File Name :4.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=200200;
struct Node;
Node *null;
struct Node{
	Node *ch[2],*fa;
	int size;
	int lsum,rsum,msum,col,val;
	Node(){
		ch[0]=ch[1]=fa=null;
		col=0;
	}
	inline void setc(Node *p,int d){
		ch[d]=p;
		p->fa=this;
	}
	inline bool d(){
		return fa->ch[1]==this;
	}
	void clear(){
		size=1;
		ch[0]=ch[1]=fa=null;
		lsum=rsum=msum=val=1;
		col=-1;
	}
	inline void push_up(){
		if(this==null)return;
		size=ch[0]->size+ch[1]->size+1;
		lsum=ch[0]->lsum;
		rsum=ch[1]->rsum;
		msum=max(ch[0]->msum,ch[1]->msum);
		if(val){
			msum=max(msum,ch[0]->rsum+ch[1]->lsum+1);
			if(lsum==ch[0]->size)lsum+=ch[1]->lsum+1;
			if(rsum==ch[1]->size)rsum+=ch[0]->rsum+1;
		}
	}
	void update(int c){
		if(this==null)return;
		lsum=rsum=msum=c*size;
		val=col=c;
	}
	inline void push_down(){
		if(this==null)return;
		if(col!=-1){
			ch[0]->update(col);
			ch[1]->update(col);
			col=-1;
		}
	}
};
inline void rotate(Node *x){
	Node *f=x->fa,*ff=x->fa->fa;
	f->push_down();
	x->push_down();
	int c=x->d(),cc=f->d();
	f->setc(x->ch[!c],c);
	x->setc(f,!c);
	if(ff->ch[cc]==f)ff->setc(x,cc);
	else x->fa=ff;
	f->push_up();
}
inline void splay(Node *&root,Node *x,Node *goal){
	while(x->fa!=goal){
		if(x->fa->fa==goal)rotate(x);
		else{
			x->fa->fa->push_down();
			x->fa->push_down();
			x->push_down();
			bool f=x->fa->d();
			x->d()==f?rotate(x->fa):rotate(x);
			rotate(x);
		}
	}
	x->push_up();
	if(goal==null)root=x;
}
Node *get_kth(Node *r,int k){
	Node *x=r;
	x->push_down();
	while(x->ch[0]->size+1!=k){
		if(k<x->ch[0]->size+1)x=x->ch[0];
		else{
			k-=x->ch[0]->size+1;
			x=x->ch[1];
		}
		x->push_down();
	}
	return x;
}
Node pool[maxn],*tail,*node[maxn],*root;
void build(Node *&x,int l,int r,Node *fa){
	if(l>r)return;
	int mid=(l+r)/2;
	x=tail++;
	x->clear();
	x->fa=fa;
	node[mid]=x;
	build(x->ch[0],l,mid-1,x);
	build(x->ch[1],mid+1,r,x);
	x->push_up();
}
void init(int n){
	tail=pool;
	null=tail++;
	null->fa=null->ch[0]=null->ch[1]=null;
	null->size=0;
	null->val=null->lsum=null->rsum=null->msum=0;null->col=-1;
	Node *p=tail++;
	p->val=p->msum=p->rsum=p->lsum=0;p->col=-1;
	p->size=1;p->ch[0]=p->ch[1]=p->fa=null;
	root=p;
	p=tail++;
	p->val=p->msum=p->rsum=p->lsum=0;p->col=-1;
	p->size=1;p->ch[0]=p->ch[1]=p->fa=null;
	root->setc(p,1);
	build(root->ch[1]->ch[0],1,n,root->ch[1]);
	root->ch[1]->push_up();
	root->push_up();
}
void update(int l,int r,int c){
	splay(root,get_kth(root,l),null);
	splay(root,get_kth(root,r+2),root);
	root->ch[1]->ch[0]->update(c);
	root->ch[1]->push_up();
	root->push_up();
}
int query(int l,int r){
	splay(root,get_kth(root,l),null);
	splay(root,get_kth(root,r+2),root);
	return root->ch[1]->ch[0]->msum;
}
int find(Node *x,int k,int pos){
	x->push_down();
	//cout<<x->msum<<endl;
	if(x->ch[0]->msum>=k)return find(x->ch[0],k,pos);
	pos+=x->ch[0]->size;
	if(x->val&&x->ch[0]->rsum+x->ch[1]->lsum+1>=k)return pos-x->ch[0]->rsum;
	return find(x->ch[1],k,pos+1);
}
int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
     int n,m;
	 while(~scanf("%d%d",&n,&m)){
		 init(n);
		 //cout<<root->msum<<" "<<root->ch[0]->msum<<" "<<root->ch[1]->msum<<endl;
		 while(m--){
			 int op,x,y;
			 scanf("%d",&op);
			 if(op==1){
				 scanf("%d",&x);
				 if(root->msum<x){
					 puts("0");continue;
				 }
				 int k=find(root,x,0);
				 printf("%d\n",k);
				 update(k,k+x-1,0);
			 }
			 else{
				 scanf("%d%d",&x,&y);
				 update(x,x+y-1,1);
			 }
		 }
	 }
     return 0;
}

/*
int main()
{
	int n,m;
	while(cin>>n>>m) {
		init(n);
		while(m--){
			int op,x,y,z;
			cin>>op;
			if(op==1){
				cin>>x>>y>>z;
				update(x,y,z);
			}
			else{
				cin>>x>>y;
				cout<<query(x,y)<<endl;
			}
		}
	}
	return 0;
}
*/
时间: 2024-11-06 11:24:43

POJ 3667 splay区间合并练习的相关文章

POJ 3667 splay区间盘整运动

Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12446   Accepted: 5363 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

POJ 2955 Brackets 区间合并

输出一个串里面能匹配的括号数 状态转移方程: if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']')             dp[i][j]=dp[i+1][j-1]+2; 然后再区间合并 1 //#pragma comment(linker, "/STACK:167772160")//手动扩栈~~~~hdu 用c++交 2 #include<cstdio> 3 #include<cstring&

POJ 3667 Hotel(区间合并)

题目链接:http://poj.org/problem?id=3667 题意:宾馆有n个房间.有人来入住.共有2种操作 输入1和d,表示查询最左的连续d个空房间数的起始位置. 输入2,x和d,表示将从x开始长度为d的连续的房间清空. 思路:裸的区间合并.每个结点区间[l,r]存从左端点l开始向右最大连续空房间数lm,从右端点r开始向左最大连续空房间数rm和当前区间内最大连续空房间数. 代码: #include <iostream> #include <stdio.h> #inclu

POJ 3667(线段树区间合并)

http://poj.org/problem?id=3667 题意:两个操作 : 1 选出靠左的长度为a的区间. 2 把从 a到a+b的区间清空. 线段树区间合并+lazy // by caonima // hehe #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cmath> using namespace std; co

POJ 3667 Hotel 【线段树 区间合并 + Lazy-tag】

Hotel Time Limit: 3000MS Memory Limit: 65536K 链接:POJ 3667   Description The cows are journeying north to ThunderBay in Canada to gain cultural enrichment and enjoy a vacation on the sunnyshores of Lake Superior. Bessie, ever the competent travel agen

线段树(区间合并) POJ 3667 Hotel

题目传送门 1 /* 2 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 3 输入 2 a b:将[a,a+b-1]的房间清空 4 线段树(区间合并):lsum[]统计从左端点起最长连续空房间数,rsum[]类似,sum[]统计区间最长连续的空房间数, 5 它有三种情况:1.纯粹是左端点起的房间数:2.纯粹是右端点的房间数:3.当从左(右)房间起都连续时,加上另一个子节点 6 从左(右)房间起的数,sum[]再求最大值更新维护.理解没错,表达能力不足 7 详细解释:htt

POJ 3667 线段树的区间合并简单问题

题目大意:有一排标号1-N的房间.操作一:询问是不是有连续长度为a的空房间,有的话住进最左边(占用a个房间)操作二:将[a,a+b-1]的房间清空(腾出b个房间)思路:记录每个区间中“靠左”“靠右”“中间”的空房间线段树操作:update:区间替换query:询问满足条件的最左端点 题目链接: http://vjudge.net/problem/viewProblem.action?id=10354 题目操作: 因为这里找从最左边住起的房间,所以这里不能像常规地写query函数那样写了,终于发现

POJ - 3667 - Hotel (线段树 - 区间合并)

题目传送:Hotel 思路:线段树,区间合并,区间替换,查询最左断点,看胡浩版本的线段树好几天了,今天看这个看了好久,慢慢来吧,具体都写在注释里了 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #inc

POJ 3667 Hotel (初遇线段树区间合并)

题意: 有一个线段,从1到n,下面m个操作,操作分两个类型,以1开头的是查询操作,以2开头的是更新操作 1 w 表示在总区间内查询一个长度为w的可用区间并且要最靠左,能找到的话返回这个区间的左端点并占用了这个区间,找不到返回0 2 a len , 表示从单位a开始,清除一段长度为len的区间(将其变为可用,不被占用),不需要输出. 思路: 这是第一次遇到线段树区间合并的题目,写下感悟,还是对线段的更新和查询工作,但是查询的对象的性质已经不像单点那样,查询的是某个线段的最大可用区间是多少,还要一并