您的位置:首页 > 其它

HDU-4614-Vases and Flowers

2013-08-14 10:30 295 查看
这个题属于线段树,题意也就是说有2个操作,一个是从某个位置开始向花瓶里插花,一个是清除现在花瓶中的花。

思路:

用线段树进行维护2种操作,我保存的是当前区间的花瓶还有多少个是空的,也就是说可以插花

调试了好久~唉~

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=50010;
const int inf=1<<29;
struct node
{
int l;
int r;
int res;
}t[maxn*10];
int n,m,ansl,ansr;
void Build(int l,int r,int index)
{
t[index].l=l;
t[index].r=r;
t[index].res=t[index].r-t[index].l+1;
if(l==r)
return;
int mid=(l+r)>>1;
Build(l,mid,index<<1);
Build(mid+1,r,index<<1|1);
}
void PushDown(int index)
{
if(!t[index].res&&t[index].l<t[index].r)
{
t[index<<1].res=0;
t[index<<1|1].res=0;
}
if(t[index].res==t[index].r-t[index].l+1&&t[index].l<t[index].r)
{
t[index<<1].res=t[index<<1].r-t[index<<1].l+1;
t[index<<1|1].res=t[index<<1|1].r-t[index<<1|1].l+1;
}
}
int Put(int l,int num,int index)
{
PushDown(index);
if(!t[index].res||!num)
return 0;
if(t[index].l>=l&&t[index].res<=num&&t[index].res==t[index].r-t[index].l+1)
{
ansl=min(ansl,t[index].l);
ansr=max(ansr,t[index].r);
int res=t[index].res;
t[index].res=0;
return res;
}
int ans;
int mid=(t[index].l+t[index].r)>>1;
if(l>mid)
ans=Put(l,num,index<<1|1);
else
{
ans=Put(l,num,index<<1);
ans+=Put(l,num-ans,index<<1|1);
}
t[index].res=t[index<<1].res+t[index<<1|1].res;
return ans;
}
int Update(int l,int r,int index)
{
PushDown(index);
if(t[index].l==l&&t[index].r==r)
{
int res=t[index].r-t[index].l+1-t[index].res;
t[index].res=t[index].r-t[index].l+1;
return res;
}
int mid=(t[index].l+t[index].r)>>1,ans=0;
if(r<=mid)
ans=Update(l,r,index<<1);
else if(l>mid)
ans=Update(l,r,index<<1|1);
else
ans=Update(l,mid,index<<1)+Update(mid+1,r,index<<1|1);
t[index].res=t[index<<1].res+t[index<<1|1].res;
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
Build(1,n,1);
while(m--)
{
int op,a,b;
scanf("%d%d%d",&op,&a,&b);
a++;
if(op==1)
{
ansl=inf,ansr=-inf;
int x;
x=Put(a,b,1);
if(x==0)
{
printf("Can not put any one.\n");
continue;
}
printf("%d %d\n",ansl-1,ansr-1);
}
else
{
b++;
printf("%d\n",Update(a,b,1));
}
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: