• 洛谷 P3372 【模板】线段树 1


    传送门

    Version 1: 线段树

    #include <iostream>
    
    using namespace std;
    using ll = long long;
    using p = pair<int, int>;
    const int maxn(1e5 + 10);
    
    struct node {
        int l, r;
        ll sum, lz;
    } tree[maxn << 2];
    
    template<typename T = int>
    inline const T read()
    {
        T x = 0, f = 1;
        char ch = getchar();
        while (ch < '0' || ch > '9') {
            if (ch == '-') f = -1;
            ch = getchar();
        }
        while (ch >= '0' && ch <= '9') {
            x = (x << 3) + (x << 1) + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    
    template<typename T>
    inline void write(T x, bool ln)
    {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9) write(x / 10, false);
        putchar(x % 10 + '0');
        if (ln) putchar(10);
    }
    
    inline int ls(int cur)
    {
        return cur << 1;
    }
    
    inline int rs(int cur)
    {
        return cur << 1 | 1;
    }
    
    void push_up(int cur)
    {
        tree[cur].sum = tree[ls(cur)].sum + tree[rs(cur)].sum;
    }
    
    void push_down(int cur)
    {
        if (tree[cur].lz) {
            tree[ls(cur)].sum += (tree[ls(cur)].r - tree[ls(cur)].l + 1) * tree[cur].lz;
            tree[rs(cur)].sum += (tree[rs(cur)].r - tree[rs(cur)].l + 1) * tree[cur].lz;
            tree[ls(cur)].lz += tree[cur].lz;
            tree[rs(cur)].lz += tree[cur].lz;
            tree[cur].lz = 0;
        }
    }
    
    void build(int cur, int l, int r)
    {
        tree[cur].l = l;
        tree[cur].r = r;
        tree[cur].lz = 0;
        if (l == r) {
            tree[cur].sum = read();
            return;
        }
        int mid = (l + r) >> 1;
        build(ls(cur), l, mid);
        build(rs(cur), mid + 1, r);
        push_up(cur);
    }
    
    void update(int cur, int l, int r, int v)
    {
        if (tree[cur].l == l and tree[cur].r == r) {
            tree[cur].sum += (r - l + 1) * v;
            tree[cur].lz += v;
            return;
        }
        push_down(cur);
        int mid = (tree[cur].l + tree[cur].r) >> 1;
        if (r <= mid) {
            update(ls(cur), l, r, v);
        } else if (l > mid) {
            update(rs(cur), l, r, v);
        } else {
            update(ls(cur), l, mid, v);
            update(rs(cur), mid + 1, r, v);
        }
        push_up(cur);
    }
    
    ll query(int cur, int l, int r)
    {
        if (tree[cur].l == l and tree[cur].r == r) {
            return tree[cur].sum;
        }
        push_down(cur);
        int mid = (tree[cur].l + tree[cur].r) >> 1;
        if (r <= mid) return query(ls(cur), l, r);
        if (l > mid) return query(rs(cur), l, r);
        return query(ls(cur), l, mid) + query(rs(cur), mid + 1, r);
    }
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("input.txt", "r", stdin);
    #endif
        int n = read(), m = read();
        build(1, 1, n);
        while (m--) {
            int t = read(), x = read(), y = read();
            if (t == 1) {
                int k = read();
                update(1, x, y, k);
            } else {
                write(query(1, x, y), true);
            }
        }
        return 0;
    }
    

    Version 2: 树状数组

    庄佬的巧妙推导

    #include <bits/stdc++.h>
    
    using namespace std;
    using ll = long long;
    using p = pair<int, int>;
    const int maxn(1e5 + 10);
    int a[maxn];
    ll c[maxn][2];
    
    template<typename T = int>
    inline const T read()
    {
        T x = 0, f = 1;
        char ch = getchar();
        while (ch < '0' || ch > '9') {
            if (ch == '-') f = -1;
            ch = getchar();
        }
        while (ch >= '0' && ch <= '9') {
            x = (x << 3) + (x << 1) + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    
    template<typename T>
    inline void write(T x, bool ln)
    {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9) write(x / 10, false);
        putchar(x % 10 + '0');
        if (ln) putchar(10);
    }
    
    inline int lowbit(int x)
    {
        return x & -x;
    }
    
    void add(int t, int p, int n, ll v)
    {
        for (int i = p; i <= n; i += lowbit(i)) {
            c[i][t] += v;
        }
    }
    
    ll getSum(int t, int p)
    {
        ll sum = 0;
        for (int i = p; i; i -= lowbit(i)) {
            sum += c[i][t];
        }
        return sum;
    }
    
    void update(int l, int r, int n, int v)
    {
        add(0, l, n, v);
        add(0, r + 1, n, -v);
        add(1, l, n, 1ll * v * (l - 1));
        add(1, r + 1, n, -1ll * v * r);
    }
    
    ll query(int l, int r)
    {
        ll sum_r = 1ll * r * getSum(0, r) - getSum(1, r);
        ll sum_l = 1ll * (l - 1) * getSum(0, l - 1) - getSum(1, l - 1);
        return sum_r - sum_l;
    }
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("input.txt", "r", stdin);
    #endif
        int n = read(), m = read();
        for (int i = 1; i <= n; ++i) {
            a[i] = read();
            add(0, i, n, a[i] - a[i - 1]);
            add(1, i, n, 1ll * (a[i] - a[i - 1]) * (i - 1));
        }
        while (m--) {
            int op = read(), x = read(), y = read();
            if (op == 1) {
                int k = read();
                update(x, y, n, k);
            } else {
                write(query(x, y), true);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    django 1.9.7 css控制模板样式
    python -- 判断给定的参数是否是地理位置的经度和纬度
    有恃无恐
    不知不觉
    vscode 编写Python走过的坑
    Python的从头再来
    【初赛】数学题错题总结
    【初赛】选择题总结
    20175317 《Java程序设计》第四周学习总结
    20175317 《Java程序设计》第三周学习总结
  • 原文地址:https://www.cnblogs.com/singularity2u/p/13997477.html
Copyright © 2020-2023  润新知