• POJ3468(线段树区间增加,区间求和)


    A Simple Problem with Integers
    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 81519   Accepted: 25185
    Case Time Limit: 2000MS

    Description

    You have N integers, A1A2, ... , 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 A1A2, ... , 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.

    Source

     
    注意lazy标记的更新
    /* 
    * @Author: LinK
    * @Date:   2015-10-31 18:34:32
    * @Last Modified by:   LinK
    * @Last Modified time: 2015-10-31 23:11:18
    */
    
    #include <map>
    #include <set>
    #include <cmath>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <cstdio>
    #include <string>
    #include <utility>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define eps 1e-8
    #define randin srand((unsigned int)time(NULL))
    #define input freopen("input.txt","r",stdin)
    #define debug(s) cout << "s = " << s << endl;
    #define outstars cout << "*************" << endl;
    const double PI = acos(-1.0);
    const int inf = 0x3f3f3f3f;
    const int INF = 0x7fffffff;
    typedef long long ll;
    
    const int maxn = 100010;
    
    struct Node {
        ll sum, add;
    } tree[maxn << 2];
    
    int n, q;
    ll num[maxn];
    
    void build(int rt, int l, int r) {
        if (l == r) {
            tree[rt].sum = num[l];
            tree[rt].add = 0;
            return;
        }
        int mid = (l + r) >> 1;
        build(rt << 1, l, mid);
        build(rt << 1 | 1, mid + 1, r);
        tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
        tree[rt].add = 0;
    }
    
    void update(int rt, int l, int r, int L, int R, ll val) {
        if (L <= l && R >= r) {
            tree[rt].add += val;
            return;
        }
        tree[rt].sum += val * (min(R, r) - max(L, l) + 1);
        int mid = (l + r) >> 1;
        if (R <= mid) update(rt << 1, l, mid, L, R, val);
        else if (L > mid) update(rt << 1 | 1, mid + 1, r, L, R, val);
        else {
            update(rt << 1, l, mid, L, R, val);
            update(rt << 1 | 1, mid + 1, r, L, R, val);
        }
    }
    
    ll query(int rt, int l, int r, int L, int R) {
        if (L <= l && R >= r) {
            return tree[rt].sum + tree[rt].add * (r - l + 1);
        }
        int mid = (l + r) >> 1;
        if (tree[rt].add) {
            tree[rt].sum += (r - l + 1) * tree[rt].add;
            tree[rt << 1].add += tree[rt].add;
            tree[rt << 1 | 1].add += tree[rt].add;
            tree[rt].add = 0;
        }
        if (R <= mid) return query(rt << 1, l, mid, L, R);
        if (L > mid) return query(rt << 1 | 1, mid + 1, r, L, R);
        return query(rt << 1, l, mid, L, R) + query(rt << 1 | 1, mid + 1, r, L, R);
    }
    
    int main() {
        char op;
        int a, b;
        ll c;
        while (~scanf("%d %d", &n, &q)) {
            for (int i = 1; i <= n; i ++) scanf("%lld", &num[i]);
            build(1, 1, n);
            while (q --) {
                scanf(" %c %d %d", &op, &a, &b);
                if (op == 'Q') printf("%lld
    ", query(1, 1, n, a, b));
                else {
                    scanf("%lld", &c);
                    update(1, 1, n, a, b, c);
                } 
            }
        }
    
        return 0;
    }
  • 相关阅读:
    mysql-5.7(centos-6.9环境)源码安装
    Oracle 11g 体系结构思维导图
    my.cnf 配置文件参数优化
    职业生涯规划
    技能栈规划图
    操作文档 Oracle 11g (CentOS7.2环境) 静默安装
    oracle 静默安装响应文件 参数说明
    oracle em企业管理器的安装、配置及相关问题
    大数据第51天—Mysql练习题12道之六-今年10月份第一次购买商品的金额-杨大伟
    大数据第50天—Mysql练习题12道之五-活跃用户的总数-杨大伟
  • 原文地址:https://www.cnblogs.com/LinKArftc/p/4926523.html
Copyright © 2020-2023  润新知