当前位置:网站首页>详解树状数组模板——理论和代码的实现
详解树状数组模板——理论和代码的实现
2022-04-22 06:14:00 【shooter7】
树状数组
-
实际题目(leetcode 307. 区域和检索 - 数组可修改)
https://leetcode-cn.com/problems/range-sum-query-mutable/ -
视频讲解
https://www.bilibili.com/video/BV1pE41197Qj?spm_id_from=333.337.search-card.all.click -
Java代码模板
// 上来先把三个方法写出来
{
int[] tree;
int lowbit(int x) {
return x & -x;
}
// 查询前缀和的方法(包含下标x)
int query(int x) {
int ans = 0;
for (int i = x; i > 0; i -= lowbit(i)) ans += tree[i];
return ans;
}
// 在树状数组 x 位置中增加值 u
void add(int x, int u) {
for (int i = x; i <= n; i += lowbit(i)) tree[i] += u;
}
}
// 初始化「树状数组」,要默认数组是从 1 开始
{
for (int i = 0; i < n; i++) add(i + 1, nums[i]);
}
// 使用「树状数组」:
{
void update(int i, int val) {
// 原有的值是 nums[i],要使得修改为 val,需要增加 val - nums[i]
add(i + 1, val - nums[i]);
nums[i] = val;
}
int sumRange(int l, int r) {
return query(r + 1) - query(l);
}
}
版权声明
本文为[shooter7]所创,转载请带上原文链接,感谢
https://blog.csdn.net/shooter7/article/details/123966754
边栏推荐
- 14 lines of code to complete arbitrary selection of image crawling
- [number theory] [indefinite equation] n-ary primary indefinite equation, Pell equation, Pythagoras theorem, Fermat theorem
- 短路
- Methods and skills of fast reading papers
- ASP.NET日常开发随手记------webService服务端开发
- Zhejiang University Edition "C language programming (3rd Edition)" topic set exercise 7-4 find out the elements that are not common to two arrays
- MySQL learning notes
- 【数论】欧拉函数(基本性质、递推法、公式法、线性筛法)
- 【数论】素数(一):基本概念、性质、猜想、定理
- Design a circle class with private member radius representing radius and function get_ Radius () is used to obtain the radius and area () is used to calculate the area of the circle; (2) Define a tabl
猜你喜欢

1. Compile the following three modules of student information management system: and detect the implementation. 1. Add student information 4. Query student information 5. Query all student information

synchronized锁优化的一些机制(锁升级)
![ERROR: [Hsi 55-1545] ,无法正常生成fsbl,Unable to read in MSS file,Failed to closesw system.mss](/img/4e/34e2820ff8579007b20b33b27d8f1d.png)
ERROR: [Hsi 55-1545] ,无法正常生成fsbl,Unable to read in MSS file,Failed to closesw system.mss

Process of stepping on the pit in the flutter environment

双向循环链表(详)

【JEECG】修改Viser图表颜色样式

format()方法的格式控制

14 lines of code to complete arbitrary selection of image crawling

. net learning notes (I) -- introduction, advantages, design ideas, principles and applications of generics

C语言 | 指针
随机推荐
14行代码完成任意选择图片爬取
【数论】素数(一):基本概念、性质、猜想、定理
把上一次作业第一部分,有参数的类,改成无参数方式呈现,功能不变。
Wechat browser cannot save cookies for a long time
Install and modify the installation path of utools and vscode plug-ins
虚拟机磁盘空间缩小
. net learning notes (2) -- ubiquitous reflection (including the method of reading JSON)
Solution to the problem of Chinese garbled code in pyftpdlib
JS realizes clicking avatar to upload picture modification
csdn文字样式
Choose any novel website, crawl any novel and save it in the form of Notepad.
[number theory] prime number (4): decomposition of numbers (Pollard rho)
短路
C语言 | 指针
MySQL learning notes
JS实现点击头像上传图片修改
What is socket programming?
【数论】素数(四):数的分解(Pollard-rho)
写一个方法sanjiao(a, b, c),判断三个参数是否能构成一个三角形,如果不能则抛出异常IllegalArgumentException,显示异常信息a,b,c”不能构成三角形”,如果可以
. net daily thoughts - compare two JSON files and get data with the same tag but different values