HDU4614 Vases and Flowers(线段树区间更新维护+二分)

题目链接

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

思路

线段树区间更新+查询,可以采用二分对空位进行选择,插花之后将花瓶状态更新,注意合理利用懒标记。

代码

#include<map>
#include<stack>
#include<queue>
#include<string>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define ls (k<<1)
#define rs (k<<1|1)
#define mid ((l+r)>>1)
using namespace std;
const int mod=1e9+7;
const int maxn=5e4+5;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
int m,n,t,tag[maxn<<2],tree[maxn<<2];
void down(int l,int r,int k)
{
    
    
	tree[ls]=(mid-l+1)*tag[k];
	tree[rs]=(r-mid)*tag[k];
	tag[ls]=tag[rs]=tag[k];
	tag[k]=-1;//0,1表示花瓶状态,所以这里需要赋为-1
}
void build(int l,int r,int k)
{
    
    
	if(l==r)
	{
    
    
		tag[k]=-1;
		tree[k]=1;
		return ;
	}
	build(l,mid,ls);
	build(mid+1,r,rs);
	tag[k]=-1;
	tree[k]=tree[ls]+tree[rs];
}
int query(int l,int r,int l1, int r1, int k) 
{
    
    
    if(l1<=l&&r<= r1)
        return tree[k];
    int res = 0;
    if(tag[k]!=-1)
    	down(l,r,k);
    if(l1 <= mid) res += query(l,mid,l1,r1,ls);
    if(r1 > mid) res += query(mid+1,r,l1,r1,rs);
    return res;
}
int search(int x,int n,int num)
{
    
    
	int l=x,r=n;
	while(l<r)
	{
    
    
		int t=query(1,n,x,mid,1);
		if(t>=num)
			r=mid;
		else
			l=mid+1;
	}
	return r;
}
void update(int l,int r,int l1,int r1,int v,int k)
{
    
    
	if(l1<=l&&r1>=r)
	{
    
    
		tree[k]=(r-l+1)*v;
		tag[k]=v;
		return ;
	}
	if(l==r)
		return ;
	if(tag[k]!=-1)
		down(l,r,k);
	if(l1<=mid)
		update(l,mid,l1,r1,v,ls);
	if(r1>mid)
		update(mid+1,r,l1,r1,v,rs);
	tree[k]=tree[ls]+tree[rs];
}
int main()
{
    
    
	scanf("%d",&t);
	while(t--)
	{
    
    
		scanf("%d%d",&n,&m);
		build(1,n,1);
		for(int i=1;i<=m;i++)
		{
    
    
			int s,x,y;
			scanf("%d%d%d",&s,&x,&y);
			if(s==1)
			{
    
    
				x++;
				int cnt=query(1,n,x,n,1);
				if(cnt==0)//无空位 
					printf("Can not put any one.\n");
				else
				{
    
    
					cnt=min(y,cnt);//获取最少能够放的花的数量
					int L=search(x,n,1);//查找空位
					int R=search(x,n,cnt);//查找足够插花的位置 
					printf("%d %d\n",L-1,R-1);//区间插花 
					update(1,n,L,R,0,1);//更新花瓶为满的状态
				}
			}
			else
			{
    
    
				x++,y++;
				int tmp=query(1,n,x,y,1);//剩余空位 
				update(1,n,x,y,1,1);//更新花瓶为空的状态 
				printf("%d\n",y-x+1-tmp);//输出x-y中被丢弃花的个数 
			}
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/WTMDNM_/article/details/108867124
今日推荐