当前位置:网站首页>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;
}
边栏推荐
- 云服务器零基础部署网站(保姆级教程)
- Goto statements
- Go 结合Gin导出Mysql数据到Excel表格
- Derivative Operations on Vectors and Derivative Operations on Vector Cross and Dot Products
- DP5340国产替代CM5340立体声音频A/D转换器芯片
- Calculate the inverse source of the matrix (using the adjoint matrix, a 3x3 matrix)
- MYSQL下载及安装完整教程
- 理解和熟悉递归中的尝试
- What new materials are used in the large aircraft C919?
- 使用navicat连接mysql数据库时常报的错误:2003、1698、1251
猜你喜欢

Is it possible to use the same port for UDP and TCP?

Electron之初出茅庐——搭建环境并运行第一个程序

selenium模块

Keil软件中map文件解析

LVM and disk quotas

The calculation and source code of the straight line intersecting the space plane

首届人工智能安全大赛正式启动

如何实时计算日累计逐单资金流
![[GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及](/img/ac/80ab67505f7df52d92a206bc3dd50e.png)
[GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及
![[GO Language Basics] 1. Why do I want to learn Golang and get started with GO language](/img/ac/80ab67505f7df52d92a206bc3dd50e.png)
[GO Language Basics] 1. Why do I want to learn Golang and get started with GO language
随机推荐
万能js时间日期格式转换
sizeof
export , export default, import complete usage
如何实时计算日累计逐单资金流
MySQL题外篇【ORM思想解析】
Keil compile size and storage instructions
How does Redis prevent oversold and inventory deduction operations?
Hex conversion...
C#的访问修饰符,声明修饰符,关键字有哪些?扫盲篇
Table with tens of millions of data, how to query the fastest?
使用navicat连接mysql数据库时常报的错误:2003、1698、1251
taro 打包编译报错
Oracle查看表空间使用率及爆满解决方案
golang: Gorm配置Mysql多数据源
No, the Log4j vulnerability hasn't been fully fixed yet?
go : go-redis 基础操作
Huawei released "ten inventions", including computing, intelligent driving and other new fields
go : 使用gorm查询记录
assert
MySQL基础篇【命名规范】