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

Vases and Flowers

Time Limit:2000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

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]
 
二分每次的插入区间,确定插入的终点,再更新这段区间。
注意二分后取l还是l-1,这里RE了几发

#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
//#pragma comment(linker, "/STACK:1024000000,1024000000");

using namespace std;

#define INF 0x3f3f3f3f
#define maxn 210005

int sum[maxn];
int tag[maxn];
int lt[maxn];
int rt[maxn];
int ll[maxn],rr[maxn];

void init(int l,int r,int k)
{
    sum[k]=r-l+1;
    tag[k]=0;
    lt[k]=l;
    rt[k]=r;
    ll[k]=l;
    rr[k]=r;
    if(l==r) return ;
    int mid=(l+r)>>1;
    init(l,mid,k<<1);
    init(mid+1,r,k<<1|1);
}

void pushdown(int k)
{
    if(!tag[k]) return ;
    if(tag[k]==1)
    {
        tag[k<<1]=tag[k<<1|1]=1;
        lt[k<<1]=INF;
        rt[k<<1]=-INF;
        lt[k<<1|1]=INF;
        rt[k<<1|1]=-INF;
        sum[k<<1]=sum[k<<1|1]=0;
    }
    else
    {
        tag[k<<1]=tag[k<<1|1]=-1;
        lt[k<<1]=ll[k<<1];
        rt[k<<1]=rr[k<<1];
        lt[k<<1|1]=ll[k<<1|1];
        rt[k<<1|1]=rr[k<<1|1];
        sum[k<<1]=rr[k<<1]-ll[k<<1]+1;
        sum[k<<1|1]=rr[k<<1|1]-ll[k<<1|1]+1;
    }
    tag[k]=0;
}

void pushup(int k)
{
    sum[k]=sum[k<<1]+sum[k<<1|1];
    lt[k]=min(lt[k<<1],lt[k<<1|1]);
    rt[k]=max(rt[k<<1],rt[k<<1|1]);
}

int ans1,ans2;
void update(int d,int l,int r,int s,int e,int k)
{
    if(s==l&&r==e)
    {
        tag[k]=d;
        if(!ans1&<[k]!=INF) ans1=lt[k];
        if(rt[k]!=-INF) ans2=rt[k];
        if(d==1)
        {
            lt[k]=INF;
            rt[k]=-INF;
            sum[k]=0;
        }
        else
        {
            lt[k]=l;
            rt[k]=r;
            sum[k]=r-l+1;
        }
        return ;
    }
    pushdown(k);
    int mid=(s+e)>>1;
    if(r<=mid) update(d,l,r,s,mid,k<<1);
    else if(l>mid) update(d,l,r,mid+1,e,k<<1|1);
    else
    {
        update(d,l,mid,s,mid,k<<1);
        update(d,mid+1,r,mid+1,e,k<<1|1);
    }
    pushup(k);
}

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

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        init(1,n,1);
        while(m--)
        {
            int p;
            scanf("%d",&p);
            if(p==1)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                a++;
                int l=a,r=n;
                while(l+1<=r)
                {
                    int mid=(l+r)>>1;
                    int temp=query(a,mid,1,n,1);
                    if(temp<=b) l=mid+1;
                    else r=mid-1;
                }
                int k;
                int temp=query(a,l,1,n,1);
                if(temp<=b) k=l;
                else k=l-1;
                temp=query(a,k,1,n,1);
                if(temp==0)
                {
                    printf("Can not put any one.\n");
                    continue;
                }
                ans1=ans2=0;
                update(1,a,k,1,n,1);
                printf("%d %d\n",ans1-1,ans2-1);
            }
            else
            {
                int a,b;
                scanf("%d%d",&a,&b);
                a++,b++;
                if(a>b) swap(a,b);
                int ans=query(a,b,1,n,1);
                printf("%d\n",b-a+1-ans);
                update(-1,a,b,1,n,1);
            }
        }
        puts("");
    }
    return 0;
}


你可能感兴趣的:(HDU 4614 Vases and Flowers (线段树 + 二分))