当前位置:网站首页>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;
}
边栏推荐
- Ali two sides: Sentinel vs Hystrix comparison, how to choose?
- Electron之初出茅庐——搭建环境并运行第一个程序
- golang : Zap log integration
- MySQL master-slave replication configuration construction, one step in place
- 五号黯区靶场 mysql 注入之limit注入记录
- “AI教练”请进家,家庭智能健身蓬勃发展
- sizeof
- 【MySQL】MySQL中如何实现分页操作
- Proof of distance calculation from space vertex to plane and its source code
- Playing script killing with AI: actually more involved than me
猜你喜欢
随机推荐
Go uses freecache for caching
什么是微服务?
Go uses the mencached cache
Boot process and service control
ETL为什么经常变成ELT甚至LET?
2020 数学建模之旅
手机端滚动至页面指定位置
mysql高阶语句(一)
From catching up to surpassing, domestic software shows its talents
go : go-redis set操作
万能js时间日期格式转换
Go 结合Gin导出Mysql数据到Excel表格
DP5340 domestic replacement for CM5340 stereo audio A/D converter chip
The terminal connection tools, rolling Xshell
Required request body is missing problem solving
这个终端连接工具,碾压Xshell
Equation Derivation Proof of Vector Triple Product
go : delete database data using grom
2020年度总结——品曾经,明得失,展未来
The usage of window.open(), js opens a new form