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

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

Author

题意:n个花瓶,m个操作,1 表示从 le开始插入ri个花,if 没有空花瓶,输出一句话(下面),else输出插花起点和终点,2表示把le,到ri的花全部去掉

思路:二分花的起点和终点




#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8

#define fre(i,a,b)  for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N 50001

struct stud{
  int le,ri;
  int lazy;
  int len;
}f[N*4];

void pushdown(int pos)
{
	if(f[pos].lazy==-1) return ;

	if(f[pos].lazy)
	{
		f[L(pos)].len=f[L(pos)].ri-f[L(pos)].le+1;
		f[R(pos)].len=f[R(pos)].ri-f[R(pos)].le+1;
	}
    else
	{
		f[L(pos)].len=f[R(pos)].len=0;
	}

	f[L(pos)].lazy=f[R(pos)].lazy=f[pos].lazy;

	f[pos].lazy=-1;

}


void pushup(int pos)
{
	f[pos].len=f[L(pos)].len+f[R(pos)].len;
}

void build(int pos,int le,int ri)
{
	f[pos].le=le;
	f[pos].ri=ri;
	f[pos].len=ri-le+1;
	f[pos].lazy=-1;
	if(le==ri) return ;

	int mid=MID(le,ri);
	build(L(pos),le,mid);
	build(R(pos),mid+1,ri);
}

void update(int pos,int le,int ri,int va)
{
	if(f[pos].le==le&&f[pos].ri==ri)
	{
		if(va) f[pos].len=f[pos].ri-f[pos].le+1;
		else  f[pos].len=0;

		f[pos].lazy=va;

		return ;
	}

    pushdown(pos);

    int mid=MID(f[pos].le,f[pos].ri);

    if(mid>=ri)
		update(L(pos),le,ri,va);
	else
		if(mid<le)
		update(R(pos),le,ri,va);
	else
	{
		update(L(pos),le,mid,va);
		update(R(pos),mid+1,ri,va);
	}
    pushup(pos);
}

int query(int pos,int le,int ri)
{
     if(f[pos].le==le&&f[pos].ri==ri)
			return f[pos].len;

	 pushdown(pos);

	 int mid=MID(f[pos].le,f[pos].ri);

	 if(mid>=ri)
		return query(L(pos),le,ri);
	 else
		if(mid<le)
		return query(R(pos),le,ri);

	 return query(L(pos),le,mid)+query(R(pos),mid+1,ri);
}

int main()
{
	int i,j,op,t,n,k;
	sf(t);
	while(t--)
	{
		sff(n,k);
		n--;
		build(1,0,n);

		int le,ri;

		while(k--)
		{
		   sfff(op,le,ri);

		   if(op==2)
		   {
		   	  pf("%d\n",ri-le+1-query(1,le,ri));
		   	  update(1,le,ri,1);
		   }
           else
		   {
		   	  int len=query(1,le,n);

		   	  if(len==0)
			  {
			  	 pf("Can not put any one.\n");
			  	 continue;
			  }

              len=min(len,ri);

              int lle=le;
              int rri=n;

              int lans,rans;

              while(lle<=rri)     //二分左端点
			  {
			  	  int mid=MID(lle,rri);

			  	  if(query(1,le,mid)>0)
				  {
				  	lans=mid;
				  	rri=mid-1;
				  }
                  else
					lle=mid+1;
			  }

			  lle=le;
			  rri=n;

			  while(lle<=rri)   //二分右端点
			  {
			  	  int mid=MID(lle,rri);
				  int hello=query(1,le,mid);

			  	  if(hello>=len)
				  {
				  	rans=mid;
				  	rri=mid-1;
				  }
                  else
					lle=mid+1;
			  }

			  pf("%d %d\n",lans,rans);
			  update(1,lans,rans,0);
		   }
		}

		pf("\n");
	}
    return 0;
}