zoukankan      html  css  js  c++  java
  • hdu4614——Vases and Flowers

    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
    

    题解:

    线段树多维护两个变量first和last,分别是区间第一个可用位置和最后一个可用位置(-1代表没有)。然后操作1直接可以查询两次来找到first和last然后更新(first,last)为0。操作2则更新区间并求和就行了。

    我看网上有很多用二分方法的,亲测这种方法比二分快出一倍到三倍。

    代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    const int MAXN = 50005;
    const int INF = 0x3f3f3f3f;
    
    struct D{
        int len;
        int value;
        int first,last;
    }Tree[MAXN*4];
    int Change[MAXN*4];
    
    void Up(int temp){
        Tree[temp].value = Tree[temp<<1].value + Tree[temp<<1|1].value;
        if(Tree[temp<<1].first!=-1)Tree[temp].first = Tree[temp<<1].first;
        else Tree[temp].first = Tree[temp<<1|1].first;
        if(Tree[temp<<1|1].last!=-1)Tree[temp].last = Tree[temp<<1|1].last;
        else Tree[temp].last = Tree[temp<<1].last;
    }
    
    void Build(int temp,int left,int right){
        Tree[temp].len = right-left+1;
        if(left == right){
            Tree[temp].value = 1;
            Tree[temp].first = Tree[temp].last = left;
            return ;
        }
        int mid = left + (right-left)/2;
        Build(temp<<1,left,mid);
        Build(temp<<1|1,mid+1,right);
        Up(temp);
    }
    
    void PushDown(int temp,int left ,int right){
        if(Change[temp]!=-1){
            Change[temp<<1] = Change[temp<<1|1] = Change[temp];
            int mid = left + (right-left)/2;
            if(Change[temp]){
                Tree[temp<<1].value = Tree[temp<<1].len;
                Tree[temp<<1].first = left;
                Tree[temp<<1].last = mid;
                Tree[temp<<1|1].value = Tree[temp<<1|1].len;
                Tree[temp<<1|1].first = mid+1;
                Tree[temp<<1|1].last = right;
            }else{
                Tree[temp<<1].value = Tree[temp<<1|1].value = 0;
                Tree[temp<<1].first = Tree[temp<<1].last = -1;
                Tree[temp<<1|1].first = Tree[temp<<1|1].last = -1;
            }
            Change[temp] = -1;
        }
    } 
    
    int Updata(int temp,int left,int right,int ql,int qr,int value){
        if(ql>right || qr<left)return 0;
        if(ql<=left && qr>=right){
            int t = Tree[temp].len-Tree[temp].value;
            if(value == 1){
                if(Tree[temp].value == Tree[temp].len)return 0;
                Tree[temp].value = Tree[temp].len;
                Tree[temp].first = left;
                Tree[temp].last = right;
                Change[temp] = 1;
                return t;
            }
            else if(value == 0){
                if(Tree[temp].value == 0)return 0;
                Tree[temp].value = 0;
                Tree[temp].first = Tree[temp].last = -1;
                Change[temp] = 0;
                return Tree[temp].len-t;
            }
        }
        PushDown(temp,left,right);
        int mid = left + (right-left)/2;
        int sum = 0;
        if(ql<=mid)sum += Updata(temp<<1,left,mid,ql,qr,value);
        if(qr>mid)sum += Updata(temp<<1|1,mid+1,right,ql,qr,value);
        Up(temp); 
        return sum;
    }
    
    int Now;
    int Query(int temp,int left,int right,int ql,int qr,bool flag){
        if(Tree[temp].value == 0 || Now == 0){
            if(flag)return INF;
            else return -INF;
        }
        if(ql>right || qr<left){
            if(flag)return INF;
            else return -INF;
        }
        if(ql<=left && qr>=right && Tree[temp].value<=Now){
            Now -= Tree[temp].value;
            if(flag)return Tree[temp].first;
            else return Tree[temp].last;
        }
        PushDown(temp,left,right);
        int mid = left + (right-left)/2;
        int a,b;
        a = Query(temp<<1,left,mid,ql,qr,flag);
        b = Query(temp<<1|1,mid+1,right,ql,qr,flag);
        if(flag)return min(a,b);
        else return max(a,b);
    }
    
    int main(){
        
        int T,N,M;
        scanf("%d",&T);
        int a,b,c;
        while(T--){
            scanf("%d %d",&N,&M);
            memset(Change,-1,sizeof(Change));
            Build(1,0,N-1);
            while(M--){
                scanf("%d %d %d",&a,&b,&c);
                if(a == 1){
                    Now = c;
                    int f = Query(1,0,N-1,b,N-1,true);
                    if(f == INF)printf("Can not put any one.
    ");
                    else{
                        Now = c;
                        int l = Query(1,0,N-1,b,N-1,false);
                        printf("%d %d
    ",f,l);
                        Updata(1,0,N-1,f,l,0);
                    }
                }
                else {
                    printf("%d
    ",Updata(1,0,N-1,b,c,1));
                }
            }
            printf("
    ");
        }
        
        return 0;
    }
  • 相关阅读:
    Math app 2.0
    “口袋精灵”单元测试
    学习进度条
    本学期总结
    sprint2的总结及团队贡献分
    点餐系统Sprint1总结
    实验8
    实验7
    实验6
    实验五
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9105865.html
Copyright © 2011-2022 走看看