HDU 4614 Vases and Flowers

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614


Vases and Flowers

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 3095    Accepted Submission(s): 1210



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

思路:很容易看出是线段树的题。那么怎么做呢?用每个节点对应的区间来存储该区间的空位数,那么对于将花放进花瓶,找出第一朵花和最后一朵花的位置,可以用二分来实现,将放花的开始位置作为二分下限(并不代表第一朵花可以放在这),最右位置作为上限,那么对于每一个中间位置,求开始位置到该中间位置的空位数,然后依条件判断缩短区间,最后返回位置信息。将得到的区间置为有花的状态。对于清理花瓶,也是求出对应区间的空位数,用总的位置数减去空位数,即是答案。将该区间置为没有花的状态。详见代码。


附上AC代码:

#include <bits/stdc++.h>
#define lrt rt<<1
#define rrt rt<<1|1
#define lson l, m, lrt
#define rson m+1, r, rrt
using namespace std;
const int maxn = 50005;
int sumv[maxn<<2], setv[maxn<<2];
int n, q;

void push_up(int rt){
	sumv[rt] = sumv[lrt]+sumv[rrt];
}

void build(int l, int r, int rt){
	sumv[rt] = r-l+1;
	setv[rt] = -1;
	if (l == r)
		return ;
	int m = (l+r)>>1;
	build(lson);
	build(rson);
}

void push_down(int l, int r, int rt){
	if (setv[rt] != -1){
		setv[lrt] = setv[rrt] = setv[rt];
		int m = (l+r)>>1;
		sumv[lrt] = setv[rt] ? 0 : m-l+1;
		sumv[rrt] = setv[rt] ? 0 : r-m;
		setv[rt] = -1;
	}
}

void update(int cl, int cr, int val, int l, int r, int rt){
	if (cl<=l && r<=cr){
		setv[rt] = val;
		sumv[rt] = val ? 0 : r-l+1;
		return ;
	}
	push_down(l, r, rt);
	int m = (l+r)>>1;
	if (cl <= m)
		update(cl, cr, val, lson);
	if (cr > m)
		update(cl, cr, val, rson);
	push_up(rt);
}

int queries(int ql, int qr, int l, int r, int rt){
	if (ql<=l && r<=qr)
		return sumv[rt];
	push_down(l, r, rt);
	int m = (l+r)>>1;
	int sumr = 0;
	if (ql <= m)
		sumr += queries(ql, qr, lson);
	if (qr > m)
		sumr += queries(ql, qr, rson);
	return sumr;
}

int bs(int l, int r, int num){
	int t = l;
	while (l < r){
		int m = (l+r)>>1;
		if (queries(t, m, 1, n, 1) >= num)
			r = m;
		else
			l = m+1;
	}
	return l;
}

int main(){
	int T;
	scanf("%d", &T);
	while (T--){
		scanf("%d%d", &n, &q);
		build(1, n, 1);
		int op, a, b;
		while (q--){
			scanf("%d%d%d", &op, &a, &b);
			if (op == 1){
				int t = queries(a+1, n, 1, n, 1);
				//printf("%d\n", t);
				if (t == 0)
					printf("Can not put any one.\n");
				else{
					int l = bs(a+1, n, 1);
					if (b > t)
						b = t;
					int r = bs(a+1, n, b);
					update(l, r, 1, 1, n, 1);
					printf("%d %d\n", l-1, r-1);
				}
			}
			else{
				//for (int i=a+1; i<=b+1; ++i)
					//printf("%d\n", queries(i, i, 1, n, 1));
				printf("%d\n", b-a+1-queries(a+1, b+1, 1, n, 1));
				update(a+1, b+1, 0, 1, n, 1);
			}
		}
		printf("\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值