【HDU4614】【线段树】【二分】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013200703/article/details/48051363

Vases and Flowers

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


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
SYSU
 

Source
 

Recommend
zhuyuanchen520




#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
    
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define mp push_back
#define lson l,m,rt<<1
#define rson m,r,rt<<1|1

int T,m,n;
int sum[50010*4];
int col[50010*4];
void build(int l,int r,int rt)
{

}

void PushDown(int rt,int l1,int l2)
{
	if(col[rt] != -1)
	{
		col[rt<<1] = col[rt<<1|1] = col[rt];
		sum[rt<<1] = l1 * col[rt];
		sum[rt<<1|1] = l2 * col[rt];
 		col[rt] = -1;
	}
}

int query(int L,int R,int l,int r,int rt)
{
	if(L <= l && r <= R)
	{
		return sum[rt];
	}

	int m = (l + r) >> 1;
	PushDown(rt,m-l,r-m);

	int ret = 0;
	if(L <= m-1) ret += query(L,min(R,m),lson);
	if(m <= R-1) ret += query(max(L,m),R,rson);

	return ret;
}

void PushUp(int rt)
{
	sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}

void update(int L,int R,int c,int l,int r,int rt)
{
	if(L <= l && r <= R)
	{
		col[rt] = c;
		sum[rt] = (r - l) * c;
		return ;
	}

	int  m = (l + r) >> 1;
	PushDown(rt,m-l,r-m);

	if(L <= m-1) update(L,min(R,m),c,lson);
	if(m <= R-1) update(max(L,m),R,c,rson);

	PushUp(rt);
} 
int queryleft(int left,int right)
{
	int l = left;
	int r = right;
	int last = -1;
	while(l < r)
	{
		int mid = (l + r) >> 1;
		int ans = query(l,mid+1,0,m,1);
		if(ans < mid+1 - l)
		{
			last = mid;
			r = mid;
		}
		else
		{
			l = mid + 1;
		}
	}
	return last;
}

int queryright(int left,int right,int nums)
{
	int l = left;
	int r = right;
	int last = right - 1;
	nums = min(nums,m-left-query(left,m,0,m,1));
	while(l < r)
	{
		int mid = (l + r) >> 1;
		int ans = query(left,mid+1,0,m,1);
		if(mid+1-left - ans >= nums)
		{
			last = mid;
			r = mid;
		}
		else
		{
			l = mid + 1;
		}
	}
	return last;
}



int main()
{
	scanf("%d",&T);
	while(T --)
	{
		scanf("%d%d",&m,&n);
		//build(0,n,1);
		memset(sum,0,sizeof(sum));
		memset(col,-1,sizeof(col));
		for(int i=0;i<n;i++)
		{
			int k, a, b;
			scanf("%d%d%d",&k,&a,&b);
			if(k == 1)
			{
				int ans = query(a,m,0,m,1);
				if(ans == m - a)
				{
					printf("Can not put any one.\n");
					continue;
				}

				int l = queryleft(a,m);
				int r = queryright(l,m,b);
				printf("%d %d\n",l,r);
				update(l,r+1,1,0,m,1);
			}
			else
			{
				printf("%d\n",query(a,b+1,0,m,1));
				update(a,b+1,0,0,m,1);
			}
		}

		puts("");
	}
	//while(1);
}


猜你喜欢

转载自blog.csdn.net/u013200703/article/details/48051363
今日推荐