【线段树区更新+二分】HDU - 4614 L - Vases and Flowers

L - Vases and Flowers HDU - 4614  

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

3 7
2
1 9
4
Can not put any one.

2 6
2
0 9
4
4 5
2 3


然而我并没有用二分,以后有时间再补二分的写法

有n个花瓶,一开始全空,然后有m次操作,如果第一个输入的为1,然后输入两个数x,y
就是将x,y区间内插花,如果有花了就跳掉往后插,结束的条件是插到末尾了或者插够了数量
如果一个都不能插就说不不能插,否则就把插的头和尾输出。
如果输入2,输入x,y就是将x,y内的花清空,输出清空了多少
lazy=1,要么全空,要么全满


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=5e4+10;
int QL,QR,L,R,n,m,x,ans;

struct node
{
    int sum,lazy;
}tree[4*maxn];

void build(int l,int r,int rt)
{
    tree[rt].sum=0,tree[rt].lazy=1;
    if(l==r) return;
    int mid=(l+r)/2;
    build(l,mid,2*rt);
    build(mid+1,r,2*rt+1);
}

void pushdown(int l,int r,int rt)
{
    int mid=(l+r)/2;
    tree[2*rt].lazy=tree[2*rt+1].lazy=1;
    if(tree[rt].sum==r-l+1)
    {
        tree[2*rt].sum=mid-l+1;
        tree[2*rt+1].sum=r-mid;
    }
    else tree[2*rt].sum=tree[2*rt+1].sum=0;
}

void put_flower(int l,int r,int rt)
{
    if(ans<=0) return;
    if(L<=l&&R>=r&&tree[rt].lazy)
    {
        if(!tree[rt].sum)
        {
            ans-=(r-l+1),tree[rt].sum=r-l+1;
            if(QL<0) QL=l-1;
            QR=r-1;
        }
        else
        {
            R+=(r-l+1);
            if(R>n) R=n;
        }
        return;
    }
    int mid=(l+r)/2;
    if(tree[rt].lazy) pushdown(l,r,rt);
    tree[rt].lazy=0;
    if(L<=mid) put_flower(l,mid,2*rt);
    if(R>mid)  put_flower(mid+1,r,2*rt+1);
    tree[rt].sum=tree[rt*2].sum+tree[rt*2+1].sum;
    if(tree[rt].sum==r-l+1||!tree[rt].sum) tree[rt].lazy=1; //全满或全空,lazy标0
}

void clearing(int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        ans+=tree[rt].sum;
        tree[rt].lazy=1;
        tree[rt].sum=0;
        return;
    }
    if(tree[rt].lazy) pushdown(l,r,rt);
    tree[rt].lazy=0;
    int mid=(l+r)/2;
    if(L<=mid) clearing(l,mid,2*rt);
    if(R>mid) clearing(mid+1,r,2*rt+1);
    tree[rt].sum=tree[rt*2].sum+tree[2*rt+1].sum;
    if(tree[rt].sum==r-l+1||!tree[rt].sum) tree[rt].lazy=1;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        build(1,n,1);
        while(m--)
        {
            scanf("%d%d",&x,&L);
            L++;
            if(x==1)
            {
                scanf("%d",&ans);
                R=L+ans-1,QL=QR=-1;
                put_flower(1,n,1);
                if(QR>=0) printf("%d %d\n",QL,QR);
                else printf("Can not put any one.\n");
            }
            else
            {
                scanf("%d",&R);
                R++,ans=0;
                clearing(1,n,1);
                printf("%d\n",ans);
            }
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41037114/article/details/81319654
今日推荐