[Luogu] 树状数组
https://www.luogu.org/problemnew/show/P3374
单点修改,区间查询
#include <iostream>
#include <cstdio> using namespace std;
const int N = 5e5 + ; #define yxy getchar() int T[N], n, Ty; inline int read() {
int x = , f = ; char c = yxy;
while(c < '' || c > '') {if(c == '-') f = -; c = yxy;}
while(c >= '' && c <= '') x = x * + c - '', 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 = ;
while(x) {
ret += T[x];
x -= lowbit(x);
} return ret;
} int main() {
n = read();
Ty = read();
for(int i = ; i <= n; i ++) {
int x = read();
Add(i, x);
}
while(Ty --) {
int opt = read(), x = read(), y = read();
if(opt == ) Add(x, y);
else cout << Sum(y) - Sum(x - ) << endl;
} return ;
}
https://www.luogu.org/problemnew/show/P3368
区间修改,单点查询
#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std;
const int N = 5e5 + ; #define yxy getchar() int T[N], A[N];
int n, Ty; inline int read() {
int x = , f = ;
char c = yxy;
while(c < '' || c > '') {
if(c == '-') f = -;
c = yxy;
}
while(c >= '' && c <= '') x = x * + c - '', 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 = ;
while(x) {
ret += T[x];
x -= lowbit(x);
} return ret;
} int main() {
n = read();
Ty = read();
for(int i = ; i <= n; i ++) A[i] = read();
while(Ty --) {
int opt = read();
if(opt == ) {
int x = read(), y = read(), k = read();
Add(x, k); Add(y + , -k);
} else {
int x = read();
cout << A[x] + Sum(x) << endl;
}
}
return ;
}
P2068 统计和
https://www.luogu.org/problemnew/show/P2068
#include <iostream>
#include <cstdio> using namespace std;
const int N = 1e5 + ; #define yxy getchar()
#define R freopen("gg.in", "r", stdin) int T[N], n, Ty; inline int read() {
int x = , f = ; char c = yxy;
while(c < '' || c > '') {if(c == '-') f = -; c = yxy;}
while(c >= '' && c <= '') x = x * + c - '', 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 = ;
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 - ) << endl;
}
return ;
}
[Luogu] 树状数组的更多相关文章
- Luogu 45887 全村最好的嘤嘤刀(线段树 树状数组)
https://www.luogu.org/problemnew/show/T45887 题目背景 重阳节到了,我们最好的八重樱拥有全村最好的嘤嘤刀…… 题目描述 在绯玉丸力量的影响下,八重村成了一条 ...
- Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分)
Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分) Description L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之 ...
- luogu P3031 [USACO11NOV]高于中位数Above the Median (树状数组优化dp)
链接:https://www.luogu.org/problemnew/show/P3031 题面: 题目描述 Farmer John has lined up his N (1 <= N &l ...
- [luogu]P2657低头一族[树状数组]
[luogu]P2657 低头一族 题目描述 一群青年人排成一队,用手机互相聊天. 每个人的手机有一个信号接收指标,第i个人的接收指标设为v[i]. 如果位置在x[i]的人要和位置在xj的人聊天,那么 ...
- [BZOJ 3295] [luogu 3157] [CQOI2011]动态逆序对(树状数组套权值线段树)
[BZOJ 3295] [luogu 3157] [CQOI2011] 动态逆序对 (树状数组套权值线段树) 题面 给出一个长度为n的排列,每次操作删除一个数,求每次操作前排列逆序对的个数 分析 每次 ...
- 【Luogu P5168】xtq玩魔塔(Kruskal 重构树 & 树状数组 & set)
Description 给定一个 \(n\) 个顶点,\(m\) 条边的无向联通图,点.边带权. 先有 \(q\) 次修改或询问,每个指令形如 \(\text{opt}\ x\ y\): \(\tex ...
- [luogu P3801] 红色的幻想乡 [线段树][树状数组]
题目背景 蕾米莉亚的红雾异变失败后,很不甘心. 题目描述 经过上次失败后,蕾米莉亚决定再次发动红雾异变,但为了防止被灵梦退治,她决定将红雾以奇怪的阵势释放. 我们将幻想乡看做是一个n*m的方格地区,一 ...
- [luogu P2184] 贪婪大陆 [树状数组][线段树]
题目背景 面对蚂蚁们的疯狂进攻,小FF的Tower defence宣告失败……人类被蚂蚁们逼到了Greed Island上的一个海湾.现在,小FF的后方是一望无际的大海, 前方是变异了的超级蚂蚁. 小 ...
- [luogu P3787][新创无际夏日公开赛] 冰精冻西瓜 [树状数组][dfs序]
题目背景 盛夏,冰之妖精琪露诺发现了一大片西瓜地,终于可以吃到美味的冻西瓜啦. 题目描述 琪露诺是拥有操纵冷气程度的能力的妖精,一天她发现了一片西瓜地.这里有n个西瓜,由n-1条西瓜蔓连接,形成一个有 ...
随机推荐
- (十五)Activitivi5之多用户任务分配
一.概念 我们在开发的时候,有一种情况是这样的, 我们有一个任务,可以让多个用户中的任何一个人办理即可,比如某个审批任务, 张三,李四,王五他们中的任何一人办理下都行,这时候,我们用到多用户任务分配. ...
- ( 十二)Json的使用(上)
一.什么是JSON 1.1 javaScript中定义对象的几种方式 使用内置对象 自定义对象构造 使用JSON符号 (1)使用内置对象 JavaScript可用的内置对象可分为两种: 1,Java ...
- winform PictureBox图片上动态添加Label或其他控件
效果图: 代码: //分页或者多次加载时,需要删除之前产生的lable等控件 ; tabID < ; tabID++) { foreach (Control control in this.ta ...
- 二、运行时JVM结构组成及作用
二.运行时JVM结构组成及作用 程序计数器 是否共享:否,线程私有,每个线程有1个独立的程序计数器! 所处位置:线程私有的内部区域 生命周期:与线程绑定 主要作用: 当前线程执行字节码的行号指示器! ...
- was控制台无法停止应用
问题描述: was控制台无法停止应用,只能通过停止server的方式停止: 代码实现: import org.slf4j.Logger; import org.slf4j.LoggerFactory; ...
- Vue路由嵌套
Vue路由嵌套 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- [Vuex系列] - Vuex中的getter的用法
Vuex 允许我们在store中定义“getter”(可以认为是store的计算属性).就像计算属性一样,getter的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算. g ...
- 为什么加了jquery mobile 会有 Loading 字样在页面底部?【已解决】
这是一个奇怪的问题,用了jquery mobile js库,页面底部就会出现Loading字样, 解决办法如下: 1,正常加上css样式 2,一定要在jquery mobile js库加载之前,设置 ...
- Axure流程图
什么是流程图 一个流程图可用于展示各种各样的处理流程,包括用例流程.商业流程.页面流程等.在Axure中,流程图常用于提供一个高保真的.能通过所设计的页面来完成的任务视图.一张简明的流程图,能促进和其 ...
- Linux网络管路——网络相关命令ping、traceroute
ping [root@51cto /]# ping www.baidu.com PING www.a.shifen.com (() bytes of data. bytes from ttl= tim ...