POJ 3667 splay区间盘整运动
| 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 ≤
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
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;
}
*/
版权声明:本文博主原创文章。博客,未经同意不得转载。
POJ 3667 splay区间盘整运动的更多相关文章
- 线段树(区间合并) POJ 3667 Hotel
题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...
- POJ 3667 & HDU 3308 & HDU 3397 线段树的区间合并
看到讲课安排上 线段树有一节课"区间合并" 我是迷茫的 因为并没有见过 然后了解了一下题目 发现以前写过 还是很麻烦的树链剖分 大概是 解决带修改的区间查询"连续问题&q ...
- POJ 3667 Hotel(线段树)
POJ 3667 Hotel 题目链接 题意:有n个房间,如今有两个操作 1.找到连续长度a的空房间.入住,要尽量靠左边,假设有输出最左边的房间标号,假设没有输出0 2.清空[a, a + b - 1 ...
- hdu 1890 Robotic Sort(splay 区间反转+删点)
题目链接:hdu 1890 Robotic Sort 题意: 给你n个数,每次找到第i小的数的位置,然后输出这个位置,然后将这个位置前面的数翻转一下,然后删除这个数,这样执行n次. 题解: 典型的sp ...
- 算法模板——splay区间反转 2
实现功能:同splay区间反转 1(基于BZOJ3223 文艺平衡树) 这次改用了一个全新的模板(HansBug:琢磨了我大半天啊有木有),大大简化了程序,同时对于splay的功能也有所完善 这里面没 ...
- P2596 [ZJOI2006]书架 && Splay 区间操作(三)
P2596 [ZJOI2006]书架 题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书, ...
- POJ 2995 Brackets 区间DP
POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...
- POJ 3667 Hotel (线段树区间合并)
题目链接:http://poj.org/problem?id=3667 最初给你n间空房,m个操作: 操作1 a 表示检查是否有连续的a间空房,输出最左边的空房编号,并入住a间房间. 操作2 a b ...
- POJ 3667 Hotel(线段树+区间合并)
http://poj.org/problem?id=3667 题意: 有N个房间,M次操作.有两种操作(1)"1a",表示找到连续的长度为a的空房间,如果有多解,优先左边的,即表示 ...
随机推荐
- iOS 发布应用时屏蔽NSLog
在开发过程中,经常需要使用NSLog来进行调试,但是NSLog是非常影响性能的,所以我们应该在发布应用时屏蔽掉NSLog,但是如果通过手工的去一行一行的改得话,未免太枯燥与费时了,庆幸的是,我们可以通 ...
- Android 开发笔记“Eclipse 调试和快捷键”
原文地址:http://blog.sina.com.cn/s/blog_5cf876340100aswr.html Eclipse 调试器和 Debug 视图 Eclipse SDK 是针对 Java ...
- php基础知识(很简单一套适合零基础的朋友学习)
红色的一般都是重点,还有自己的一些废话 运算符 算术运算符: 基本运算(除数不能为0) 比较运算符: 大小比较(类型比较), 如果两个类型不一样,系统会自动转换成统一类型 赋值运算符: 基本赋值和运算 ...
- java源码解析——Stack类
在java中,Stack类继承了Vector类.Vector类和我们经常使用的ArrayList是类似的,底层也是使用了数组来实现,只不过Vector是线程安全的.因此可以知道Stack也是线程安全的 ...
- Matplotlib中文乱码
想要分析一批数据,画出图形会比较直观.所以就搜索了一下各种软件,最终选择使用python的matplotlib.原因也是因为python使用起来比较方便,虽然R才是分析数据的首选,不过,没有R的基础, ...
- 关于js的一些关键知识点(call,apply,callee, caller,clourse,prototypeChain)
可能不少学习javascript在使用call,apply,callee时会感到困惑,以下希望对于你有所帮助: 1.~~~call ,apply是函数(函数对象)的方法:callee是函数argume ...
- Node Node
http://www.nodejs.org/ http://outofmemory.cn/code-snippet/1403/node-javascript-classic-introduction- ...
- javascript 变量 命名规范 变量的作用域
原文:javascript 变量 命名规范 变量的作用域 大家好,我是小强老师,今天讲解的是变量 变量 小时候我们学过 这个 应用题 : X+1=2; 问 X 等于几? 答案是 1 对了,很聪 ...
- [Windows编程] 使用AttachThreadInput 来捕捉其它窗口的键盘输入
在一些情况下(比如屏幕软键盘或者输入法程序),自己的窗口没有输入焦点但是想要当前焦点窗口的键盘输入消息,可以使用Win32 API函数AttachThreadInput()来解决这个问题.Attach ...
- jdbc操作步骤和preparedStatment相比Statment的好处
java操纵数据库封装了一组API,通过这组API可以透明的操作各种数据库,一般来讲,操纵数据库的步骤是: 一. try{ 1.加载数据库驱动 Class.forName("数据库驱动类&q ...