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] 树状数组的更多相关文章

  1. Luogu 45887 全村最好的嘤嘤刀(线段树 树状数组)

    https://www.luogu.org/problemnew/show/T45887 题目背景 重阳节到了,我们最好的八重樱拥有全村最好的嘤嘤刀…… 题目描述 在绯玉丸力量的影响下,八重村成了一条 ...

  2. Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分)

    Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分) Description L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之 ...

  3. luogu P3031 [USACO11NOV]高于中位数Above the Median (树状数组优化dp)

    链接:https://www.luogu.org/problemnew/show/P3031 题面: 题目描述 Farmer John has lined up his N (1 <= N &l ...

  4. [luogu]P2657低头一族[树状数组]

    [luogu]P2657 低头一族 题目描述 一群青年人排成一队,用手机互相聊天. 每个人的手机有一个信号接收指标,第i个人的接收指标设为v[i]. 如果位置在x[i]的人要和位置在xj的人聊天,那么 ...

  5. [BZOJ 3295] [luogu 3157] [CQOI2011]动态逆序对(树状数组套权值线段树)

    [BZOJ 3295] [luogu 3157] [CQOI2011] 动态逆序对 (树状数组套权值线段树) 题面 给出一个长度为n的排列,每次操作删除一个数,求每次操作前排列逆序对的个数 分析 每次 ...

  6. 【Luogu P5168】xtq玩魔塔(Kruskal 重构树 & 树状数组 & set)

    Description 给定一个 \(n\) 个顶点,\(m\) 条边的无向联通图,点.边带权. 先有 \(q\) 次修改或询问,每个指令形如 \(\text{opt}\ x\ y\): \(\tex ...

  7. [luogu P3801] 红色的幻想乡 [线段树][树状数组]

    题目背景 蕾米莉亚的红雾异变失败后,很不甘心. 题目描述 经过上次失败后,蕾米莉亚决定再次发动红雾异变,但为了防止被灵梦退治,她决定将红雾以奇怪的阵势释放. 我们将幻想乡看做是一个n*m的方格地区,一 ...

  8. [luogu P2184] 贪婪大陆 [树状数组][线段树]

    题目背景 面对蚂蚁们的疯狂进攻,小FF的Tower defence宣告失败……人类被蚂蚁们逼到了Greed Island上的一个海湾.现在,小FF的后方是一望无际的大海, 前方是变异了的超级蚂蚁. 小 ...

  9. [luogu P3787][新创无际夏日公开赛] 冰精冻西瓜 [树状数组][dfs序]

    题目背景 盛夏,冰之妖精琪露诺发现了一大片西瓜地,终于可以吃到美味的冻西瓜啦. 题目描述 琪露诺是拥有操纵冷气程度的能力的妖精,一天她发现了一片西瓜地.这里有n个西瓜,由n-1条西瓜蔓连接,形成一个有 ...

随机推荐

  1. MySQL explain功能展示的各种信息的解释

    1.id: MySQL Query Optimizer 选定的执行计划中查询的序列号. 2.select_type: 所使用的查询类型,主要有以下这几种查询类型.  DEPENDENT SUBQUER ...

  2. docker部署Eurake服务,服务节点无法注册服务

    前言 昨天在部署一个docker项目时遇到了一个问题,故记录下来. 环境说明 Centos7 Docker version 18.06.3-ce, build d7080c1 问题说明 该项目分别启动 ...

  3. Android 音频播放速率调整实现

    最近接触到的一个项目, 有音频播放.切换播放速率和拖动进度到某处播放的需求 ,由于之前只是见过并没有尝试过切换播放速率 , 于是开始调研并最终实现,下面简单记录一下这次的调研过程. MediaPlay ...

  4. QCamera : no such file 问题

    在项目 *.pro中添加即可. QT += core gui QT += multimedia QT += multimediawidgets QT += multimedia ==>对应< ...

  5. Maven 三种archetype说明--转载

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 原文链接:https://blog.csdn.net/cx1110162/article/deta ...

  6. Go语言——值方法 & 指针方法

    1 package main import ( "fmt" "sort" ) type SortableStrings []string type Sortab ...

  7. IntelliJ IDEA详细配置和使用教程(适用于Java开发人员)

    关闭Intellij IDEA自动更新在File->Settings->Appearance & Behavior->System Settings->Updates下 ...

  8. Spark(二)算子详解

    目录 Spark(二)算子讲解 一.wordcountcount 二.编程模型 三.RDD数据集和算子的使用 Spark(二)算子讲解 @ 一.wordcountcount 基于上次的wordcoun ...

  9. PAT Basic 1074 宇宙无敌加法器 (20 分)

    地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的.而在 PAT 星人开挂的世界里,每个数字的每一位都是不同进制的,这种神奇的数字称为“PAT数”.每个 PAT 星人都必须熟记各位数字的进制 ...

  10. cas的客户端配置

    知识点:cas的客户端配置 一:cas客户端配置 二:cas认证流程原理(图) 参考:https://www.cnblogs.com/suiyueqiannian/p/9359597.html 源码: ...