HDU 4614 Vases and Flowers (二分查找+线段树区间更新)

  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区间内插花,如果有花了就跳掉往后插,结束的条件是插到末尾了或者插够了数量,如果一个都不能插就骂人qwq,如果输入2,输入x,y就是将x,y内的花清空

这题思路就是普通的lazy tag区间更新+二分查找合适的位置插

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
struct node
{
    int l,r;
    int tag;//如果为-1则为初始状态,如果为0意思将子区间内的花清空,为1为插满花
    int v;//记录区间内插了多少花
}t[50005*4];
int n;
void Build(int l,int r,int k)
{
    t[k].l=l;
    t[k].r=r;
    t[k].v=0;
    t[k].tag=-1;
    if(l==r)
        return;
    int mid=(l+r)/2;
    Build(l,mid,k*2);
    Build(mid+1,r,k*2+1);
}
void pushdown(int k)//向下传递状态
{
    t[k*2].tag=t[k*2+1].tag=t[k].tag;
    t[k*2].v=t[k].tag*(t[k*2].r-t[k*2].l+1);
    t[k*2+1].v=t[k].tag*(t[k*2+1].r-t[k*2+1].l+1);
    t[k].tag=-1;
}
void update(int l,int r,int v,int k)
{
    if(t[k].l==l&&t[k].r==r)
    {
        t[k].tag=v;
        t[k].v=v*(r-l+1);//插满或者清空
        return;
    }
    if(t[k].tag!=-1)//要传递状态
        pushdown(k);
    int mid=(t[k].l+t[k].r)/2;
    if(r<=mid)
        update(l,r,v,k*2);
    else if(l>mid)
        update(l,r,v,k*2+1);
    else
    {
        update(l,mid,v,k*2);
        update(mid+1,r,v,k*2+1);
    }
    t[k].v=t[k*2].v+t[k*2+1].v;//向上传递区间信息
}
int query(int l,int r,int k)//查询区间内插花的数目
{
    if(t[k].l==l&&t[k].r==r)
    {
        return t[k].v;
    }
    if(t[k].tag!=-1)//同样lazy tag
        pushdown(k);
    int mid=(t[k].l+t[k].r)/2;
    if(r<=mid)
        return query(l,r,k*2);
    else if(l>mid)
        return query(l,r,k*2+1);
    else
    {
        return query(l,mid,k*2)+query(mid+1,r,k*2+1);
    }
    t[k].v=t[k*2].v+t[k*2+1].v;
}
int dive(int s,int num)//二分查找,第一个为开始的位置,第二个参数为要插多少个
{
    int temp=query(s,n,1);
    if(temp==n-s+1)//如果一个都不能插
        return -1;
    if(n-s+1-temp=l)//二分
    {
        mid=(l+r)/2;
        d=mid-s+1-query(s,mid,1);//d为从s到mid的空位数目
        if(d>num)
        {
            r=mid-1;
        }
        else if(d


你可能感兴趣的:(ACM,数据结构,线段树,二分,三分)