HDU - 4616 Vases and Flowers(线段树 -区间更新 + 二分)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614

题意:给你你个花瓶,开始时n个花瓶都是空的,且从0 -> n-1编号,m此操作,操作分两种。
(1)给一个起点a和花的数量f问从a开始知道把f朵花全部插入花瓶中或者到达最后一个,求这个区间的左边界和有边界。
(2)给一个区间x,y问这个区间内一共有多少朵花,并将这些花扔掉。

思路:我们假设花瓶里有花为1,无花为0,那么第二部操作就是线段树的区间查询操作。对于第一步,我们就是要找第一个区间和等于1的位置,以及第一个区间和等于m的位置,所以可以想到二分,判断条件中将左端点固定就好。

代码:

#include<set>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int INF=0x3f3f3f3f;
const int maxn = 5e4+7;

int sum[maxn<<2],lazy[maxn<<2];
int L[maxn<<2],R[maxn<<2];

void push_up(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void push_down(int rt)
{
    if(lazy[rt]!=-1)
    {
        lazy[rt<<1]=lazy[rt],lazy[rt<<1|1]=lazy[rt];
        sum[rt<<1]=lazy[rt]*(R[rt<<1]-L[rt<<1]+1);
        sum[rt<<1|1]=lazy[rt]*(R[rt<<1|1]-L[rt<<1|1]+1);
        lazy[rt]=-1;
    }
}

void build(int l,int r,int rt)
{
    R[rt]=r,L[rt]=l;
    lazy[rt]=-1;
    if(l==r)
    {
        sum[rt]=1;
        return ;
    }
    int m=(l+r)>>1;
    build(lson),build(rson);
    push_up(rt);
}

void update(int L1,int R1,int val,int l,int r,int rt)
{
    if(L1<=l&&r<=R1)
    {
        sum[rt]=val*(R[rt]-L[rt]+1);
        lazy[rt]=val;
        return ;
    }
    push_down(rt);
    int m=(l+r)>>1;
    if(L1<=m) update(L1,R1,val,lson);
    if(R1>m) update(L1,R1,val,rson);
    push_up(rt);
}

int query(int L1,int R1,int l,int r,int rt)
{
    if(L1<=l&&r<=R1) return sum[rt];
    push_down(rt);
    int ans=0;
    int m=(l+r)>>1;
    if(L1<=m) ans+=query(L1,R1,lson);
    if(R1>m) ans+=query(L1,R1,rson);
    return ans;
}

int n,m;

int Find(int l,int r,int val)
{
    int temp=l;
    while(l<r)
    {
        int mid=(l+r)>>1;
        if(query(temp,mid,1,n,1)>=val) r=mid;
        else l=mid+1;
    }
    return r;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        build(1,n,1);

        while(m--)
        {
            int opt;
            scanf("%d",&opt);
            if(opt==1)
            {
                int x,f;
                scanf("%d%d",&x,&f);
                x++;
                int res=query(x,n,1,n,1);
                if(res==0) printf("Can not put any one.\n");
                else
                {
                    int l=Find(x,n,1);
                    int r=Find(l,n,min(f,res));
                    update(l,r,0,1,n,1);
                    printf("%d %d\n",--l,--r);
                }
            }
            else
            {
                int x,y;
                scanf("%d%d",&x,&y);
                x++,y++;
                printf("%d\n",y-x+1-query(x,y,1,n,1));
                update(x,y,1,1,n,1);
            }
        }
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值