hdu4614Vases and Flowers(线段树,段设置,更新时范围的右边值为变量)

本文介绍了一个涉及花瓶管理和操作的算法问题。主要内容包括接收花朵并尝试将它们放入一系列编号的花瓶中,以及随机选择一定范围内的花瓶进行清理。算法通过特定的数据结构实现了高效的花朵放置和清理操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
  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
3 7 2 1 9 4 Can not put any one. 2 6 2 0 9 4 4 5 2 3
题目意思:每一组的第一行有两个数n,m, n表示花瓶数量0~n-1,开始全为空,接下来有m组,每组有数k,a,b;当k=1时, 从a位置开始插花,b为花数量,当花瓶有花时,跳过当前,直到找到空花瓶再插入,输出插入第一支花的位置和最后一支花的位置,花可以没插完,当一支都没有插入则输出Can not put any one.;当k=2时,清空【a,b】的花瓶,并输出在范围内花的数量。
#include<stdio.h>
#define N 50010
struct node
{
	int sum,b;//sum为在范围内的花数,b为判断是否全为空或全为满则为1,否则为0
}tree[4*N];
void biulde(int l,int r,int k)
{
	int m=(l+r)/2;
	tree[k].sum=0; tree[k].b=1;
	if(l==r) return ;
	biulde(l,m,k*2);  biulde(m+1,r,k*2+1);
}
void set_child(int l,int r,int k)
{
	int m=(l+r)/2;
	tree[k*2].b=tree[k*2+1].b=1;
	if(tree[k].sum==r-l+1){
		tree[k*2].sum=m-l+1; tree[k*2+1].sum=r-m;
	}
	else{
		tree[k*2].sum=0; tree[k*2+1].sum=0;
	}
}
int QL,QR,L,R,ans,n;
void putInFlower(int l,int r,int k)
{
	if(ans<=0) return ;
	int m=(l+r)/2;
	if(L<=l&&r<=R&&tree[k].b)
	{
		if(!tree[k].sum) {
			int tans=ans;
			ans-=(r-l+1); tree[k].sum=r-l+1;
			if(QL<0) QL=l-1;
			 QR=r-1;
		}
		else{//跳动插花范围的右边值,R刚好是插完花的右边范围的最小值,除非超出花瓶数量,则为n
			R+=(r-l+1); if(R>n) R=n;
		}
		return ;
	}
	if(tree[k].b) 
		set_child(l,r,k);
	 tree[k].b=0;
	if(L<=m) putInFlower(l,m,k*2);
	if(R>m) putInFlower(m+1,r,k*2+1);

	tree[k].sum=tree[k*2].sum+tree[k*2+1].sum;
	if(tree[k].sum==r-l+1||!tree[k].sum)
		tree[k].b=1;
}
void clear(int l,int r,int k)
{
	int m=(l+r)/2;
	if(L<=l&&r<=R)
	{
		ans+=tree[k].sum; tree[k].b=1; tree[k].sum=0;
		return ;
	}
	if(tree[k].b)
		set_child(l,r,k);
	tree[k].b=0;
	if(L<=m) clear(l,m,k*2);
	if(R>m) clear(m+1,r,k*2+1);

	tree[k].sum=tree[k*2].sum+tree[k*2+1].sum;
	if(tree[k].sum==r-l+1||!tree[k].sum)
		tree[k].b=1;
}
int main()
{
	int t,m,x;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		biulde(1,n,1);
		while(m--)
		{
			scanf("%d%d",&x,&L); L++;
			if(x==1){
				scanf("%d",&ans);
				R=L+ans-1; QL=QR=-1;
				 putInFlower(1,n,1);
				if(QR>=0) 
					printf("%d %d\n",QL,QR);
				else
					printf("Can not put any one.\n");
			}
			else{
				scanf("%d",&R); R++; ans=0;
				clear(1,n,1);
				printf("%d\n",ans);
			}
		}	
		printf("\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值