【线段树区间更新+二分】 L - Vases and Flowers HDU - 4614

Vases and Flowers

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


 

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

2013 Multi-University Training Contest 2

 

 

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

const int mn = 50010;

struct node
{
	int l, r;
	int b, lazy; /// b存储区间内空位
} t[8 * mn];

void build(int i, int l, int r)
{
	t[i].l = l, t[i].r = r;
	t[i].b = r - l + 1;
	t[i].lazy = -1;
	if (l == r)
        return;
	int m = (l + r) / 2;
	build(2 * i, l, m);
	build(2 * i + 1, m + 1, r);
	return;
}
void pushdown(int i, int l, int r, int m)
{
    t[2 * i].lazy = t[2 * i + 1].lazy = t[i].lazy;
    t[2 * i].b = (m - l + 1) * t[i].lazy;
    t[2 * i + 1].b = (r - m) * t[i].lazy;
    t[i].lazy = -1;
    return;
}
void update(int i, int a, int b, int f)
{
    int l = t[i].l, r = t[i].r;
    int m = (l + r) / 2;
    if (a == l && r == b)
    {
        t[i].b = f * (r - l + 1);
        t[i].lazy = f;
        return;
    }
    if (t[i].lazy != -1)
        pushdown(i, l, r, m);
    if (b <= m)
        update(2 * i, a, b, f);
    else if (a > m)
        update(2 * i + 1, a, b, f);
    else
    {
        update(2 * i, a, m, f);
        update(2 * i + 1, m + 1, b, f);
    }
    t[i].b = t[2 * i].b + t[2 * i + 1].b;
    return;
}
int query(int i, int a, int b)
{
	int l = t[i].l, r = t[i].r;
	int m = (l + r) / 2;
	if (a == l && r == b)
		return t[i].b;
	if (t[i].lazy != -1)
        pushdown(i, l, r, m);
	if (b <= m)
		return query(2 * i, a, b);
	else if (a > m)
		return query(2 * i + 1, a, b);
	else
		return query(2 * i, a, m) + query(2 * i + 1, m + 1, b);
}
int findleft(int a, int b)
{
    int ans;
    int l = a, r = b;
    while (l <= r)
    {
        int m = (l + r) / 2;
        if (query(1, l, m) >= 1)
        {
            ans = m;
            r = m - 1;
        }
        else
            l = m + 1;
    }
    return ans;
}
int findright(int a, int b, int sum)
{
    int ans;
    int l = a, r = b;
    while (l <= r)
    {
        int m = (l + r) / 2;
        if (query(1, a, m) >= sum)   /// 从a到m的空格多于所需
        {
            ans = m;
            r = m - 1;
        }
        else
            l = m + 1;
    }
    return ans;
}

int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		int n, m;
		scanf("%d %d", &n, &m);
		build(1, 0, n - 1);
		for (int i = 1; i <= m; i++)
		{
			int x, y, z;
			scanf("%d %d %d", &x, &y, &z);
			if (x == 1)
			{
			    int sum = min(z, query(1, y, n - 1));
			    if (sum == 0)
                {
                    printf("Can not put any one.\n");
                    continue;
                }
                int left = findleft(y, n - 1);  /// 二分确定左右端点
                int right = findright(y, n - 1, sum);
                printf("%d %d\n", left, right);
                update(1, left, right, 0);
			}
			else if (x == 2)
			{
				printf("%d\n", z - y + 1 - query(1, y, z));
				update(1, y, z, 1);
			}
		}
		printf("\n");
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值