首页 > 代码库 > hdu4614 Vases and Flowers

hdu4614 Vases and Flowers

Vases and Flowers

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2148    Accepted Submission(s): 836


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
[pre]3 7 2 1 9 4 Can not put any one. 2 6 2 0 9 4 4 5 2 3 [/pre]
 

Source
2013 Multi-University Training Contest 2
 

Recommend
zhuyuanchen520

插花时,二分起始点即可

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
using namespace std;
struct node
{
	int l,r,flower;
	bool flag;
}tree[50001<<2];
void create(int l,int r,int k)
{
	tree[k].l=l;
	tree[k].r=r;
	tree[k].flower=0;
	tree[k].flag=0;
	if(l==r)
		return;
	int m=(l+r)>>1;
	create(l,m,k<<1);
	create(m+1,r,k<<1|1);
}
int discard;
void update(int l,int r,int k,bool flag)
{
	if(l==tree[k].l&&r==tree[k].r)
	{
		if(flag)
			tree[k].flower=r-l+1;
		else
		{
			discard+=tree[k].flower;
			tree[k].flower=0;
		}
		tree[k].flag=1;
		return;
	}
	if(tree[k].flag)
	{
		tree[k].flag=0;
		tree[k<<1].flag=tree[k<<1|1].flag=1;
		if(tree[k].flower==0)
			tree[k<<1].flower=tree[k<<1|1].flower=0;
		else
		{
			tree[k<<1].flower=tree[k<<1].r-tree[k<<1].l+1;
			tree[k<<1|1].flower=tree[k<<1|1].r-tree[k<<1|1].l+1;
		}
	}
	int m=(tree[k].l+tree[k].r)>>1;
	if(r<=m)
		update(l,r,k<<1,flag);
	else if(l>m)
		update(l,r,k<<1|1,flag);
	else
	{
		update(l,m,k<<1,flag);
		update(m+1,r,k<<1|1,flag);
	}
	tree[k].flower=tree[k<<1].flower+tree[k<<1|1].flower;
}
int seek(int l,int r,int k)
{
	if(l==tree[k].l&&r==tree[k].r)
		return tree[k].flower;
	if(tree[k].flag)
	{
		tree[k].flag=0;
		tree[k<<1].flag=tree[k<<1|1].flag=1;
		if(tree[k].flower==0)
			tree[k<<1].flower=tree[k<<1|1].flower=0;
		else
		{
			tree[k<<1].flower=tree[k<<1].r-tree[k<<1].l+1;
			tree[k<<1|1].flower=tree[k<<1|1].r-tree[k<<1|1].l+1;
		}
	}
	int m=(tree[k].l+tree[k].r)>>1;
	if(r<=m)
		return seek(l,r,k<<1);
	else if(l>m)
		return seek(l,r,k<<1|1);
	else
		return seek(l,m,k<<1)+seek(m+1,r,k<<1|1);
}
int n;
void add(int a,int b)
{
	if(seek(a,n-1,1)==n-a)
	{
		puts("Can not put any one.");
		return;
	}
	int l=a,r=n-1;
	while(l<=r)
	{
		int m=(l+r)>>1;
		if(m-a+1-seek(a,m,1)<1)
			l=m+1;
		else
			r=m-1;
	}
	int x=l;
	l=a;r=n-1;
	while(l<=r)
	{
		int m=(l+r)>>1;
		if(m-a+1-seek(a,m,1)<b)
			l=m+1;
		else
			r=m-1;
	}
	int y=l;
	if(y>n-1)
		y=n-1;
	int goal=y-x+1-seek(x,y,1);
	l=x;r=y;
	while(l<=r)
	{
		int m=(l+r)>>1;
		if(m-x+1-seek(x,m,1)<goal)
			l=m+1;
		else
			r=m-1;
	}
	y=l;
	update(x,y,1,1);
	printf("%d %d\n",x,y);
}
void clean(int l,int r)
{
	discard=0;
	update(l,r,1,0);
	printf("%d\n",discard);
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int m;
		scanf("%d%d",&n,&m);
		create(0,n-1,1);
		while(m--)
		{
			int k,a,b;
			scanf("%d%d%d",&k,&a,&b);
			if(k==1)
				add(a,b);
			else
				clean(a,b);
		}
		puts("");
	}
}


hdu4614 Vases and Flowers