#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): 5892    Accepted Submission(s): 2492


 

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

题目大意 : 有N个空花瓶, 两种操作, 第一个操作是从U开始, 往后到N插M朵花, 多了扔掉,并输出第一次插花和最后一次插花的花瓶下标,插不了特判, 第二个操作是从U到V将所有已经插进去的花扔掉, 并输出扔了多少朵花

思路 : 这道题并不难, 主要是需要对答案进行二分。我们存的是所有区间的空花瓶数量, 第二个操作直接输出该区间有多少个空花瓶, 然后用区间长度直接减掉, 再更新就可以了;第一个操作要输出起始位置和结束位置, 由于你不知道从哪开始(U位置可能有花可能没有),也不知道什么时候插完,所以需要二分一下,先判断该区间是否有空花瓶,没有输出那行英文,有的话,开始二分起始位置, 起始位置的特点就是该位置左边的空花瓶数为1, 所以从U到N, 如果U到mid的空花瓶数大于0, 记录答案并往左, 否则往右(都是有花的瓶子), 终止位置的时候先对输入的插花数量和你实际的空花瓶数量, 这两个取一个最小值, 然后从U到N开始二分, 如果从U到mid的空花瓶数量 ≥ 你要插花的数量, 记录此时的mid并向右二分, 否则向左,注意每次查询完都要更新

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & (-x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

struct node
{
	int l, r;
	int z_sum, lzy; // 空花瓶数量, 懒人标记, 0表示都是空花瓶,1表示都有花
}t[MAXN * 4];
int n, m, T; 
void build(int rt, int l, int r) {
	t[rt].l = l, t[rt].r = r;
	t[rt].lzy = 0;
	if (l == r) { t[rt].z_sum = 1; return; } // 初始空花瓶数量都是1
	int mid = (l + r) >> 1;
	build(ls, l, mid);
	build(rs, mid + 1, r);
	t[rt].z_sum = t[ls].z_sum + t[rs].z_sum;
}
void Pushdown(int rt) {
	t[ls].lzy = t[rs].lzy = t[rt].lzy;
	if (t[rt].lzy == 1) t[ls].z_sum = t[rs].z_sum = 0;
	else {
		t[ls].z_sum = t[ls].r - t[ls].l + 1;
		t[rs].z_sum = t[rs].r - t[rs].l + 1;
	}
	t[rt].lzy = -1;  // 懒人传递
}
int Query(int rt, int l, int r) {
	if (t[rt].l >= l && t[rt].r <= r) return t[rt].z_sum; // 在区间直接返回
	else if (t[rt].r < l || t[rt].l > r) return 0;  // 出区间返回0
	if (t[rt].lzy != -1) Pushdown(rt);  // 
	int ui = Query(ls, l, r);
	int vi = Query(rs, l, r);
	return ui + vi; 
}
void Update(int rt, int l, int r, int x) {
	if (t[rt].l >= l && t[rt].r <= r) {
		if (x) t[rt].z_sum = 0;
		else t[rt].z_sum = t[rt].r - t[rt].l + 1;  // 在区间内更新
		t[rt].lzy = x;
		return;
	}
	else if (t[rt].r < l || t[rt].l > r) return;  // 出区间退出
	int mid = (t[rt].l + t[rt].r) >> 1;
	if (t[rt].lzy != -1) Pushdown(rt);
	if (mid >= r) Update(ls, l, r, x);   
	else if (mid < l) Update(rs, l, r, x);
	else {
		Update(ls, l, mid, x);
		Update(rs, mid + 1, r, x);
	}
	t[rt].z_sum = t[ls].z_sum + t[rs].z_sum;
	if (t[rt].z_sum == 0) t[rt].lzy = 1;
	else if (t[rt].z_sum == t[rt].r - t[rt].l + 1) t[rt].lzy = 0;
	else t[rt].lzy = -1;
}
int find_f(int ui) {   // 查询第一个位置
	int l = ui, r = n, stot = INF;
	while (l <= r) {
		int mid = (l + r) >> 1;
		if (Query(1, ui, mid) > 0) Min(stot, mid), r = mid - 1;
		else l = mid + 1;
	}
	return stot;
}
int find_s(int ui, int x) {   //查询最后一个位置
	int l = ui, r = n, scnt = n;
	while (l <= r) {
		int mid = (l + r) >> 1;
		if (Query(1, ui, mid) >= x) Min(scnt, mid), r = mid - 1;
		else l = mid + 1;
	}
	return scnt;
}

int main()
{
	cin >> T;
	while (T--) {
		sc("%d %d", &n, &m); 
		build(1, 1, n); 
		for (int i = 0; i < m; i++) {
			int op, ui, vi;
			sc("%d %d %d", &op, &ui, &vi);
			if (op == 1) {
				ui++;
				int ans = Query(1, ui, n);
				if (!ans) { cout << "Can not put any one." << endl; continue; }
				int stot = find_f(ui);
				int scnt = find_s(ui, min(ans, vi));
				cout << stot - 1 << " " << scnt - 1 << endl;
				Update(1, stot, scnt, 1);  // 更新
			}
			else {
				ui++, vi++;
				int ans = Query(1, ui, vi);
				int left = vi - ui + 1 - ans; // 有多少个花
				cout << left << endl;
				Update(1, ui, vi, 0);
			}
		}
		cout << endl;
	}
	return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值