当前位置:网站首页>Basic usage of tree arrays
Basic usage of tree arrays
2022-07-30 08:05:00 【Rounding up the two-meter-high Xiaochen】
1.The usefulness of dendritic array:
- In a certain position of the array with a number
- 求某一个前缀和
- Is the most important of these are dynamically calculated
2.The composition of dendritic array
A tree each element of the array is composed of a range of,例如TR[i]是由(x-lowbit(x),x]构成.(Note left on the right off)
3.Write the core of the dendritic array code
int lowbit(int x)
{
return x&-x;
}
//第x个数加上v
int add(int x,int v)
{
//Starting from the current position and,Each interval islowbit(i),Every time I found it on the above number,直到n
for(int i=x;i<=n;i+=lowbit(i))
tr[i]+=v;
}
//返回x的前缀和
int query(int x)
{
int res=0;
//Starting from the current position accumulation,Each interval islowbit(i),一直减到i==0停止
for(int i=x;i;i-=lowbit(i)) res+=tr[i];
return res;
}
4.例题:
代码:
// 时间:2022.07.26 18点07分
// 算法:树状数组
#include<iostream>
using namespace std;
const int N=100010;
int a[N],tr[N];
int n,m;
int lowbit(int x)
{
return x & -x;
}
void add(int x,int v)
{
for(int i=x;i<=n;i+=lowbit(i)) tr[i]+=v;
}
int query(int x)
{
int res=0;
for(int i=x;i;i-=lowbit(i)) res+=tr[i];
return res;
}
int main ()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) add(i,a[i]);
while(m--)
{
int k,x,y;
scanf("%d%d%d",&k,&x,&y);
if(k==0) printf("%d\n",query(y)-query(x-1));
else add(x,y);
}
return 0;
}
边栏推荐
- 【COCI 2020/2021 Round #2 D】Magneti(DP)
- Equation Derivation Proof of Vector Triple Product
- 【雷达目标检测】恒定阈值法和恒虚警(CFAR)法及代码实现
- 这个终端连接工具,碾压Xshell
- [硬核干货]由0到1,突破信息系统项目管理师(呕心沥血经验之谈)!!!
- Universal js time date format conversion
- Electron之初出茅庐——搭建环境并运行第一个程序
- 便携小风扇PD取电芯片
- go : go gin returns JSON data
- 2020 ACM | MoFlow: An Invertible Flow Model for Generating Molecular Graphs
猜你喜欢

2020 数学建模之旅

树状数组的基本用法
![[硬核干货]由0到1,突破信息系统项目管理师(呕心沥血经验之谈)!!!](/img/9a/f3e4bdd0ce8ec153a8e6bdbff5647e.jpg)
[硬核干货]由0到1,突破信息系统项目管理师(呕心沥血经验之谈)!!!

Huawei released "ten inventions", including computing, intelligent driving and other new fields

Proof of distance calculation from space vertex to plane and its source code

MySQL master-slave replication configuration construction, one step in place

专访蚂蚁:这群技术排头兵,如何做好底层开发这件事?| 卓越技术团队访谈录
获取controller中所有接口路径和名称

Go combines Gin to export Mysql data to Excel table

Equation Derivation Proof of Vector Triple Product
随机推荐
AI可通过X光片识别种族,但没人知道为什么
阿里二面:Sentinel vs Hystrix 对比,如何选择?
go : go-redis list操作
C#的访问修饰符,声明修饰符,关键字有哪些?扫盲篇
解决datagrip连接sqlserver报错:[08S01] 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。
Go: use gorm query record
[GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及
Keil编译大小和存储说明
go : 使用gorm创建数据库记录
How to use Swagger, say goodbye to postman
roslyn folder under bin folder
go : go-redis set operations
MySQL题外篇【ORM思想解析】
Mybitatis相关配置文件
Go 使用 freecache 缓存
Table with tens of millions of data, how to query the fastest?
IDEA 中CheckStyle安装及使用
【MySQL】MySQL中如何实现分页操作
云服务器零基础部署网站(保姆级教程)
进制转换。。。