hdu4614(线段树+二分)

 

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]
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 50005;
int n,m;
struct node
{
    int sum,lazy;//lazy是标记数组。
}tree[N<<2];

void build(int num,int s,int e)
{
    tree[num].sum = 0;
    tree[num].lazy = -1;
    if(s == e)
        return;
    int mid = (s + e)>>1;
    build(num<<1,s,mid);
    build(num<<1|1,mid + 1,e);
}

void pushup(int num)
{
    tree[num].sum = tree[num<<1].sum + tree[num<<1|1].sum;
}

void pushdown(int num,int s,int e)
{
    tree[num<<1].lazy = tree[num<<1|1].lazy = tree[num].lazy;
    if(tree[num].lazy)
    {
        int mid = (s + e)>>1;
        tree[num<<1].sum = mid - s + 1;
        tree[num<<1|1].sum = e - mid;
    }
    else
        tree[num<<1].sum = tree[num<<1|1].sum = 0;
        tree[num].lazy = -1;
}

void update(int num,int s,int e,int l,int r,int val)
{
    if(s == l && r == e)
    {
        tree[num].lazy = val;
        if(val)
            tree[num].sum = e - s + 1;
        else
            tree[num].sum = 0;
        return;
    }
    if(tree[num].lazy > -1)
        pushdown(num,s,e);
    int mid = (s + e)>>1;
    if(r <= mid)
        update(num<<1,s,mid,l,r,val);
    else
    {
        if(l > mid)
            update(num<<1|1,mid + 1,e,l,r,val);
        else
        {
            update(num<<1,s,mid,l,mid,val);
            update(num<<1|1,mid + 1,e,mid + 1,r,val);
        }
    }
    pushup(num);
}

int query(int num,int s,int e,int l,int r)
{
    if(s == l && e == r)
        return tree[num].sum;
    if(tree[num].lazy > -1)
        pushdown(num,s,e);
    int mid = (s + e)>>1;
    if(r <= mid)
        return query(num<<1,s,mid,l,r);
    else
    {
        if(l > mid)
            return query(num<<1|1,mid + 1,e,l,r);
        else
            return query(num<<1,s,mid,l,mid) + query(num<<1|1,mid + 1,e,mid + 1,r);
    }
}

int bin(int s,int rank)
{
    int l = s;
    int r = n;
    int ans = -1;
    int mid;
    while(l <= r)
    {
        int mid = (l + r)>>1;
        int tmp = query(1,1,n,s,mid);
        if(tmp + rank == mid - s + 1)
        {
            ans = mid;
            r = mid - 1;
        }
        else
        {
            if(tmp + rank < mid - s + 1)
                r = mid - 1;
            else
                l = mid + 1;
        }
    }
    return ans;
}

int main()
{
    int i,t;
    int op,a,b;
    scanf("%d",&t);
    while(t --)
    {
        scanf("%d%d",&n,&m);
        build(1,1,n);
        while(m --)
        {
            scanf("%d%d%d",&op,&a,&b);
            if(op == 1)
            {
                int st,ed;
                a ++;
                st = bin(a,1);//从a开始能放的第一个位置。
                if(st == -1)
                    puts("Can not put any one.");
                else
                {
                    int tmp = query(1,1,n,st,n);
                    tmp = n - st + 1 - tmp;
                    if(tmp <= b)//剩余能放的瓶子,如果不足的话b=tmp(也就是放到最后其余的花扔了)。
                        b = tmp;
                    ed = bin(a,b);
                    printf("%d %d\n",st - 1,ed - 1);
                    update(1,1,n,st,ed,1);
                }
            }
            else
            {
                a ++;b ++;
                printf("%d\n",query(1,1,n,a,b));
                update(1,1,n,a,b,0);
            }
        }
        puts("");
    }
    return 0;
}
//890MS	1256K

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值