hdu4614(二分法+线段树)

Vases and Flowers

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


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]
 
 
这是个比较裸的线段树,但比赛时没有想到二分查找法+线段树,一直希望设一个左右第一个为1的两个变量,结果搞来搞去没搞出来
这两天一直没看解题报告,终于自己写出来了,枚举思想万岁,二分查找加快枚举速度。
 
#include
#include
using namespace std;
#define MAX 50000

struct node 
{
	int left,right,num,tag;
}tree[MAX*3];

/*
int ml,mr;
int Min(int a,int b)
{
	return ab?a:b;
}
*/


void build( int id, int left, int right )
{
    tree[id].left = left;
    tree[id].right = right;
	tree[id].num=right-left+1;
	tree[id].tag=0;
    if( left == right )
    {
		return ;
    }
    else
    {
        int mid = ( left + right )/2;        
        build( id * 2, left, mid );
        build( id * 2 + 1, mid + 1, right );
	}
}

int qurry(int id,int l,int r)
{
	if(tree[id].tag!=0)
	{
		if(tree[id].tag==1)
			return r-l+1;
		else 
			return 0;
	}
	if(tree[id].left==l&&tree[id].right==r)
	{
		return tree[id].num;
	}
	int mid=(tree[id].left+tree[id].right)/2;
	if(r<=mid)
		return qurry(id*2,l,r);
	else if(l>mid)
		return qurry(id*2+1,l,r);
	else
	{
		return qurry(id*2,l,mid)+qurry(id*2+1,mid+1,r);
	}
}

void updata(int id,int l,int r,int v)
{
    if(tree[id].left==l&&tree[id].right==r)
    {
		tree[id].tag=v;
		if(v==-1)
			tree[id].num=0;
		else 
			tree[id].num=r-l+1;
		return ;
    }
	if(tree[id].tag!=0)
	{
		tree[id*2].tag=tree[id].tag;	
		tree[id*2+1].tag=tree[id].tag;
		if(tree[id].tag==-1)
		{
			tree[id*2].num=0;	
	    	tree[id*2+1].num=0;
		}
		else if(tree[id].tag==1)
		{
				tree[id*2].num=(tree[id*2].right-tree[id*2].left+1);	
	    	    tree[id*2+1].num=(tree[id*2+1].right-tree[id*2+1].left+1);;
		}
		tree[id].tag=0;
	}
	tree[id].tag=0;
	int mid=(tree[id].left+tree[id].right)/2;
    if(r<=mid) 
		updata(id * 2, l,r,v);
	else if(l>mid)
		updata(id * 2+1, l,r,v);
    else 
	{
		updata(id*2,l,mid,v);
		updata(id*2+1,mid+1,r,v);
	}
	tree[id].num=tree[id*2].num+tree[id*2+1].num;
}


/*
int getup(int id,int st,int sum)
{
	if(tree[id].tag!=0)
	{
		tree[id*2].tag=tree[id].tag;	
		tree[id*2+1].tag=tree[id].tag;

		tree[id].tag=0;
	}
	if(tree[id].num==sum)
	{
	//	cout<<"mr="<>cas;
	while(cas--)
	{
		scanf("%d%d",&n,&m);
		build(1,1,n);	
		for(i=0;i0)
							ansl=mid,high=mid-1;
						else 
							low=mid+1;
					}

					//二分法求上界
					high=n,low=a;
					ansr=n;
					tmp=qurry(1,a,n);
					if(tmp=b)
							ansr=mid,high=mid-1;
						else 
							low=mid+1;
					}
					updata(1,ansl,ansr,-1);
					printf("%d %d\n",ansl-1,ansr-1);
				}
			}
			else 
			{
				a++,b++;
				tmp=qurry(1,a,b);//[a,b]之间有多少不用修改
				updata(1,a,b,1);
				printf("%d\n",b-a+1-tmp);
			}
		}
		printf("\n");
	}
	return 0;
}

你可能感兴趣的:(数据结构-线段树(树状数组),折半法)