zoukankan      html  css  js  c++  java
  • POJ

    题意:区间add,区间求和。

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define lowbit(x) (x & (-x))
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 100000 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    LL a[MAXN];
    LL sum[MAXN << 2];
    LL lazy[MAXN << 2];
    void build(int id, int L, int R){
        if(L == R){
            sum[id] = a[L];
        }
        else{
            int mid = L + (R - L) / 2;
            build(id << 1, L, mid);
            build(id << 1 | 1, mid + 1, R);
            sum[id] = sum[id << 1] + sum[id << 1 | 1];
        }
    }
    void pushdown(int id, int L, int R){
        if(lazy[id]){
            lazy[id << 1] += lazy[id];
            lazy[id << 1 | 1] += lazy[id];
            int mid = L + (R - L) / 2;
            sum[id << 1] += (mid - L + 1) * lazy[id];
            sum[id << 1 | 1] += (R - mid) * lazy[id];
            lazy[id] = 0;
        }
    }
    void update(int l, int r, int id, int L, int R, LL v){
        if(l <= L && R <= r){
            lazy[id] += v;
            sum[id] += (R - L + 1) * v;
        }
        else{
            pushdown(id, L, R);
            int mid = L + (R - L) / 2;
            if(l <= mid) update(l, r, id << 1, L, mid, v);
            if(r > mid) update(l, r, id << 1 | 1, mid + 1, R, v);
            sum[id] = sum[id << 1] + sum[id << 1 | 1];
        }
    }
    LL query(int l, int r, int id, int L, int R){
        if(l <= L && R <= r){
            return sum[id];
        }
        pushdown(id, L, R);
        int mid = L + (R - L) / 2;
        LL ans = 0;
        if(l <= mid) ans += query(l, r, id << 1, L, mid);
        if(r > mid) ans += query(l, r, id << 1 | 1, mid + 1, R);
        return ans;
    }
    int main(){
        int N, Q;
        scanf("%d%d", &N, &Q);
        for(int i = 1; i <= N; ++i){
            scanf("%lld", &a[i]);
        }
        build(1, 1, N);
        while(Q--){
            char cc;
            int a, b;
            getchar();
            scanf("%c%d%d", &cc, &a, &b);
            if(cc == 'C'){
                int c;
                scanf("%d", &c);
                update(a, b, 1, 1, N, (LL)c);
            }
            else{
                printf("%lld
    ", query(a, b, 1, 1, N));
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    centos7安装supervisor
    redis4.0 cluster搭建
    网易cetus数据库中间件安装-读写分离版本
    mongodb副本集基于centos7部署
    C# 单例模式实现
    HttpWebRequest的GET和POST方法
    C#中$的用法
    判断一个表是否存在
    C# 继承的一些解释
    C# 虚方法和抽象方法
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/7275509.html
Copyright © 2011-2022 走看看