竹丝码迹

  • 首页
  • 技术分享
    • 流媒体音视频
    • Note
    • Technology
    • System
    • Code
    • Toys
  • 友情链接
    • LeetCode
    • OSCHINA
    • 牛客网
  • 关于博主
纸上得来终觉浅,绝知此事要躬行
  1. 首页
  2. Note
  3. 正文

树状数组模板

2019年11月15日 341点热度 1人点赞 0条评论

树状数组

参考博客 https://blog.csdn.net/bestsort/article/details/80796531

博主 https://bestsort.cn/

单点更新 区间查询

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;

#define maxn 2000010
#define lowbit(a) ((a) & (-a))
int n, m, tree[maxn];

void add(int x, int k) {
    while (x <= n) tree[x] += k, x += lowbit(x);
    return ;
}

int get_sum(int x) {
    int ret = 0;
    while (x != 0) ret += tree[x], x -= lowbit(x);
    return ret;
}

int main() {
    int a, b, c;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a;
        add(i, a);
    }
    for (int i = 0; i < m; i++) {
        cin >> a >> b >> c;
        if (a == 1) add(b, c);
        else cout << get_sum(c) - get_sum(b - 1) << endl;
    }
    return 0;
}

区间更新 单点查询

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;

#define maxn 2000010
#define lowbit(a) ((a) & (-a))
int n, m;
int tree[maxn];

void add(int x, int k) { //这个函数用来在树状数组中直接修改
    while(x <= n) tree[x] += k, x += lowbit(x);
}
void range_add(int l, int r, int k) { //给区间[l, r]加上x
    add(l, k), add(r + 1, -k);
}
int ask(int x) { //单点查询
    int res = 0;
    while(x) res += tree[x], x -= lowbit(x);
    return res;
}

int main() {
    int op, x, y, z, last = 0, now;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) { // 差分思想 
        scanf("%d", &now);
        add(i, now - last);
        last = now;
    }
    for (int i = 1; i <= m; i++) {
        cin >> op;
        if (op == 1) {
            cin >> x >> y >> z;
            range_add(x, y, z);
        } else {
            cin >> x;
            cout << ask(x) << endl;
        }
    }
    return 0;
}

区间修改 区间查询

#include <iostream>

using namespace std;

#define lowbit(a) ((a) & (-a))
#define maxn 2000010
typedef long long LL;
int n, m;
LL sum1[maxn], sum2[maxn];

void add(LL x, LL k) {
    for(LL i = x; i <= n; i += lowbit(i)) {
        sum1[i] += k, sum2[i] += x * k;
    }
}
void range_add(LL l, LL r, LL x) {
    add(l, x), add(r + 1, -x);
}
LL ask(LL x) {
    LL res = 0;
    for (LL i = x; i; i -= lowbit(i)) {
        res += (x + 1) * sum1[i] - sum2[i];
    }
    return res;
}
LL range_ask(LL l, LL r) {
    return ask(r) - ask(l - 1);
}

int main() {
    int op, x, y, z;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> x;
        range_add(i, i, x);
    }
    for (int i = 0; i < m; i++) {
        cin >> op;
        if (op == 1) {
            cin >> x >> y >> z;
            range_add(x, y, z);
        } else {
            cin >> x >> y;
            cout << range_ask(x, y) << endl;
        }
    }
    return 0;
}
本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: 暂无
最后更新:2019年11月17日

ronnie

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

取消回复
最新 热点 随机
最新 热点 随机
H264基础 YUV入门基础 编解码基础 WebRTC入门 Bat脚本基础入门 只需三步便可下载无水印图片
WebRTC入门H264基础多线程与多进程STL简记C++学习历程基于C语言的Socket编程
存储任意类型的链表 树状数组模板 【shell脚本】自制垃圾桶 P1803凌乱的yyy / 线段覆盖 HZOJ_354 Coins题解 高精度模板
分类目录
标签聚合
分治 单调队列 WebRTC 操作系统 归并排序 多重背包 数学 贪心 树状数组 素数 动态规划 离散化 编解码 SPFA
目录
  • 1 树状数组
    • 1.1 单点更新 区间查询
    • 1.2 区间更新 单点查询
    • 1.3 区间修改 区间查询

COPYRIGHT © 2021 竹丝码迹. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

黑ICP备19006658号