• 线段树——D


    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

    Input

    The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
    The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
    Each of the next Q lines represents an operation.
    "C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
    "Q a b" means querying the sum of AaAa+1, ... , Ab.

    Output

    You need to answer all Q commands in order. One answer in a line.

    Sample Input

    10 5
    1 2 3 4 5 6 7 8 9 10
    Q 4 4
    Q 1 10
    Q 2 4
    C 3 6 3
    Q 2 4
    

    Sample Output

    4
    55
    9
    15

    Hint

    The sums may exceed the range of 32-bit integers.

    ps:怕了怕了,数组开大点。空格%c注意,用long long 吧。呜呜呜

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    typedef long long ll;
    const int N=500005;
    ll a[N];
    ll sum[N],lazy[N];
    void build(ll l,ll r,ll rt){
        if(l==r){
            sum[rt]=a[l];
            return;
        }
        ll mid=(l+r)/2;
        build(l,mid,rt*2);
        build(mid+1,r,rt*2+1);
        sum[rt]=sum[rt*2]+sum[rt*2+1];
    }
    void pushdown(ll l,ll r,ll rt){
        if(lazy[rt]){
            lazy[rt*2]+=lazy[rt];
            lazy[rt*2+1]+=lazy[rt];
            sum[rt*2]+=lazy[rt]*l;//这里以后还是这样写吧。。。
            sum[rt*2+1]+=lazy[rt]*r;
            lazy[rt]=0;
        }
    }
    void update(ll l1,ll r1,ll v,ll l,ll r,ll rt){
        if(l1<=l&&r1>=r){
            sum[rt]+=v*(r-l+1);
            lazy[rt]+=v;
            return;
        }
        ll mid=(l+r)/2;
        pushdown(mid-l+1,r-mid,rt);
        if(l1<=mid){
            update(l1,r1,v,l,mid,rt*2);
        }
        if(r1>mid){
            update(l1,r1,v,mid+1,r,rt*2+1);
        }
        sum[rt]=sum[rt*2]+sum[rt*2+1];
    }
    ll query(ll l1,ll r1,ll l,ll r,ll rt){
        if(l1<=l&&r1>=r){
            return sum[rt];
        }
        ll ret=0;
        ll mid=(l+r)/2;
        pushdown(mid-l+1,r-mid,rt);
        if(l1<=mid){
            ret+=query(l1,r1,l,mid,rt*2);
        }
        if(r1>mid){
            ret+=query(l1,r1,mid+1,r,rt*2+1);
        }
        return ret;
    }
    int main()
    {
        ll n,q;
        scanf("%lld%lld",&n,&q);
        for(int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
        }
        build(1,n,1);
        while(q--){
            char s;
            ll a,b,c;
            scanf(" %c",&s);
            if(s=='Q'){
                scanf("%lld %lld",&a,&b);
                ll ans=query(a,b,1,n,1);
                printf("%lld
    ",ans);
            }
            else{
                scanf("%lld %lld %lld",&a,&b,&c);
                update(a,b,c,1,n,1);
            }
        }
        return 0;
    }
  • 相关阅读:
    2-SAT
    CDQ分治
    整体二分
    未完成
    [BZOJ1857][SCOI2010]传送带-[三分]
    [LCT应用]
    [胡泽聪 趣题选讲]大包子环绕宝藏-[状压dp]
    [清华集训2015 Day2]矩阵变换-[稳定婚姻模型]
    [清华集训2015 Day1]主旋律-[状压dp+容斥]
    [清华集训2015 Day1]玛里苟斯-[线性基]
  • 原文地址:https://www.cnblogs.com/skyleafcoder/p/12319547.html
Copyright © 2020-2023  润新知