Vases and Flowers - HDU 4614 - 线段树

140 篇文章 0 订阅

Vases and Flowers - HDU 4614 - 线段树

题目描述:

  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

题意:

  每一组的第一行有两个数n,m, n表示花瓶数量0~n-1,开始全为空,接下来有m组,每组有数k,a,b;当k=1时, 从a位置开始插花,b为花数量,当花瓶有花时,跳过当前,直到找到空花瓶再插入,输出插入第一支花的位置和最后一支花的位置,花可以没插完,当一支都没有插入则输出Can not put any one.;当k=2时,清空[a,b]的花瓶,并输出在范围内花的数量。

思路:

  不是特别难的一道线段树,主要问题就是在插花的过程中求左右端点的问题。在这里只要记录下第一次插花的左端点和在花插完之后的右端点就可以。

AC代码:

//
//  main.cpp
//  L
//
//  Created by LucienShui on 2017/6/2.
//  Copyright © 2017年 LucienShui. All rights reserved.
//

#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#define memset(a,b,n) memset(a,b,n*sizeof(a[0]))
#define il inline
#define ll long long
#define ull unsigned long long

using namespace std;

#define ls (u<<1)
#define rs (u<<1|1)
#define lson u<<1,l,mid
#define rson u<<1|1,mid+1,r
#define maxn 50007

int sum[maxn<<2],ans;
bool same[maxn<<2];
//sum为范围内花的数量,same为当前区间是否全为空或全有花

void build(int u, int l, int r) {
    sum[u]=0,same[u]=true;
    if(l==r) return;
    int mid = (l+r)>>1;
    build(lson);
    build(rson);
}

void pushdown(int u, int l, int r) {
    same[ls] = same[rs]= true;
    int mid = (l+r)>>1;
    if(sum[u] == r-l+1) sum[ls] = mid-l+1,sum[rs] = r-mid;
    else sum[ls] = sum[rs] = 0;
    same[u]=false;
}

int ql,qr,b,e,n;
void putinfolwer(int u, int l, int r) {
    if(ans<=0) return ;
    int mid = (l+r)>>1;
    if(b <= l && r <= e && same[u]) {
        if(sum[u]==0) {
            ans -= r-l+1;
            sum[u] = r-l+1;
            if(ql<0) ql = l-1;
            qr = r-1;
        }
        else {//跳动插花范围的右边值,e(end)刚好是插完花的右边范围的最小值,如果超出花瓶数量,则为n
            e += r-l+1;
            if(e>n) e=n;
        }
        return ;
    }
    if(same[u]) pushdown(u, l, r);
    if(b<=mid) putinfolwer(lson);
    if(e>mid) putinfolwer(rson);
    sum[u] = sum[ls] + sum[rs];
    if(sum[u] == r-l+1 || sum[u]==0) same[u] = true;
}

void clear(int u, int l, int r) {
    int mid = (l+r)>>1;
    if(b <= l && r <= e) {
        ans += sum[u];
        same[u] = true;
        sum[u] = 0;
        return ;
    }
    if(same[u]) pushdown(u,l,r);
    if(b<=mid) clear(lson);
    if(e>mid) clear(rson);
    sum[u] = sum[ls] + sum[rs];
    if(sum[u]==r-l+1||sum[u]==0) same[u]=true;
}

int main ()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
    int t,m,x;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%d",&n,&m);
        build(1,1,n);
        while(m--) {
            scanf("%d%d",&x,&b);
            b++;
            if(x==1) {
                scanf("%d",&ans);
                e = b+ans-1;
                ql = qr = -1;
                putinfolwer(1,1,n);
                if(qr>=0) printf("%d %d\n",ql,qr);
                else printf("Can not put any one.\n");
            }
            else {
                scanf("%d",&e);
                e++;
                ans = 0;
                clear(1,1,n);
                printf("%d\n",ans);
            }
        }
        putchar('\n');
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值