Vases and Flowers HDU - 4614(线段树+二分)

Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N-1. When she receive some flowers, she will try to put them in the vases, one flower in one vase. She randomly choose the vase A and try to put a flower in the vase. If the there is no flower in the vase, she will put a flower in it, otherwise she skip this vase. And then she will try put in the vase A+1, A+2, …, N-1, until there is no flower left or she has tried the vase N-1. The left flowers will be discarded. Of course, sometimes she will clean the vases. Because there are too many vases, she randomly choose to clean the vases numbered from A to B(A <= B). The flowers in the cleaned vases will be discarded.
Input
  The first line contains an integer T, indicating the number of test cases.
  For each test case, the first line contains two integers N(1 < N < 50001) and M(1 < M < 50001). N is the number of vases, and M is the operations of Alice. Each of the next M lines contains three integers. The first integer of one line is K(1 or 2). If K is 1, then two integers A and F follow. It means Alice receive F flowers and try to put a flower in the vase A first. If K is 2, then two integers A and B follow. It means the owner would like to clean the vases numbered from A to B(A <= B).
Output
  For each operation of which K is 1, output the position of the vase in which Alice put the first flower and last one, separated by a blank. If she can not put any one, then output ‘Can not put any one.’. For each operation of which K is 2, output the number of discarded flowers.
   Output one blank line after each test case.
Sample Input
2
10 5
1 3 5
2 4 5
1 1 8
2 3 6
1 8 8
10 6
1 2 5
2 3 4
1 0 8
2 2 5
1 4 4
1 2 3
Sample Output
[pre]3 7
2
1 9
4
Can not put any one.

2 6
2
0 9
4
4 5
2 3
题意:有n个花瓶(0~ n-1),有m次操作。1操作:给你一个起始的位置和鲜花数,问可以插花的初始位置和最终位置是哪两个。2操作:清空x~y的花瓶,问可以清空多少个花瓶。
思路:对于2操作来说就是区间lazy操作。但是对于1操作来说,起始位置和终止位置怎么确定?一开始我们把线段树上所有节点都设置为1,代表花瓶没有花,0代表着花瓶有花。对于起始位置,我们二分去从给定的起始位置找能插入的第一支花的位置,因为位置坐标越大,花数呈现非递减性质,可以用二分去找。对于最后的位置,我们也是二分去找,但是不能用给定的花数,因为有可能不一定都装下。所以在给定的花数和最多能装下的花数去一个最小值。记住位置是从0~n-1的。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=5e4+100;
struct node{
	int l;
	int r;
	int sum;
	int lazy;
}p[maxx<<2];
int n,m;

inline void pushup(int cur)
{
	p[cur].sum=p[cur<<1].sum+p[cur<<1|1].sum;
}
inline void pushdown(int cur)
{
	if(p[cur].lazy!=-1)
	{
		p[cur<<1].lazy=p[cur<<1|1].lazy=p[cur].lazy;
		p[cur<<1].sum=(p[cur<<1].r-p[cur<<1].l+1)*p[cur].lazy;
		p[cur<<1|1].sum=(p[cur<<1|1].r-p[cur<<1|1].l+1)*p[cur].lazy;
		p[cur].lazy=-1;
	}
}
inline void build(int l,int r,int cur)
{
	p[cur].l=l;
	p[cur].r=r;
	p[cur].lazy=-1;
	if(l==r)
	{
		p[cur].sum=1;
		return ;
	}
	int mid=l+r>>1;
	build(l,mid,cur<<1);
	build(mid+1,r,cur<<1|1);
	pushup(cur);
}
inline void update(int l,int r,int v,int cur)//更新的时候,选定了一段位置,这段位置中的花瓶状态就一定是一样的。
{
	int L=p[cur].l;
	int R=p[cur].r;
	if(l<=L&&R<=r)
	{
		p[cur].sum=(R-L+1)*v;
		p[cur].lazy=v;
		return ;
	}
	pushdown(cur);
	int mid=L+R>>1;
	if(r<=mid) update(l,r,v,cur<<1);
	else if(l>mid) update(l,r,v,cur<<1|1);
	else update(l,mid,v,cur<<1),update(mid+1,r,v,cur<<1|1);
	pushup(cur);
}
inline int query(int l,int r,int cur)
{
	int L=p[cur].l;
	int R=p[cur].r;
	if(l<=L&&R<=r) return p[cur].sum;
	pushdown(cur);
	int mid=L+R>>1;
	if(r<=mid) return query(l,r,cur<<1);
	else if(l>mid) return query(l,r,cur<<1|1);
	else return query(l,mid,cur<<1)+query(mid+1,r,cur<<1|1);
	pushup(cur);
}
inline void solve1(int x,int y)
{
	x++;
	int cnt=query(x,n,1);
	int l=x,r=n,mid,ans1=0,ans2=0;
	while(l<=r)
	{
		mid=l+r>>1;
		if(query(x,mid,1)>=1) r=mid-1,ans1=mid;
		else l=mid+1;
	}
	l=x,r=n;
	while(l<=r)
	{
		mid=l+r>>1;
		if(query(x,mid,1)>=min(cnt,y)) r=mid-1,ans2=mid;
		else l=mid+1;
	}
	if(ans1==0||ans2==0) 
	{
		printf("Can not put any one.\n");
		return ;
	}
	else printf("%d %d\n",ans1-1,ans2-1);
	update(ans1,ans2,0,1);//插花花瓶状态就是0,没花状态就是1.
}
inline void solve2(int x,int y)
{
	x++,y++;
	printf("%d\n",y-x+1-query(x,y,1));
	update(x,y,1,1);
}
int main()
{
	int t,op,x,y;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		build(1,n,1);
		while(m--)
		{
			scanf("%d%d%d",&op,&x,&y);
			if(op==1) solve1(x,y);
			else solve2(x,y);
		}
		printf("\n");
	}
	return 0;
}

努力加油a啊,(o)/~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值