hdu 4614 线段树+二分

Vases and Flowers

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

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
 
裸线段树。线段树区间修改查询+二分。
代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
using namespace std;
const int maxn=55555;
#define L(x) 2*x
#define R(x) 2*x+1
struct node
{
        int l,r,flag,num;
        int mid(){return (l+r)>>1;}
        int len(){return r-l+1;}
}tree[5*maxn];
void pushup(int p)
{
        tree[p].num=tree[L(p)].num+tree[R(p)].num;
}
void pushdown(int p)
{
        if(tree[p].flag==0)
        {
                tree[L(p)].flag=0;
                tree[R(p)].flag=0;
                tree[p].flag=-1;
                tree[L(p)].num=tree[L(p)].len();
                tree[R(p)].num=tree[R(p)].len();
        }
        else if(tree[p].flag==1)
        {
                tree[L(p)].flag=1;
                tree[R(p)].flag=1;
                tree[p].flag=-1;
                tree[L(p)].num=0;
                tree[R(p)].num=0;
        }
}
void build(int p,int l,int r)
{
        tree[p].l=l;
        tree[p].r=r;
        tree[p].flag=-1;
        tree[p].num=tree[p].len();
        if(l==r)return;
        int m=tree[p].mid();
        build(L(p),l,m);
        build(R(p),m+1,r);
}
void update(int p,int l,int r,int flag)
{
        if(tree[p].l>=l&&tree[p].r<=r)
        {
                tree[p].flag=flag;
                if(flag==0)tree[p].num=tree[p].len();
                else tree[p].num=0;
                return;
        }
        pushdown(p);
        int m=tree[p].mid();
        if(l<=m)update(L(p),l,r,flag);
        if(r>m)update(R(p),l,r,flag);
        pushup(p);
}
int query(int p,int l,int r)
{
        if(tree[p].flag==1)return 0;
       // else if(tree[p].flag==0)return min(r-l+1,tree[p].len());
        if(tree[p].l>=l&&tree[p].r<=r)return tree[p].num;
        pushdown(p);
        int m=tree[p].mid();
        int ans=0;
        if(l<=m)ans+=query(L(p),l,r);
        if(r>m)ans+=query(R(p),l,r);
        return ans;
}
int main()
{
        int T,i,j,k,m,n,p,q,a,b;
       // freopen("data.in","r",stdin);
        //freopen("data1.out","w",stdout);
        scanf("%d",&T);
        while(T--)
        {
                scanf("%d%d",&n,&m);
                build(1,0,n-1);
                while(m--)
                {
                        scanf("%d%d%d",&i,&j,&k);
                        if(i==2)
                        {
                                p=query(1,j,k);
                               // cout<<"p="<<p<<endl;
                                p=k-j+1-p;
                                printf("%d\n",p);
                              //cout<<j<<" "<<k<<" "<<p<<endl;

                                update(1,j,k,0);
                        }
                        else
                        {
                                int l,r,mid;
                                p=query(1,j,n-1);
                                if(p==0)puts("Can not put any one.");
                                else
                                {
                                        p=min(p,k);
                                        l=j,r=n-1;
                                        while(l<r)
                                        {
                                                mid=(l+r)>>1;
                                                if(query(1,j,mid)==0)l=mid+1;
                                                else r=mid;
                                        }
                                        a=l;
                                        l=j;r=n-1;
                                        while(l<r)
                                        {
                                                mid=(l+r)>>1;
                                                if(query(1,j,mid)<p)l=mid+1;
                                                else r=mid;
                                        }
                                        b=l;
                                        printf("%d %d\n",a,b);
                                        update(1,a,b,1);
                                }
                        }
                }
                puts("");
        }
        return 0;
}

 

posted @ 2013-09-28 23:24  线性无关  阅读(128)  评论(0)    收藏  举报