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): 4695    Accepted Submission(s): 1934


 

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]

 

Author

SYSU

 

Source

2013 Multi-University Training Contest 2

 

Recommend

zhuyuanchen520   |   We have carefully selected several similar problems for you:  6408 6407 6406 6405 6404 

#include<bits/stdc++.h>
using namespace std;

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define read(x,y) scanf("%d%d",&x,&y)

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1

const int  maxn =2e5+5;
/*
题目大意:插花,
删除操作要统计拿掉的花,插花的话要一直插道末尾或者花用光了。

线段树维护序列操作,
删除就是序列区间赋值0并统计区间和,
而难点在插入上,它要不断的插入直到剩余的花为零
或者插到花瓶序列的末尾才结束。
对区间二分即可,如果该区间的长度减去该区间已有的花数,
还不足以满足要插的数量,则更新数量和区间,然后二分。

二分找左端点要初始化为左闭右开区间,最终的左界是l。
虽然我不知道我原来的写法错哪里了,但是就是过不了。。。
*/
int n,m;
int op,lb,rb;
int tree[maxn<<2],lazy[maxn<<2];///维护01序列
void pushup(int rt){ tree[rt] = tree[rt<<1] + tree[rt<<1|1]; }
void pushdown(int l,int r,int rt)
{
    if(l==r) return ;
    int mid=l+r>>1;
    if(lazy[rt]>-1)
    {
        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
        tree[rt<<1]=(mid-l+1)*lazy[rt];
        tree[rt<<1|1]=(r-mid)*lazy[rt];
        lazy[rt]=-1;
    }
}

void build(lrt)
{
    lazy[rt]=-1;
    if(l==r) { tree[rt]=0;return;}
    int mid=l+r>>1;
    pushdown(l,r,rt);
    build(lson),build(rson);
    pushup(rt);
}

void update(lrt,int L,int R,int v)
{
    if(L<=l&&r<=R)
    {
        tree[rt]=(r-l+1)*v;
        lazy[rt]=v;
        return ;
    }
    pushdown(l,r,rt);
    int mid=l+r>>1;
    if(L<=mid) update(lson,L,R,v);
    if(mid<R) update(rson,L,R,v);
    pushup(rt);
}

int query(lrt,int L,int R)
{
    if(L<=l&&r<=R)   return tree[rt];
    pushdown(l,r,rt);
    int mid=l+r>>1,ans=0;
    if(L<=mid) ans+=query(l,mid,rt<<1,L,R);
    if(mid<R) ans+=query(mid+1,r,rt<<1|1,L,R);
    return ans;
}

int main()
{
    int t;scanf("%d",&t);
    for(int ca=1;ca<=t;ca++)
    {
        scanf("%d%d",&n,&m);
        build(1,n,1);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&op,&lb,&rb);
            if(op==1)
            {
                rb+=lb,lb++;

                int tot=query(1,n,1,lb,n);
                if(tot==n-lb+1) {  puts("Can not put any one.");  continue; }

                int cnt=rb-lb+1;///cnt记录要插的数量
                cnt=min(cnt,n-lb+1-tot);
                ///下面两个二分,一个找0的下界,一个找cnt的边界

                int l,r,ansl=0,ansr=0;
                l=lb,r=n;
                while(l<r)
                {
                    int mid=l+r>>1;
                    if(query(1,n,1,l,mid)==mid-l+1)  l=mid+1;
                    else   r=mid,ansl=mid;
                }

                l=lb,r=n+1,ansr=n;///二分细节,,初始化为左闭右开的区间,被wrong自闭了
                while(l<r)
                {
                    int mid=l+r>>1,q=query(1,n,1,l,mid);
                    if(mid-l+1-q<cnt)
                    {
                        update(1,n,1,l,mid,1);
                        cnt=cnt-(mid-l+1-q);
                        l=mid+1;
                    }
                    else  r=mid;
                 }
                 ansr=l;
                 if(cnt)   update(1,n,1,r,r,1);
                 printf("%d %d\n",ansl-1,ansr-1);
            }
            else
            {
                lb++,rb++;
                printf("%d\n",query(1,n,1,lb,rb));
                update(1,n,1,lb,rb,0);
            }
          ///for(int i=1;i<=n;i++) cout<<query(1,n,1,i,i)<<" ";puts("");
        }
        puts("");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/81709403