zoukankan      html  css  js  c++  java
  • poj 3468

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <list>
    #include <map>
    #include <queue>
    #include <stack>
    #include <bitset>
    #include <algorithm>
    #include <numeric>
    #include <functional>
    using namespace std;
    #define LL __int64
    #define DB double
    #define N 100500
    const int INF = 0x3f3f3f3f;
    const LL INFF = 1LL << 60;
    const DB EPS = 1e-9;
    const DB OO = 1e15;
    const DB PI = acos(-1.0);
    LL sum[N*4],col[N*4],ql,qr,v;
    char s[3];
    void Pushup(LL rt)
    {
        sum[rt] = sum[rt<<1] + sum[rt<<1|1];
    }
    void build(LL rt,LL l,LL r)
    {
        col[rt] = 0;
        if(l == r) {scanf("%I64d",&sum[rt]);return ;}
        LL m = (l+r)>>1;
        build(rt<<1,l,m);
        build(rt<<1|1,m+1,r);
        Pushup(rt);
        return ;
    }
    void Pushdown(LL rt,LL m)
    {
        if(col[rt])
        {
            col[rt<<1] += col[rt];
            col[rt<<1|1] += col[rt];
            sum[rt<<1] += (m-(m>>1))*col[rt];
            sum[rt<<1|1] += (m>>1)*col[rt];
            col[rt] = 0;
            return ;
        }
    }
    void updata(LL rt,LL l,LL r)
    {
        if(l>=ql&&r<=qr)
        {
            sum[rt] += (r-l+1)*v;
            col[rt] += v;
            return ;
        }
        Pushdown(rt,r-l+1);
        LL m = (l+r)>>1;
        if(ql<=m) updata(rt<<1,l,m);
        if(m<qr) updata(rt<<1|1,m+1,r);
        Pushup(rt);
    }
    LL Query(LL rt,LL l,LL r)
    {
        LL ans = 0 ,m;
        if(ql<=l&&r<=qr)
        return sum[rt];
        Pushdown(rt,r-l+1);
        m = (l+r)>>1;
        if(ql<=m) ans+=Query(rt<<1,l,m);
        if(qr>m) ans+=Query(rt<<1|1,m+1,r);
        return ans;
    }
    int main()
    {
        LL n,m;
        while(~scanf("%I64d %I64d",&n,&m))
        {
            build(1,1,n);
            while(m--)
            {
                scanf("%s",s);
                if(s[0] == 'C')
                {
                    scanf("%I64d %I64d %I64d",&ql,&qr,&v);
                    updata(1,1,n);
                }else
                {
                    scanf("%I64d %I64d",&ql,&qr);
                    LL ans = Query(1,1,n);
                    printf("%I64d
    ",ans);
                }
            }
        }
        return 0;
    }
    View Code

    线段树区间修改

  • 相关阅读:
    Fiddler显示响应时间
    WPF RichTextBox
    设计模式摘要
    sqlite insert时一直报constraint 约束
    try catch使用规则
    关于WPF的2000件事 06--WPF布局
    SmartAssembly 汉化说明
    GP80系列打印机驱动 V17下载地址
    记录 java 安卓 各类引用包报错处理方法 例如 android.support.v4.app.+ ,io.reactivex.+
    第一讲 新手如何学习HTTP协议之实践项目多开微信机器人
  • 原文地址:https://www.cnblogs.com/llei1573/p/3912984.html
Copyright © 2011-2022 走看看