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

    [pre]3 7
    2
    1 9
    4
    Can not put any one.
    
    2 6
    2
    0 9
    4
    4 5
    2 3
    
    [/pre]

    要和二分结合一下 线段树方面没有什么特别的

    但是一时想不到要和二分结合

    查询就是查区间的和 更新也就是全变0或全变1 关键就是怎么找到最开始可以插花的位子和最后一个位子

    这里就需要二分了

    先二分找到可以插花的位子 pos

    再二分找到可以插花的最远的位子ans 这个位子可能已经有花了 反正就是pos到ans可以把花都插进去

    最后二分找到实际的最后插花的位子 realans

    
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<stack>
    #define inf 1e18
    using namespace std;
    
    const int maxn = 50005;
    int flower[maxn << 2];
    int same[maxn << 2];
    int m, n;
    
    void pushup(int rt)
    {
        flower[rt] = flower[rt << 1] + flower[rt<<1|1];
        //same[rt] = same[rt<<1] && same[rt<<1|1];
    }
    
    void build(int l, int r, int rt)
    {
        flower[rt] = 0;
        same[rt] = 0;
        if(l == r) return;
        int m = (l + r) >> 1;
        build(l, m, rt << 1);
        build(m + 1, r, rt << 1 | 1);
        pushup(rt);
    }
    
    void pushdown(int l, int r, int rt)
    {
        if(same[rt]){
            int m = (l + r) / 2;
            same[rt<<1] = same[rt<<1|1] = same[rt];
            flower[rt<<1] = (m - l + 1) * same[rt];
            flower[rt<<1|1] = (r - m) * same[rt];
            if(same[rt] == -1){
                flower[rt<<1] = flower[rt<<1|1] = 0;
            }
            same[rt] = 0;
        }
    
    }
    
    void update(int L, int R, int x, int l, int r, int rt)
    {
        if(l >= L && r <= R){
            if(x == 1){
                flower[rt] = r - l + 1;
                same[rt] = true;
            }
            else{
                flower[rt] = 0;
                same[rt] = -1;
            }
            return;
        }
        pushdown(l, r, rt);
        int m = (l + r) / 2;
        if(L <= m) update(L, R, x, l, m, rt<<1);
        if(R > m) update(L, R, x, m + 1, r, rt<<1|1);
        pushup(rt);
    }
    
    int query(int L, int R, int l, int r, int rt)
    {
        if(L <= l && R >= r){
            return flower[rt];
        }
        pushdown(l, r, rt);
        int m = (l + r) / 2, ans = 0;
        if(L <= m) ans += query(L, R, l, m, rt<<1);
        if(R > m) ans += query(L, R, m + 1, r, rt<<1|1);
        pushup(rt);
        return ans;
    }
    
    int main()
    {
        int t;
        cin>>t;
        while(t--){
            scanf("%d%d", &n, &m);
            build(1, n, 1);
            while(m--){
                int l, r, op;
                scanf("%d%d%d", &op, &l, &r);
                l++;r++;
                if(op == 1){
                    r--;
                    int left = l, right = n, pos = -1;
                    while(right - left >= 0){//找第一个可插花点
                        int mid = (right + left) / 2;
                        if(query(l, mid, 1, n, 1) == mid - l + 1){
                            left = mid + 1;
                        }
                        else{
                            right = mid - 1;
                            pos = mid;
                        }
                    }
                    if(pos == -1)
                        printf("Can not put any one.
    ");
                    else{
                        int ans = -1;
                        left = pos;
                        right = n;
                        while(right - left >= 0){//找最远可插花位子 得到的ans可能原来已经被插上花
                            int mid = (left + right) / 2;
                            if(mid - pos + 1 - query(pos, mid, 1, n, 1) <= r){
                                left = mid + 1;
                                ans = mid;
                            }
                            else{
                                right = mid - 1;
                            }
                        }
                        int realans = -1;
                        left = pos;
                        right = ans;
                        while(right - left >= 0){//找到实际插最后一朵花的位子,可能花没被插完
                                //要在pos到ans中找到最远的空位
                            int mid = (left + right) / 2;
                            if(ans - mid + 1 - query(mid, ans, 1, n, 1) == 0){
                                right = mid - 1;
                                realans = mid;
                            }
                            else left = mid + 1;
                        }
                        if(realans == -1){
                            printf("%d %d
    ", pos - 1, ans - 1);
                        }
                        else{
                            printf("%d %d
    ", pos - 1, realans - 2);
                        }
                        update(pos, ans, 1, 1, n, 1);
                    }
                }
                if(op == 2){
                    printf("%d
    ", query(l, r, 1, n, 1));
                    update(l, r, -1, 1, n, 1);
                }
            }
            printf("
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    解决CSS图片底部3像素问题总结
    常用伪元素及content属性值的使用
    javascript中用正则表达式判断是否为汉字及常用的判断
    javascript 正则表达式
    js常用事件
    js动态获取select选中的option
    innerHTML innerText与outerHTML间的区别
    vue的生命周期(又称钩子函数)----以及vue1.0版本与vue2.0版本生命周期的不同
    JavaScript实现表单验证_02
    JavaScript数组实现图片轮播
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9643391.html
Copyright © 2011-2022 走看看