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;
    }
    

      

  • 相关阅读:
    Ubuntu16.04安装openldap和phpldapadmin
    Java 8 中的抽象类和接口到底有啥区别?
    Redis 开发陷阱及避坑指南!
    Java 中的 6 颗语法糖
    Java 8 有多牛逼?打破一切你对接口的认知!
    Git操作常用的命令都在这里了。
    Github 太狠了,居然把 "master" 干掉了!
    微服务业务日志收集方案,写得非常好!
    Maven基本介绍与安装
    IntelliJ IDEA 调试 Java 8 Stream,实在太香了!
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/7275509.html
Copyright © 2011-2022 走看看