【HDU】4614 Vases and Flowers 线段树

Vases and Flowers

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


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
 

Source
2013 Multi-University Training Contest 2

题目链接:【HDU】4614 Vases and Flowers

题目大意:
给你一串0~n-1的区间,初始值全0,有m次操作。( 1 < n , m < 50001 )
操作分两类:
1 X Y 从位置X开始寻找Y个0,如果不足Y个,则寻找尽量多的0,并将他们的值全部修改为1,输出第一个和最后一个修改的1的位置。
2 X Y 输出区间[ X , Y ]内1的个数,并将区间内的1修改为0。

题目分析:
区间修改、区间查询。
定义一个lazy标记,并且记录区间内1的个数即可。
每次操作1时,先查询区间[ X , N )内是否0存在,没有输出Can not put any one. 。如果有,看区间内0的个数是否大于Y,如果大于,直接就直接找[ X , N )内第Y个0,否则找最后一个。然后二分查找区间内第一个0以及最后一个要修改的0。
二分查找时利用区间内1的个数可以间接推导0的个数。比如区间[ L , R ]内1的个数为cnt,则区间内0的个数即R - L + 1 - cnt 。因此根据这一信息二分。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define rt o , l , r
#define root 1 , 1 , n
#define lson ls , l , m
#define rson rs , m + 1 , r
#define mid ( ( l + r ) >> 1 )
#define clear( A , X ) memset ( A , X , sizeof A )

const int maxN = 50005 ;

int set[maxN << 2] ;
int num[maxN << 2] ;

void PushUp ( int o ) {
	num[o] = num[ls] + num[rs] ;
}

void PushDown ( int o , int l , int r ) {
	if ( ~set[o] ) {
		int m = mid ;
		set[ls] = set[rs] = set[o] ;
		num[ls] = ( set[o] ? m - l + 1 : 0 ) ;
		num[rs] = ( set[o] ? r - m : 0 ) ;
		set[o] = -1 ;
	}
}

void Update ( int L , int R , int v , int o , int l , int r ) {s
	if ( L <= l && r <= R ) {
		set[o] = v ;
		num[o] = ( set[o] ? r - l + 1 : 0 ) ;
		return ;
	}
	PushDown ( rt ) ;
	int m = mid ;
	if ( L <= m ) Update ( L , R , v , lson ) ;
	if ( m <  R ) Update ( L , R , v , rson ) ;
	PushUp ( o ) ;
}

int Query ( int L , int R , int o , int l , int r ) {
	if ( L <= l && r <= R ) return num[o] ;
	PushDown ( rt ) ;
	int m = mid , ans = 0 ;
	if ( L <= m ) ans += Query ( L , R , lson ) ;
	if ( m <  R ) ans += Query ( L , R , rson ) ;
	return ans ;
}

int Search ( int x , int o , int l , int r ) {
	if ( l == r ) return l ;
	PushDown ( rt ) ;
	int m = mid , tmp = m - l + 1 - num[ls] ;
	//tmp表示左节点有的0的个数
	if ( tmp >= x ) return Search ( x , lson ) ;
	else return Search ( x - tmp , rson ) ;
}

void work () {
	//将0~n-1转化成1~n
	int n , m ;
	int ch , l , r , x , y ;
	scanf ( "%d%d" , &n , &m ) ;
	clear ( num ,  0 ) ;
	clear ( set , -1 ) ;
	while ( m -- ) {
		scanf ( "%d" , &ch ) ;
		if ( 1 == ch ) {
			scanf ( "%d%d" , &x , &y ) ;
			++ x ;
			int ans = n - x + 1 - Query ( x , n , root ) ;
			int tmp = n - num[1] - ans ;
			//ans表示[x , n]区间内0的个数。
			//tmp表示[1 , x - 1]区间内0的个数。s
			if ( 0 == ans ) {
				printf ( "Can not put any one.\n" ) ;
			}
			else {
				l = Search ( 1 + tmp , root ) ;
				if ( y > ans ) r = Search ( ans + tmp, root ) ;
				else r = Search ( y + tmp, root ) ;
				printf ( "%d %d\n" , l - 1 , r - 1 ) ;
				Update ( l , r , 1 , root ) ;
			}
		}
		if ( 2 == ch ) {
			scanf ( "%d%d" , &l , &r ) ;
			++ l , ++ r ;
			printf ( "%d\n" , Query ( l , r , root ) ) ;
			Update ( l , r , 0 , root ) ;
		}
	}
	printf ( "\n" ) ;
}

int main () {
	int T ;
	scanf ( "%d" , &T ) ;
	while ( T -- ) work () ;
	return 0 ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值