• [Luogu] 树状数组


    https://www.luogu.org/problemnew/show/P3374

    单点修改,区间查询

    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    const int N = 5e5 + 10;
    
    #define yxy getchar()
    
    int T[N], n, Ty;
    
    inline int read() {
        int x = 0, f = 1; char c = yxy;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = yxy;}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = yxy;
        return x * f;
    }
    
    int lowbit(int x) {return x & (- x);}
    
    inline void Add(int id, int num) {
        while(id <= n) {
            T[id] += num;
            id += lowbit(id);
        }
    }
    
    inline int Sum(int x) {
        int ret = 0;
        while(x) {
            ret += T[x];
            x -= lowbit(x);
        } return ret;
    }
    
    int main() {
        n = read();
        Ty = read();
        for(int i = 1; i <= n; i ++) {
            int x = read();
            Add(i, x);
        }
        while(Ty --) {
            int opt = read(), x = read(), y = read();
            if(opt == 1) Add(x, y);
            else cout << Sum(y) - Sum(x - 1) << endl;
        }
        
        return 0;
    }

    https://www.luogu.org/problemnew/show/P3368

    区间修改,单点查询

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    
    using namespace std;
    const int N = 5e5 + 10;
    
    #define yxy getchar()
    
    int T[N], A[N];
    int n, Ty;
    
    inline int read() {
        int x = 0, f = 1;
        char c = yxy;
        while(c < '0' || c > '9') {
            if(c == '-') f = -1;
            c = yxy;
        }
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = yxy;
        return x * f;
    }
    
    inline int lowbit(int x) {
        return x & - x;
    }
    
    inline void Add(int x, int num) {
        while(x <= n) {
            T[x] += num;
            x += lowbit(x);
        }
    }
    
    inline int Sum(int x) {
        int ret = 0;
        while(x) {
            ret += T[x];
            x -= lowbit(x);
        } return ret;
    }
    
    int main() {
        n = read();
        Ty = read();
        for(int i = 1; i <= n; i ++) A[i] = read();
        while(Ty --) {
            int opt = read();
            if(opt == 1) {
                int x = read(), y = read(), k = read();
                Add(x, k); Add(y + 1, -k);
            } else {
                int x = read();
                cout << A[x] + Sum(x) << endl;
            }
        }
        return 0;
    }

    P2068 统计和

    https://www.luogu.org/problemnew/show/P2068

    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    const int N = 1e5 + 10;
    
    #define yxy getchar()
    #define R freopen("gg.in", "r", stdin)
    
    int T[N], n, Ty;
    
    inline int read() {
        int x = 0, f = 1; char c = yxy;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = yxy;}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = yxy;
        return x * f;
    }
    
    inline int lowbit(int x) {
        return x & (-x);
    }
    
    inline void Add(int x, int y) {
        while(x <= n) {
            T[x] += y;
            x += lowbit(x);
        }
    }
    
    inline int Sum(int x){
        int ret = 0;
        while(x) {
            ret += T[x];
            x -= lowbit(x);
        } return ret;
    }
    
    int main() {
        n = read();
        Ty = read();
        while(Ty --) {
            char c = yxy;
            int x = read(), y = read();
            if(c == 'x') Add(x, y);
            else cout << Sum(y) - Sum(x - 1) << endl;
        }
        return 0;
    }
  • 相关阅读:
    HTTP 错误 404.13
    C# 文件操作(全部) 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名
    设计模式---装饰模式(Decorator)
    设计模式---订阅发布模式(Subscribe/Publish)
    Merge into 详细介绍
    优化案例--多语句表值函数的影响
    常用脚本--Kill所有连接到指定数据库上的回话
    常用脚本--查看当前锁信息
    常用脚本--查看死锁和阻塞usp_who_lock
    常用脚本--在线重建或重整实例下所有索引
  • 原文地址:https://www.cnblogs.com/shandongs1/p/8422295.html
Copyright © 2020-2023  润新知