• 【HDU-4614】Vases and Flowers(线段树双查询)


    11946317 2014-10-23 09:08:28 Accepted 4614 437MS 2348K 3340 B G++

    KinderRiven

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<map>
    #include<stack>
    #include<queue>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    /********************************
            KinderRiven
     *******************************/
    #define lson (pos<<1)
    #define rson (pos<<1|1)
    const int maxn = 55555;
    struct Node{
        int l,r;
        int v,col;
    }node[maxn << 2];
    int N,M;
    void pushdown(int pos){
        if(node[pos].col >= 0){
            node[lson].col = node[pos].col; node[rson].col = node[pos].col;
            node[lson].v = (node[lson].r - node[lson].l + 1) * node[pos].col;
            node[rson].v = (node[rson].r - node[rson].l + 1) * node[pos].col;
            node[pos].col = -1;
        }
    }
    void pushup(int pos){
        node[pos].v = node[lson].v + node[rson].v;
    }
    void BuildTree(int L,int R,int pos){
        node[pos].l  = L;
        node[pos].r  = R;
        node[pos].col= -1;
        if(L == R){
            node[pos].v = 1;
            return;
        }
        int m = (L + R) >> 1;
        BuildTree(L,m,lson);
        BuildTree(m + 1,R,rson);
        pushup(pos);
        return;
    }
    void UpDate(int L,int R,int pos,int value){
        //printf("%d %d
    ",node[pos].l,node[pos].r);
        if(L <= node[pos].l && node[pos].r <= R){
            node[pos].v = (node[pos].r - node[pos].l + 1) * value;
            node[pos].col = value;
            return;
        }
        pushdown(pos);
        int m = (node[pos].l + node[pos].r) >> 1;
        if(L <= m)
            UpDate(L,R,lson,value);
        if(R  > m)
            UpDate(L,R,rson,value);
        pushup(pos);
    }
    int Query_sum(int L,int R,int pos){
        if(L <= node[pos].l && node[pos].r <= R){
              return node[pos].v;
        }
        pushdown(pos);
        int m = (node[pos].l + node[pos].r) >> 1;
        int ret = 0;
        if(L <= m)
            ret += Query_sum(L,R,lson);
        if(R >  m)
            ret += Query_sum(L,R,rson);
        pushup(pos);
        return ret;
    }
    void Query(int value,int pos,int &p){
        if(node[pos].l == node[pos].r){
            p = node[pos].l;
            return;
        }
        pushdown(pos);
        int m = (node[pos].l + node[pos].r) >> 1;
        if(node[lson].v >= value)
            Query(value,lson,p);
        else
            Query(value - node[lson].v,rson,p);
        pushup(pos);
        return;
    }
    int main(){
        int T;
        scanf("%d",&T);
        while(T--){
            scanf("%d%d",&N,&M);
            BuildTree(0,N - 1,1);
            int op,a,b;
            for(int i = 0;i < M;i ++){
                scanf("%d%d%d",&op,&a,&b);
                //printf("%d
    ",Query_sum(0,N-1,1));
                if(op == 1){  //须要改动
                    int sum = Query_sum(0,N - 1,1);
                    int  k1 = Query_sum(a,N - 1,1);
                    int   v = sum - k1;
                    //printf("%d
    ",k1);
                    int  p,q;
                    if(k1 == 0){
                        printf("Can not put any one.
    ");
                        continue;
                    }
                    if(k1 < b) b = k1;
                    Query(v + 1,1,p);
                    Query(v + b,1,q);
                    UpDate(p,q,1,0);
                    printf("%d %d
    ",p,q);
                }
                else if(op == 2){
                    int value = Query_sum(a,b,1);
                    UpDate(a,b,1,1);
                    printf("%d
    ",b - a + 1 - value);
                }
            }
            printf("
    ");
        }
        return 0;
    }

  • 相关阅读:
    八字案例董易奇
    nginx和tomcat二合一服务器配置SSL证书
    RecyclerView,内容不居中的解决办法。
    cxf接口生成WSDL带密码的code实践
    去除server.key的密码
    Window下openssl的安装教程(通俗易懂)
    Nginx配置https证书
    自己做CA
    自己生成ssl证书
    自己制作ssl证书:自己签发免费ssl证书,为nginx生成自签名ssl证书
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5183827.html
Copyright © 2020-2023  润新知