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


    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 
     

    思路:

    我们可以对于插花我们可以考虑区间更新 我们还要用线段树维护一个最左和最优区间 然后在这个区间上二分找到右端点

    #include <bits/stdc++.h>
    using namespace std;
    const double pi = acos(-1.0);
    const int N = 5e4+7;
    const int inf = 0x3f3f3f3f;
    const double eps = 1e-6;
    typedef long long ll;
    const ll mod = 10007;
    struct tree{
        int l,r;
        ll v,add;
        int left,right;
    }t[N<<2];
    void build(int p,int l,int r){
        t[p].l=l; t[p].r=r; t[p].add=-1;
        if(l==r){
            t[p].v=1;
            t[p].left=t[p].right=l;
            return ;
        }
        int mid=(l+r)>>1;
        build(p<<1,l,mid);
        build(p<<1|1,mid+1,r);
        t[p].v=t[p<<1].v+t[p<<1|1].v;
        t[p].left=min(t[p<<1].left,t[p<<1|1].left);
        t[p].right=max(t[p<<1].right,t[p<<1|1].right);
    }
    void pushdown(int p){
        if(t[p].add!=-1){
            t[p<<1].add=t[p<<1|1].add=t[p].add;
            t[p<<1].v=(t[p<<1].r-t[p<<1].l+1)*t[p].add;
            t[p<<1|1].v=(t[p<<1|1].r-t[p<<1|1].l+1)*t[p].add;
            if(t[p].add==1){
                t[p<<1].left=t[p<<1].l;
                t[p<<1].right=t[p<<1].r;
                t[p<<1|1].left=t[p<<1|1].l;
                t[p<<1|1].right=t[p<<1|1].r;
            }else{
                t[p<<1].left=inf;
                t[p<<1].right=-inf;
                t[p<<1|1].left=inf;
                t[p<<1|1].right=-inf;
            }
            t[p].add=-1;
        }
    }
    void update(int p,int l,int r,int v){
        if(l<=t[p].l&&t[p].r<=r){
            t[p].v=(t[p].r-t[p].l+1)*v;
            t[p].add=v;
            if(v==1){
                t[p].left=t[p].l;
                t[p].right=t[p].r;
            }else{
                t[p].left=inf;
                t[p].right=-inf;
            }
            return ;
        }
        pushdown(p);
        int mid=(t[p].l+t[p].r)>>1;
        if(l<=mid) update(p<<1,l,r,v);
        if(r>mid) update(p<<1|1,l,r,v);
        t[p].v=t[p<<1].v+t[p<<1|1].v;
        t[p].left=min(t[p<<1].left,t[p<<1|1].left);
        t[p].right=max(t[p<<1].right,t[p<<1|1].right);
    }
    int query1(int p,int l,int r){
        if(l<=t[p].l&&t[p].r<=r){
            return t[p].v;
        }
        pushdown(p);
        int mid=(t[p].l+t[p].r)>>1;
        int res=0;
        if(l<=mid)    res+=query1(p<<1,l,r);
        if(r>mid) res+=query1(p<<1|1,l,r);
        return res; 
    }
    int query2(int p,int l,int r){
        if(l<=t[p].l&&t[p].r<=r){
            return t[p].left;
        }
        pushdown(p);
        int mid=(t[p].l+t[p].r)>>1;
        int res=inf;
        if(l<=mid)    res=min(res,query2(p<<1,l,r));
        if(r>mid) res=min(res,query2(p<<1|1,l,r));
        return res; 
    }
    int query3(int p,int l,int r){
        if(l<=t[p].l&&t[p].r<=r){
            return t[p].right;
        }
        pushdown(p);
        int mid=(t[p].l+t[p].r)>>1;
        int res=-inf;
        if(l<=mid)    res=max(res,query3(p<<1,l,r));
        if(r>mid) res=max(res,query3(p<<1|1,l,r));
        return res; 
    }
    int main(){
    //    ios::sync_with_stdio(false);
    //    cin.tie(0); cout.tie(0);
        int t; scanf("%d",&t);
        while(t--){
            int n,m; scanf("%d%d",&n,&m);
            build(1,1,n);
            for(int i=1;i<=m;i++){
                int k,x,y; scanf("%d%d%d",&k,&x,&y);
                if(k==1){
                    //cout<<query2(1,x+1,n)-1<<" "<<query3(1,x+1,n)-1<<"
    ";
                    int l=query2(1,x+1,n); int r=query3(1,x+1,n);
                    int res=query1(1,l,r);
                    if(res<y){
                        if(res>=1){
                            printf("%d %d
    ",l-1,r-1);
                            update(1,l,r,0);
                        }else{
                            printf("Can not put any one.
    ");
                        }
                        continue;
                    }
                    int L=l,R;
                    while(l<=r){
                        int mid=(l+r)>>1;
                        int ans=query1(1,L,mid);
                        if(ans>=y){
                            r=mid-1;
                            R=mid;
                        }else{
                            l=mid+1;
                        }
                    }
                    printf("%d %d
    ",L-1,R-1);
                    update(1,L,R,0);
                }
                else{
                    int res=query1(1,x+1,y+1);
                    printf("%d
    ",(y-x+1)-res);
                    update(1,x+1,y+1,1);
                }
            }
            printf("
    ");
        }
    }
    //1
    //10 5
    //1 0 10
    //1 8 7
    //2 2 7
    //2 5 9
    //1 8 10
    View Code
  • 相关阅读:
    jq select 一些操作
    jq控制select值为某个时选中
    php loop循环 拿到键名
    php输出textarea数据(入库没有处理的)
    div下面多个a标签的点击事件,并且获取a的属性
    微信支付签名错误(第四点试过已得)
    微擎系统二维码关注回调(后台生成的二维码,可用于生成下线)
    微擎系统生成可以扫描关注的二维码,在后台二维码管理哪里有生成流程
    关于ContentProvider(二)
    关于ContentProvider(初识篇)
  • 原文地址:https://www.cnblogs.com/wmj6/p/11439828.html
Copyright © 2020-2023  润新知