L - Vases and Flowers HDU - 4614(线段树)

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

解题思路:

线段树的区间更新与查询并不难,这道题的难处在于如何找到插花的起始点与结束点。可以用二分来查找,至于为什么能用二分,我先解释一下,首先时间给的较多,其次,随着花瓶标号s的逐渐上升,s花瓶之前的空花瓶是呈非递减的,所以符合使用二分的条件(序列是有序的)。具体如何二分请看代码的表演。。。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn = 500020;
int Tree[maxn << 2], lazy[maxn << 2];
int t, n, m, k;

void Build(int l, int r, int rt)
{
	if(l == r)
	{
		Tree[rt] = 1;
		return ;
	}
	int m = (l + r) / 2;
	Build(l, m, rt << 1);
	Build(m + 1, r, rt << 1 | 1);
	Tree[rt] = Tree[rt << 1] + Tree[rt << 1 | 1];
}

void pushdown(int l, int r, int rt)
{
    if(lazy[rt] == -1)
        return ;
    int m = (l + r) / 2;
    lazy[rt << 1] = lazy[rt << 1 | 1] = lazy[rt];
    Tree[rt << 1] = lazy[rt] * (m - l + 1);
    Tree[rt << 1 | 1] = lazy[rt] * (r - m);
    lazy[rt] = -1;
}

void update(int L, int R, int val, int l, int r, int rt)
{
    if(l >= L && r <= R)
    {
        Tree[rt] = val * (r - l + 1);
        lazy[rt] = val;
        return ;
    }
    pushdown(l, r, rt);
    int m = (l + r) / 2;
    if(m >= L)
        update(L, R, val, l, m, rt << 1);
    if(m < R)
        update(L, R, val, m + 1, r, rt << 1 | 1);
    Tree[rt] = Tree[rt << 1] + Tree[rt << 1 | 1];
}

int query(int L, int R, int l, int r, int rt)
{
    if(l >= L && r <= R)
        return Tree[rt];
    pushdown(l, r, rt);
    int m = (l + r) / 2;
    int ans = 0;
    if(m >= L)
        ans += query(L, R, l, m, rt << 1);
    if(m < R)
        ans += query(L, R, m + 1, r, rt << 1 | 1);
    return ans;
}

int Bin(int s,int num)
{
	int l = s;
	int r = n;
	int ans = -1;
	while(l <= r)
	{
		int m = (l + r) / 2;
		int temp = query(s, m, 1, n, 1);
		if(temp == num)
		{
			ans = m;
			r = m - 1;
		}
		else
		{
			if(temp < num)
				l = m + 1;
			else
				r = m - 1;
		}
	}
	return ans;
}

int main()
{
    //freopen("in.txt", "r", stdin);
	cin >> t;
	while(t--)
	{
		scanf("%d%d", &n, &m);
		Build(1, n, 1);
		memset(lazy, -1, sizeof(lazy));
		int a, b;
		for(int i = 1; i <= m; ++ i)
		{
			scanf("%d%d%d", &k, &a, &b);
			if(k == 1)
			{
				a++;
				int st = Bin(a, 1);
				if(st == -1)
				{
					cout << "Can not put any one." << endl;
				}
				else
				{
					int temp = query(st, n, 1, n, 1);
					if(temp <= b)
						b = temp;
					int ed = Bin(st, b);
					update(st, ed, 0, 1, n, 1);
					printf("%d %d\n", st - 1, ed - 1);
				}
			}
			else
			{
				a ++;
				b ++;
				int ans = query(a, b, 1, n, 1);
				update(a, b, 1, 1, n, 1);
				printf("%d\n", b - a + 1 - ans);
			}
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/aqa2037299560/article/details/82872960
今日推荐