码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-18 11:53:25      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

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
3 7 2 1 9 4 Can not put any one. 2 6 2 0 9 4 4 5 2 3 线段树区间更新题目:
操作一:(A,F),从A花瓶开始插花,可以不连续,将F朵花插到花瓶中。给出初末位置。
操作二:(L,R),将区间中的花清除。计算区间中的花的朵数。
操作一,由于是不连续,所以我们考虑两次二分找出初末位置来做。
<pre name="code" class="cpp">#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int maxn=50001+100;
int col[maxn<<2],sum[maxn<<2];//lazy标记,col[i]=1表示空位,col[i]=0表示无空位,col[i]==-1表示全是空位
int t,n,m;
void pushup(int rs)
{
    sum[rs]=sum[rs<<1]+sum[rs<<1|1];
}
void pushdown(int rs,int s)
{
    if(col[rs]==0)
    {
        col[rs<<1]=col[rs<<1|1]=0;
        sum[rs<<1]=sum[rs<<1|1]=0;
        col[rs]=-1;
    }
    else if(col[rs]==1)
    {
        col[rs<<1]=col[rs<<1|1]=1;
        sum[rs<<1]=(s-(s>>1));
        sum[rs<<1|1]=(s>>1);
        col[rs]=-1;
    }
}
void build(int l,int r,int rs)
{
    col[rs]=-1;
    sum[rs]=1;
    if(l==r)
        return ;
    int mid=(l+r)>>1;
    build(l,mid,rs<<1);
    build(mid+1,r,rs<<1|1);
    pushup(rs);
}
void update(int x,int y,int c,int l,int r,int rs)
{
    if(l>=x&&r<=y)
    {
        col[rs]=c;
        if(c) sum[rs]=r-l+1;//为1表示空
        else  sum[rs]=0;
        return ;
    }
    int mid=(l+r)>>1;
    pushdown(rs,r-l+1);
    if(x<=mid)  update(x,y,c,l,mid,rs<<1);
    if(y>mid)  update(x,y,c,mid+1,r,rs<<1|1);
    pushup(rs);
}
int query(int x,int y,int l,int r,int rs)
{
    if(l>=x&&r<=y)
       return sum[rs];
    int mid=(l+r)>>1;
    pushdown(rs,r-l+1);
    int res=0;
    if(x<=mid)  res+=query(x,y,l,mid,rs<<1);
    if(y>mid)  res+=query(x,y,mid+1,r,rs<<1|1);
    pushup(rs);
    return res;
}
int main()
{
    int op,l,r;
    scanf("%d",&t);
    while(t--)
    {
       scanf("%d%d",&n,&m);
       build(0,n-1,1);
       while(m--)
       {
           scanf("%d%d%d",&op,&l,&r);//A--F
           if(op==1)
           {
               int ans=query(l,n-1,0,n-1,1);
               if(ans==0)//若没有位置了,输出。。
                 puts("Can not put any one.");
               else
               {
                   if(ans<r)  r=ans;
                   int ll=l,rr=n-1;//二分初末位置
                   while(ll<=rr)
                   {
                       int mid=(ll+rr)>>1;
                       if(query(l,mid,0,n-1,1)>=1) rr=mid-1;
                       else  ll=mid+1;
                   }
                   int a=ll;
                   ll=l;rr=n-1;
                   while(ll<=rr)
                   {
                       int mid=(ll+rr)>>1;
                       if(query(l,mid,0,n-1,1)>=r) rr=mid-1;
                       else  ll=mid+1;
                   }
                   int b=ll;
                   update(a,b,0,0,n-1,1);
                   printf("%d %d\n",a,b);
               }
           }
           else
           {
               printf("%d\n",r-l+1-query(l,r,0,n-1,1));
               update(l,r,1,0,n-1,1);
           }
       }
       puts("");
    }
    return 0;
}



 

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

标签:

原文地址:http://blog.csdn.net/u013582254/article/details/42834093

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!