hdu 4614 Vases and Flowers(线段树+二分)

Vases and Flowers

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 4132    Accepted Submission(s): 1681


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

210 51 3 52 4 51 1 82 3 61 8 810 61 2 52 3 41 0 82 2 51 4 41 2 3
 

Sample Output

[pre]3 721 94Can not put any one.2 620 944 52 3[/pre]
 

Author
SYSU
 

Source
2013 Multi-University Training Contest 2
 

Recommend
zhuyuanchen520

题意:

有一排的空花瓶,编号从0至N-1,一个花瓶只能插入一朵花。

操作1是从x开始往后插入F朵花,如果碰到了插了花的花瓶则跳过,否则插花。

操作2是将x到y之间的花全部清空,问丢掉多少朵花。


思路:

用线段树存入区间的空花瓶的数量,因为从x到N之间的空花盆的数量是单调不下降的,所以可以用二分来做。

对于第一种操作,二分出插入第一朵和插入最后一朵花的位置,二分最后一朵花的位置,是用min(F,total)来二分。

对于第二种操作,直接查询就可以了。


代码:

#include 
#include 
#include 
#include 
using namespace std;
const int maxn=50005;
int tr[maxn<<2],lazy[maxn<<2];
int n,m,k,x,y;
void pushup(int i)
{
    tr[i]=tr[i*2]+tr[i*2+1];
}
void pushdown(int i,int len)
{
    if(lazy[i]!=-1)
    {
        lazy[i*2]=lazy[i*2+1]=lazy[i];
        tr[i*2]=(len-len/2)*lazy[i];
        tr[i*2+1]=len/2*lazy[i];
        lazy[i]=-1;
    }
    return;
}
void build(int i,int l,int r)
{
    lazy[i]=-1;
    if(l==r)
    {
        tr[i]=1;
        return;
    }
    int mid=(l+r)/2;
    build(2*i,l,mid);
    build(2*i+1,mid+1,r);
    pushup(i);
    return;
}
void update(int i,int l,int r,int x,int y,int c)
{
    if(x<=l&&r<=y)
    {
        tr[i]=(r-l+1)*c;
        lazy[i]=c;
        return;
    }
    pushdown(i,r-l+1);
    int mid=(l+r)/2;
    if(x<=mid) update(2*i,l,mid,x,y,c);
    if(y>mid) update(2*i+1,mid+1,r,x,y,c);
    pushup(i);
    return;
}
int query(int i,int l,int r,int x,int y)
{
    int sum=0;
    if(x<=l&&r<=y)
    {
        return tr[i];
    }
    pushdown(i,r-l+1);
    int mid=(l+r)/2;
    if(x<=mid) sum+=query(2*i,l,mid,x,y);
    if(y>mid) sum+=query(2*i+1,mid+1,r,x,y);
    pushup(i);
    return sum;
}
int findpos(int x,int num)//二分位置
{
    int l=x,r=n;
    int pos,mid;
    while(l<=r)
    {
        mid=(l+r)/2;
        if(query(1,1,n,x,mid)>=num)
            pos=mid,r=mid-1;
        else
            l=mid+1;
    }
    return pos;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        build(1,1,n);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&k,&x,&y);
            if(k==1)
            {
                x++;
                int total=query(1,1,n,x,n);
                if(total==0)
                    printf("Can not put any one.\n");
                else
                {
                    int pos1=findpos(x,1);
                    int pos2=findpos(x,min(y,total));
                    printf("%d %d\n",pos1-1,pos2-1);
                    update(1,1,n,pos1,pos2,0);
                }
            }
            else
            {
                x++,y++;
                printf("%d\n",y-x+1-query(1,1,n,x,y));
                update(1,1,n,x,y,1);
            }
        }
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(线段树)