Vases and Flowers(hdu4614,线段树+二分查找)

http://acm.hdu.edu.cn/showproblem.php?pid=4614

Vases and Flowers

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 685    Accepted Submission(s): 226

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

思路+线段树+二分查找;

参考自:http://blog.csdn.net/juststeps/article/details/9526367

*/

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 50005;
struct node
{
    int l,r;
    int sum;
    int flag;//这里是作为该花瓶是否已经插了花的标志
    int mid()
    {
        return (l+r)>>1;
    }
};
node tree[MAXN*4];
inline void pushup(int pos)//维护结点信息
{
    tree[pos].sum=tree[pos<<1].sum+tree[pos<<1|1].sum;
}
inline void pushdown(int pos)//传递标记
{
    if(tree[pos].flag==1)
    {
        tree[pos<<1].flag=1;
        tree[pos<<1|1].flag=1;
        tree[pos<<1].sum=0;
        tree[pos<<1|1].sum=0;
        tree[pos].flag=-1;
    }
    else if(tree[pos].flag==0)
    {
        tree[pos<<1].flag=0;
        tree[pos<<1|1].flag=0;
        tree[pos<<1].sum=tree[pos<<1].r-tree[pos<<1].l+1;
        tree[pos<<1|1].sum=tree[pos<<1|1].r-tree[pos<<1|1].l+1;
        tree[pos].flag=-1;
    }
}
void build(int l,int r,int pos)//建树
{
    tree[pos].l=l;
    tree[pos].r=r;
    if(l==r)
    {
        tree[pos].sum=1;
        tree[pos].flag=-1;
        return ;
    }
    int mid=tree[pos].mid();
    build(l,mid,pos<<1);
    build(mid+1,r,pos<<1|1);
    pushup(pos);//维护节点
    tree[pos].flag=-1;//标志更新
}
int query(int l,int r,int pos)
{
    if(tree[pos].flag==1)//如果当前区间的花瓶没有花
        return 0;
    else if(tree[pos].flag==0)//当前区间的有花
        return r-l+1;
    if(l==tree[pos].l&&tree[pos].r==r)//查询到边界直接输出
    {
        return tree[pos].sum;
    }
    int mid=tree[pos].mid();
    if(r<=mid)//否则访问左右节点
        return query(l,r,pos<<1);
    else if(l>mid)
        return query(l,r,pos<<1|1);
    else
        return query(l,mid,pos<<1)+query(mid+1,r,pos<<1|1);
}
void update(int l,int r,int x,int pos)
{
    if(tree[pos].l==l&&tree[pos].r==r)//更新
    {
        tree[pos].flag=x;//插花
        if(x==0)
            tree[pos].sum=r-l+1;
        else
            tree[pos].sum=0;
        return ;
    }
    pushdown(pos);//传递标记
    int mid=tree[pos].mid();
    if(r<=mid)
        update(l,r,x,pos<<1);
    else if(l>mid)
        update(l,r,x,pos<<1|1);
    else
    {
        update(l,mid,x,pos<<1);
        update(mid+1,r,x,pos<<1|1);
    }
    pushup(pos);//维护
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        build(1,n,1);
        int i;
        for(i=0;i<m;i++)
        {
            int k,a,b;
            scanf("%d%d%d",&k,&a,&b);
            a++;
            if(k==2)
                b++;
            if(k==2)
            {
                int x=query(a,b,1);//当前空余的区间值
                update(a,b,0,1);//更新
                printf("%d\n",b-a+1-x);//将总的区间值减去空闲区间即为所求花的数量之和
            }
            else
            {
                int l=a,r=n;
                int x=query(l,r,1);//寻找空余空间
                //printf("asdf %d\n",x);
                if(x==0)//如果没有你空余空间则说明不能插入花了
                {
                    printf("Can not put any one.\n");
                    continue;
                }
                int m1,m2;
                while(l<=r)//找起点,否则利用二分法查找插花的起点和终点
                {
                    int mid=(l+r)>>1;
                    x=query(a,mid,1);//一般空间
                    if(x>0)//如果存在空余区间
                    {
                        m1=mid;
                        r=mid-1;//往左移动
                    }
                    else
                        l=mid+1;//右移动
                }
                l=a;r=n;
                x=query(l,r,1);//访问区间
                m2=n;
                if(x<b)//b表示需要插花的数量
                    b=x;
                while(l<=r)
                {
                    int mid=(l+r)>>1;
                    x=query(a,mid,1);
                    if(x>=b)//当空间比插花的要多的话
                    {
                        m2=mid;
                        r=mid-1;//往左移动
                    }
                    else
                        l=mid+1;
                }
                update(m1,m2,1,1);//在m1至m2过程进行更新
                printf("%d %d\n",m1-1,m2-1);//输出起点和终点
            }
        }
        printf("\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值