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

转载于:https://www.cnblogs.com/shandongs1/p/8422295.html

[Luogu] 树状数组相关推荐

  1. Luogu P4178 Tree (点分治 + 树状数组)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Weblink https://www.luogu.com.cn/problem/P4178 Prob ...

  2. 【DP】【树状数组】方伯伯的玉米田/优美玉米(luogu 3287/金牌导航 数据结构优化DP-5)

    正题 luogu 3287 金牌导航 数据结构优化DP-5 题目大意 有n个玉米,给出高度,你可以选择一个区间,使这个区间的玉米高度+1,你可以进行k次这样的操作,查询你操作完后最长不下降子序列最大值 ...

  3. 【DP】【树状数组】免费馅饼(luogu 7302/金牌导航 数据结构优化DP-4)

    正题 luogu 7302 金牌导航 数据结构优化DP-4 题目大意 在坐标轴上会出现n个金币,第i个金币tit_iti​时在wiw_iwi​出现(只出现一个单位时间),价值为sis_isi​,当你t ...

  4. 【LCT】【树状数组】Matches Are Not a Child‘s Play(luogu CF1137F)

    正题 luogu CF1137F 题目大意 定义一棵树的产出序列为依次删除权值最小的叶子节点的顺序 进行q此操作: 1.把一个点的权值改为当前树中的最大权值+1 2.查询一个点在删除序列中的位置 3. ...

  5. luogu 2717 寒假作业 (树状数组)

    题目背景 zzs和zzy正在被寒假作业折磨,然而他们有答案可以抄啊. 题目描述 他们共有n项寒假作业.zzy给每项寒假作业都定义了一个疲劳值Ai,表示抄这个作业所要花的精力.zzs现在想要知道,有多少 ...

  6. 【luogu P4183】Cow at Large P(点分治)(图论)(树状数组)

    Cow at Large P 题目链接:luogu P4183 题目大意 给你一棵树,然后叶子节点可以放守卫. 然后有个人在树上,然后每个时刻那个人和守卫都可以移动,如果人和守卫相遇人就被抓了,如果人 ...

  7. 【Luogu】P3369 【模板】普通平衡树(树状数组)

    P3369 [模板]普通平衡树(树状数组) 一.树状数组 树状数组(Binary Indexed Tree(B.I.T), Fenwick Tree)是一个查询和修改复杂度都为log(n)的数据结构. ...

  8. BZOJ 2434 Luogu P2414 [NOI2011]阿狸的打字机 (AC自动机、树状数组)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2434 题解: 我写的是离线做法,不知道有没有在线做法. 转化一波题意,\(x\)在AC ...

  9. luogu P2344 奶牛抗议 DP 树状数组 离散化

    P2344 奶牛抗议 最新讨论 暂时没有讨论 题目背景 Generic Cow Protests, 2011 Feb 题目描述 约翰家的N 头奶牛正在排队游行抗议.一些奶牛情绪激动,约翰测算下来,排在 ...

  10. [luogu] P2345 MooFest G 树状数组

    前言 传送门 : 思路 很好的一个 树状数组入门题 我们可以通过树状数组维护 sumsumsum 和 cntcntcnt最后计算出答案 CODE struct node {int v,x;bool o ...

最新文章

  1. Linux环境编程--waitpid与fork与execlp
  2. Set常用用法元素检索
  3. 我是一个内向的人,也是一个不善交际的人
  4. spring_IOC_自動裝配
  5. MOSS 2010:Visual Studio 2010开发体验(15)——LINQ to SharePoint
  6. 重装系统后恢复oracle数据
  7. qq消息连发代码_我们用的QQ是怎样实现通讯的
  8. linux系统报警怎么办,常见Linux系统故障和解决方法
  9. 第一章 对象引论02
  10. 一个figure存在多个legend:多行多列图例
  11. Direct3D提高篇:HLSL编程实现PhotoShop滤镜效果 - 锐化模糊
  12. WEBPACK+ES6+REACT入门(4/7)-评论列表DEMO以及CSS样式
  13. Winform 表格布局
  14. java课程设计仓库管理系统_java课程设计仓库管理系统.doc
  15. 计算机专业论文要怎么写,计算机专业本科生论文摘要怎么写 计算机专业本科生论文摘要范文参考...
  16. JavaScript函数开关思想
  17. ICCV、ECCV、CVPR三大国际会议
  18. 投影仪怎么安装才能得到最大的屏幕?学会这个投影距离公式轻松拿捏
  19. 记一次vue2.0+vue-video-player实现hls播放全过程
  20. 人民币纸币采用防伪油墨

热门文章

  1. mysql base64的编码与解码
  2. kafka sqs 与_RabbitMQ和Kafka的比较
  3. go - range
  4. linux 网络编程 ------------TCP多进程并发服务器
  5. python写论文有帮助吗_python中有帮助函数吗
  6. Json文件转Map(三)之获取嵌套Map值
  7. 【渝粤教育】国家开放大学2018年秋季 2115T人体解剖学与组织胚胎学 参考试题
  8. [渝粤教育] 西南科技大学 机械设计基础 在线考试复习资料(1)
  9. [渝粤教育] 西南科技大学 婚姻家庭继承法 在线考试复习资料
  10. 【渝粤题库】陕西师范大学163212旅游地理学 作业(专升本)