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): 1336    Accepted Submission(s): 532


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]
 


Source

2013 Multi-University Training Contest 2

题意:n个花瓶从0到n-1,刚开始都是空的,有两种操作,第一种操作是从A位置开始放放F只花,每个花瓶只能放一束花,如果花瓶内已经有花则跳过,直到F只花放完或者放到n-1只花瓶时结束,输出开始放花的位置和结束放花的位置,如果从A开始没有空花瓶,输出一句话,第二种操作是,输出区间【A,B】的有花的花瓶数,并把有花的花瓶清空。

思路:线段树题,第二种操作很好处理,区间查询之后再区间更新,把区间置为0,使用了lazy,第一种操作比较麻烦,如果我们直到了开始放花的位置和结束放花的位置,直接区间更新,全置为1就行了,如何得到这两个位置呢,方法是二分,两次二分,首先二分起始位置,然后在二分结束位置。

#include <stdio.h>
#include <algorithm>

using namespace std;

#define lson num << 1
#define rson num << 1 | 1
#define maxn 50010
#define mod 10007

typedef long long ll;

struct node
{
    int l,r;
    int sum;
    int lazy;
}segTree[maxn << 2];
int ans;
void pushup(int num)
{
    segTree[num].sum = segTree[lson].sum + segTree[rson].sum;
}
void build(int num,int l,int r)
{
    segTree[num].l = l;
    segTree[num].r = r;
    segTree[num].lazy = -1;
    if(l == r){
        segTree[num].sum = 0;
        return;
    }
    int mid = (segTree[num].l + segTree[num].r) >> 1;
    build(lson,l,mid);
    build(rson,mid + 1,r);
    pushup(num);
}
void pushdown(int num)
{
    if(segTree[num].lazy != -1){
        segTree[lson].lazy = segTree[rson].lazy = segTree[num].lazy;
        segTree[lson].sum = (segTree[lson].r - segTree[lson].l + 1) * segTree[num].lazy;
        segTree[rson].sum = (segTree[rson].r - segTree[rson].l + 1) * segTree[num].lazy;
        segTree[num].lazy = -1;
    }
}
void update(int num,int l,int r,int v)
{
    if(segTree[num].l == l && segTree[num].r == r){
        segTree[num].sum = (r - l + 1) * v;
        segTree[num].lazy = v;
        return;
    }
    pushdown(num);
    int mid = (segTree[num].l + segTree[num].r) >> 1;
    if(r <= mid){
        update(lson,l,r,v);
    }
    else if(l > mid){
        update(rson,l,r,v);
    }
    else{
        update(lson,l,mid,v);
        update(rson,mid + 1,r,v);
    }
    pushup(num);
}
int query(int num,int l,int r)
{
    if(segTree[num].l == l && segTree[num].r == r){
        return segTree[num].sum;
    }
    pushdown(num);
    int mid = (segTree[num].l + segTree[num].r) >> 1;
    int ans = 0;
    if(r <= mid){
        ans += query(lson,l,r);
    }
    else if(l > mid){
        ans += query(rson,l,r);
    }
    else{
        ans += query(lson,l,mid);
        ans += query(rson,mid + 1,r);
    }
    pushup(num);
    return ans;
}
int main(void)
{
    int T;
    scanf("%d",&T);
    while(T--){
        int n,m;
        scanf("%d%d",&n,&m);
        build(1,1,n);
        while(m--){
            int op;
            int l,r;
            int p,num;
            scanf("%d",&op);
            if(op == 1){
                scanf("%d%d",&p,&num);
                if(query(1,p + 1,n) == (n - (p + 1) + 1)){
                    printf("Can not put any one.\n");
                }
                else{
                    int L = p + 1,R = n;
                    int mid;
                    int pos1 = n;
                    while(L <= R){
                        int mid = (L + R) >> 1;
                        if(mid - (p + 1) + 1 > query(1,p + 1,mid)){
                            pos1 = min(pos1,mid);
                            R = mid - 1;
                        }
                        else{
                            L = mid + 1;
                        }
                    }
                    int temp = n - pos1 + 1 - query(1,pos1,n);
                    int pos2 = n;
                    if(num >= temp){
                        num = temp;
                    }
                    L = pos1,R = n;
                    while(L <= R){
                        int mid = (L + R) >> 1;
                        if(mid - pos1 + 1 - query(1,pos1,mid) >= num){
                            pos2 = min(pos2,mid);
                            R = mid - 1;
                        }
                        else{
                            L = mid + 1;
                        }
                    }
                    printf("%d %d\n",pos1 - 1,pos2 - 1);
                    update(1,pos1,pos2,1);
                }

            }
            else{
                scanf("%d%d",&l,&r);
                printf("%d\n",query(1,l + 1,r + 1));
                update(1,l + 1,r + 1,0);
            }
        }
        printf("\n");
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值