# POJ 3667 Hotel 区间合并+线段树 *

156人阅读 评论(0)

1.MaxLen=max( max(2.MaxLen,3.MaxLen) , 2.RightLen+3.LeftLen)

AC code：688MS

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=50000+5;
const int INF=(1<<30);
struct Node{
int L,R,mid;
int MaxLen,LeftLen,RightLen; //L~R最大线段长度，从L开始的最大线段长都，从R开始的
int Occupied;  //0无住客 ,1 满住客
Node *pLeft,*pRight;
void init(){
if(Occupied==1)  MaxLen=LeftLen=RightLen=0;
else if(Occupied==0) MaxLen=LeftLen=RightLen=R-L+1;
}
Node(int L=0,int R=0):L(L),R(R){
pLeft=pRight=NULL;
Occupied=0;
mid=(L+R)/2;
MaxLen=LeftLen=RightLen=R-L+1;
}
}tree[maxn*2];
int nNode;
void BuildTree(Node *root,int s,int e)
{
*root=Node(s,e);
if(s==e) return;
root->pLeft=++nNode+tree;
root->pRight=++nNode+tree;
BuildTree(root->pLeft,s,root->mid);
BuildTree(root->pRight,root->mid+1,e);
}
void update(Node *root)
{
int LO=root->pLeft->Occupied,RO=root->pRight->Occupied;
int &TO=root->Occupied;
if(LO==-1||RO==-1) TO=-1;
else if(LO==1&&RO==1) TO=1;
else if(LO==0&&RO==0) TO=0;
else TO=-1;

if(TO==1||TO==0) root->init();
else {
root->MaxLen=max(root->pLeft->MaxLen,root->pRight->MaxLen);
root->MaxLen=max(root->MaxLen,root->pLeft->RightLen+root->pRight->LeftLen);
root->LeftLen =root->pLeft->LeftLen;
root->RightLen=root->pRight->RightLen;
if(root->pLeft->Occupied==0)  root->LeftLen+=root->pRight->LeftLen;
if(root->pRight->Occupied==0) root->RightLen+=root->pLeft->RightLen;
}
}
{
if(root->Occupied!=-1&&root->L!=root->R) {   //每次都要更新
root->pLeft->Occupied=root->pRight->Occupied=root->Occupied;
root->pLeft->init(); root->pRight->init();
}
if(root->L==s&&root->R==e)
{
root->LeftLen=root->RightLen=root->MaxLen=0;
root->Occupied=1;
return;
}
else {
}
update(root);
}
void Delete(Node *root,int s,int e)
{
if(root->Occupied!=-1&&root->L!=root->R) {
root->pLeft->Occupied=root->pRight->Occupied=root->Occupied;
root->pLeft->init(); root->pRight->init();
}
if(root->L==s&&root->R==e)
{
root->Occupied=0;
root->LeftLen=root->RightLen=root->MaxLen=e-s+1;
return;
}
if(e<=root->mid)        Delete(root->pLeft,s,e);
else if(s>=root->mid+1) Delete(root->pRight,s,e);
else {
Delete(root->pLeft,s,root->mid);
Delete(root->pRight,root->mid+1,e);
}
update(root);
}
int QueryLen(Node *root,int len)
{
if(root->Occupied!=-1&&root->L!=root->R) {
root->pLeft->Occupied=root->pRight->Occupied=root->Occupied;
root->pLeft->init(); root->pRight->init();
}
if(root->pLeft->MaxLen>=len)       return QueryLen(root->pLeft ,len);
else if(root->pLeft->RightLen+root->pRight->LeftLen>=len)
return root->pLeft->R-root->pLeft->RightLen+1;
else if(root->pRight->MaxLen>=len) return QueryLen(root->pRight,len);
else return 0;
}
int main()
{
int N,M,c,n,m;
scanf("%d%d",&N,&M);
nNode=0; BuildTree(tree,1,N);
while(M--)
{
scanf("%d",&c);
if(c==1) {
scanf("%d",&n);
int ans=QueryLen(tree,n);
cout<<ans<<endl;
}
else {
scanf("%d%d",&n,&m);
Delete(tree,n,n+m-1);
}
}
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：73856次
• 积分：5203
• 等级：
• 排名：第6154名
• 原创：467篇
• 转载：6篇
• 译文：0篇
• 评论：7条
文章分类
评论排行
最新评论