HDOJ 4614 Vases and Flowers


线段树+二分区间

用线段树维护某一段内还有多少个花瓶可以用,二分确定插入的左右界.....

Vases and Flowers

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


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
 



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

const int maxn=60050;
int sum[maxn<<2],col[maxn<<2];
int n,m,leftbound,rightbound,F,A,B;

void init()
{
    memset(sum,0,sizeof(sum));
    memset(col,-1,sizeof(col));
}

void push_up(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void build(int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=1; return ;
    }
    int m=(l+r)/2;
    build(lson); build(rson);
    push_up(rt);
}

void push_down(int len,int rt)
{
    if(col[rt]!=-1)
    {
        col[rt<<1]=col[rt<<1|1]=col[rt];
        sum[rt<<1]=(len-len/2)*col[rt];
        sum[rt<<1|1]=(len/2)*col[rt];
        col[rt]=-1;
    }
}

int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
        return sum[rt];
    push_down(r-l+1,rt);
    int m=(l+r)/2;
    int ret=0;
    if(L<=m) ret+=query(L,R,lson);
    if(R>m) ret+=query(L,R,rson);
    return ret;
}

void Update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        col[rt]=c;
        sum[rt]=(r-l+1)*c;
        return ;
    }
    push_down(r-l+1,rt);
    int m=(l+r)/2;
    if(L<=m) Update(L,R,c,lson);
    if(R>m) Update(L,R,c,rson);
    push_up(rt);
}

void showit(int l,int r,int rt)
{
    cout<<"rt: "<<rt<<" left "<<l<<" right "<<r<<" sum "<<sum[rt]<<" col: "<<col[rt]<<endl;
    if(l>=r) return ;
    int m=(l+r)/2;
    showit(lson);showit(rson);
}

int get_left_point(int x)
{
    int ans=-1;
    int low=x,high=n,mid;
    int w;
    while(low<=high)
    {
        mid=(low+high)/2;
        w=query(x,mid,1,n,1);
        if(!w) low=mid+1;
        else ans=mid,high=mid-1;
    }
    return ans;
}

int get_right_point()
{
    int ans=-1;
    int low=leftbound,high=n,mid;
    int w;
    B=min(B,query(low,n,1,n,1));
    while(low<=high)
    {
        mid=(low+high)/2;
        w=query(leftbound,mid,1,n,1);
        if(w>=B) high=mid-1;
        else low=mid+1;
        if(w==B) ans=mid;
    }
    return ans;
}

int main()
{
    int T_T;
    scanf("%d",&T_T);
    while(T_T--)
    {
        scanf("%d%d",&n,&m);
        init();
        build(1,n,1);
        while(m--)
        {
            scanf("%d%d%d",&F,&A,&B);
            A++;
            if(F==1)
            {
                if(!query(A,n,1,n,1)||B==0)
                {
                    puts("Can not put any one.");
                    continue;
                }
                leftbound=get_left_point(A);
                rightbound=get_right_point();
                printf("%d %d\n",leftbound-1,rightbound-1);
                Update(leftbound,rightbound,0,1,n,1);
            }
            else if(F==2)
            {
                B++;
                printf("%d\n",B-A+1-query(A,B,1,n,1));
                Update(A,B,1,1,n,1);
            }
        }
        putchar(10);
    }
    return 0;
}





你可能感兴趣的:(HDOJ 4614 Vases and Flowers)