Vases and Flowers(线段树+二分+区间修改区间查询)

Vases and Flowers

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 21   Accepted Submission(s) : 11
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.   [b]Output one blank line after each test case.[/b]
 

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]
 

Author
SYSU
 

Source
2013 Multi-University Training Contest 2
 

Statistic |  Submit |  Back

题意:

就是有n个花瓶,一开始全空,然后又m次操作,如果第一个输入的为1,然后输入两个数x,y,就是将x,y区间内插花,如果有花了就跳掉往后插,结束的条件是插到末尾了或者插够了数量,如果一个都不能插就输出相应内容,如果输入2,输入x,y就是将x,y内的花清空


思路:

二分用于确定区间,找到合适的位置插入,然后就是普通lazy的区间更新。


代码:

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=50010;
#define L(i) (i<<1)
#define R(i) (i<<1|1)
struct node
{
    int le,ri,sum;
    
    int mid()
    {
        return (le+ri)>>1;
    }
    
} tree[maxn<<2];
int change[maxn<<2];

void Build(int rt,int le,int ri)
{
    tree[rt].le=le,tree[rt].ri=ri;
    if(le==ri)
    {
        tree[rt].sum=1;
        return ;
    }
    int mid=tree[rt].mid();
    Build(L(rt),le,mid);
    Build(R(rt),mid+1,ri);
    tree[rt].sum=tree[L(rt)].sum+tree[R(rt)].sum;
}
void Pushdown(int rt)   //lazy
{
    change[L(rt)]=change[rt];
    change[R(rt)]=change[rt];
    tree[L(rt)].sum=change[rt]*(tree[L(rt)].ri-tree[L(rt)].le+1);
    tree[R(rt)].sum=change[rt]*(tree[R(rt)].ri-tree[R(rt)].le+1);
    change[rt]=-1;
}

void Update(int rt,int le,int ri,int num)
{
    if(le<=tree[rt].le&&tree[rt].ri<=ri)
    {
        change[rt]=num;
        tree[rt].sum=num*(tree[rt].ri-tree[rt].le+1);
        return ;
    }
    if(change[rt]!=-1)
        Pushdown(rt);
    int mid=tree[rt].mid();
    if(le<=mid)
        Update(L(rt),le,ri,num);
    if(ri>mid)
        Update(R(rt),le,ri,num);
    tree[rt].sum=tree[L(rt)].sum+tree[R(rt)].sum;
}

int Query(int rt,int le,int ri)
{
    if(le<=tree[rt].le&&ri>=tree[rt].ri)
        return tree[rt].sum;
    if(change[rt]!=-1)
        Pushdown(rt);
    int mid=tree[rt].mid();
    if(le>mid)
        return Query(R(rt),le,ri);
    else if(ri<=mid)
        return Query(L(rt),le,ri);
    else
        return Query(L(rt),le,mid)+Query(R(rt),mid+1,ri);
}

int main()
{
    int t,n,m;
    scanf("%d",&t);
    while(t--)
    {
        memset(change,-1,sizeof(change));
        scanf("%d%d",&n,&m);
        Build(1,1,n);
        int op,x,y;
        while(m--)
        {
            scanf("%d%d%d",&op,&x,&y);
            if(op==1)
            {
                ++x;
                int l=x,r=n,st=-1,ed;
                while(l<=r)
                {
                    int mid=(l+r)>>1;
                    if(Query(1,x,mid)>=1)
                    {
                        st=mid;
                        r=mid-1;
                    }
                    else
                        l=mid+1;
                }
                if(st==-1)  //一朵也不能插
                {
                    printf("Can not put any one.\n");
                    continue;
                }
                if(Query(1,st,n)<y)//不能够插够y朵花
                {
                    l=st,r=n;
                    while(l<=r)
                    {
                        int mid=(l+r)>>1;
                        if(Query(1,mid,n)>=1)
                        {
                            ed=mid;
                            l=mid+1;
                        }
                        else
                            r=mid-1;
                    }
                }
                else//能插够y朵花
                {
                    l=st,r=n,ed=n;
                    while(l<=r)
                    {
                        int mid=(l+r)>>1;
                        if(Query(1,st,mid)>=y)
                        {
                            ed=mid;
                            r=mid-1;
                        }
                        else
                            l=mid+1;
                    }
                }
                printf("%d %d\n",st-1,ed-1);
                Update(1,st,ed,0);
            }
            else
            {
                ++x,++y;
                printf("%d\n",y-x+1-Query(1,x,y));
                Update(1,x,y,1);
            }
        }
        printf("\n");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值