• hdu 4107 Gangster 线段树(成段更新)


    维护每个区间的最小值和最大值,update的时候判断low[rt]与up[rt]和p的大小关系,进行更新操作。

    卡时卡得很紧。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    using namespace std;
    const int maxn=211111;
    int up[maxn<<2],low[maxn<<2],col[maxn<<2];
    int n,m,p;
    
    inline int max(int &a,int &b){
        if(a>b)return a;
        return b;
    }
    inline int min(int &a,int &b){
        if(a<b)return a;
        return b;
    }
    void build(int l,int r,int rt){
    //    up[rt]=low[rt]=col[rt]=0;
        if(l==r)return ;
        int m=(l+r)>>1;
        build(lson);
        build(rson);
    }
    void pushup(int rt){
        up[rt]=max(up[rt<<1],up[rt<<1|1]);
        low[rt]=min(low[rt<<1],low[rt<<1|1]);
    }
    void down(int rt){
        if(col[rt]){
            col[rt<<1]+=col[rt];
            col[rt<<1|1]+=col[rt];
            up[rt<<1]+=col[rt];
            up[rt<<1|1]+=col[rt];
            low[rt<<1]+=col[rt];
            low[rt<<1|1]+=col[rt];
            col[rt]=0;
        }
    }
    void update(int L,int R,int c,int l,int r,int rt){
        if(L<=l&&R>=r){
            if(up[rt]<p)up[rt]+=c,low[rt]+=c,col[rt]+=c;
            else if(low[rt]>=p)low[rt]+=2*c,up[rt]+=2*c,col[rt]+=2*c;
            else {
                down(rt);
                int m=(l+r)>>1;
                if(L<=m)update(L,R,c,lson);
                if(R>m)update(L,R,c,rson);
                pushup(rt);
            }
            return;
        }
        down(rt);
        int m=(l+r)>>1;
        if(L<=m)update(L,R,c,lson);
        if(R>m)update(L,R,c,rson);
        pushup(rt);
    }
    void query(int l,int r,int rt){
        if(l==r){
            if(l!=1)printf(" ");
            printf("%d",low[rt]);
            return ;
        }
        down(rt);
        int m=(l+r)>>1;
        query(lson);
        query(rson);
    }
    void run(){
        memset(up,0,sizeof(int)*((n<<2)+10));
        memset(low,0,sizeof(int)*((n<<2)+10));
        memset(col,0,sizeof(int)*((n<<2)+10));
        build(1,n,1);
        while(m--){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            update(a,b,c,1,n,1);
        }
        query(1,n,1);
        puts("");
    }
    int main()
    {
    //    freopen("in","r",stdin);
        while(scanf("%d%d%d",&n,&m,&p)>0)run();
        return 0;
    }
  • 相关阅读:
    高级程序员和普通程序员有哪些区别?
    自学编程需要注意什么?
    你是如何转行的?转行容易吗?
    什么是VueCLI3
    强制缓存和协商缓存有什么区别
    npm与package.json
    最高效的学习方法——逆向学习法
    html space空格符
    in typeof instanceof ===这些运算符有什么作用
    教你如何“快速”激活IntelliJ IDEA 工具
  • 原文地址:https://www.cnblogs.com/wshh/p/3989237.html
Copyright © 2020-2023  润新知