当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

五号黯区靶场 mysql 注入之limit注入记录

AI can identify race from X-rays, but no one knows why

k8s 部署mysql8(PV和PVC 版本)

MySql详解基础
获取controller中所有接口路径和名称

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

mysql高阶语句(一)

uniapp中canvas与v-if更“配”

The terminal connection tools, rolling Xshell

2020 ACM | MoFlow: An Invertible Flow Model for Generating Molecular Graphs
随机推荐
export , export default,import完整用法
The introduction of AI meta-learning into neuroscience, the medical effect is expected to improve accurately
Electron中设置菜单(Menu),主进程向渲染进程共享数据
预测人们对你的第一印象,“AI颜狗”的诞生
MYSQL下载及安装完整教程
Mybitatis相关配置文件
适合程序员的输入法
C#的访问修饰符,声明修饰符,关键字有哪些?扫盲篇
Table with tens of millions of data, how to query the fastest?
和AI一起玩儿剧本杀:居然比我还入戏
Ali: How many methods are there for multi-threaded sequential operation?
架构设计指南 如何成为架构师
MySQL基础篇【命名规范】
如何实时计算日累计逐单资金流
go : 使用gorm修改数据
go : use gorm to modify data
从追赶到超越,国产软件大显身手
goto语句
Headline 2: there are several kinds of common SQL errors in MySQL usage?
Go 结合Gin导出Mysql数据到Excel表格