HDU4614 Vases and Flowers(线段树+二分)

该博客讨论了如何使用线段树和二分查找技术来解决HDU4614题目的花瓶与花朵操作问题。内容涉及在遇到已有花朵的花瓶时如何插入新花朵,以及清空特定范围花瓶的方法,并强调了二分查找在确定查询区间中的应用。

给出一些花瓶,有两种操作,1.从x位置向后插入y朵花,遇见已经有的就跳过,直到最后一个花瓶为止,输出第一和最后一个花瓶的位置,2.将x和y之间的花瓶清空输出花数。

线段树的写法差不多一致,关键是用二分来确定每次查询的左右端点。

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int N=500005;
struct node
{
    int val;
    int mark;
}tree[N*4];
void pushdown(int i,int m)
{
    if(tree[i].mark!=-1)
    {
        tree[i*2+1].mark=tree[i*2].mark=tree[i].mark;
        tree[i*2+1].val=(m>>1)*tree[i].mark;
        tree[i*2].val=(m-(m>>1))*tree[i].mark;

        tree[i].mark=-1;
    }
}
void build(int i,int l,int r)
{
    tree[i].mark=-1;//标记-1 0也是需要传输的标记
    tree[i].val=0;
    if(l==r)
        return ;
    int mid=(l+r)/2;
    build(i*2,l,mid);
    build(i*2+1,mid+1,r);
}
void updata(int l,int r,int L,int R,int i,int num)
{
    if(l>=L&&r<=R)
    {
        tree[i].mark=num;
        tree[i].val=(r-l+1)*tree[i].mark;
        return ;
    }
    pushdown(i,r-l+1);
    int mid=(l+r)/2;
    if(L<=mid)
        updata(l,mid,L,R,i*2,num);
    if(R>mid)
        updata(mid+1,r,L,R,i*2+1,num);
    tree[i].val=tree[i*2].val+tree[i*2+1].val;
}
int query(int l,int r,int L,int R,int i)
{
    if(l>=L&&r<=R)
        return tree[i].val;
    int mid=(l+r)/2;
    int ans=0;
    pushdown(i,r-l+1);
    if(L<=mid)
        ans+=query(l,mid,L,R,i*2);
    if(R>mid)
        ans+=query(mid+1,r,L,R,i*2+1);
    return ans;
}

int sera(int k,int n,int val)//分别二分左右区间 查询时的左区间不动
{
    int l=k;
    int r=n;
    int ans=-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        int tmp=query(1,n,k,mid,1);
        if(tmp+val==mid-k+1)
        {
            ans=mid;
            r=mid-1;
        }
        else
        {
            if(tmp+val<mid-k+1)
                r=mid-1;
            else
                l=mid+1;
        }
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,q;
        scanf("%d %d",&n,&q);
        build(1,1,n);
        while(q--)
        {
            int sym,l,x;
            scanf("%d %d %d",&sym,&l,&x);
            if(sym==1)
            {
                l++;
                int left=sera(l,n,1);//满足到l点容纳量为1的右端点是答案的左区间
                int right;
                if(left==-1)
                    printf("Can not put any one.\n");
                else
                {
                    int tmp=query(1,n,left,n,1);//left到末尾的已经插入数量
                    int tmp1=n-left+1-tmp;//剩余的值
                    right=sera(left,n,min(x,tmp1));//剩余值和查询值的最小值是二分所需要确定的区间长度
                    printf("%d %d\n",left-1,right-1);
                    updata(1,n,left,right,1,1);
                }
            }
            if(sym==2)
            {
                l++;
                x++;
                printf("%d\n",query(1,n,l,x,1));
                updata(1,n,l,x,1,0);
            }
        }
        printf("\n");
    }
    return 0;
}

Vases and Flowers

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


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]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值