Range Sum Query - Mutable 精简无递归线段树
操作:
单点更新,区间求和

区间求和:如sum [3,10) 需要对19,5,12,26节点求和即可。
观察可知,左端点为右子节点(奇数)时直接相加,右端点为左子节点(偶数)时直接相加,两边向中间移动并求其父节点。
class NumArray {
public:
NumArray(vector<int> nums) {
n = nums.size();
tree.resize(n * ); // 满二叉树
buildTree(nums);
}
void buildTree(vector<int>& nums) {
for (int i = n; i < n * ; ++i) {
tree[i] = nums[i - n];
}
for (int i = n - ; i > ; --i) {
tree[i] = tree[i<<] + tree[i<<|];
}
}
void update(int i, int val) {
tree[i += n] = val;
while (i > ) {
tree[i / ] = tree[i] + tree[i^];
i /= ;
}
}
int sumRange(int i, int j) {
int sum = ;
for (i += n, j += n; i <= j; i /= , j /= ) {
if ((i & ) == ) sum += tree[i++];
if ((j & ) == ) sum += tree[j--];
}
return sum;
}
private:
int n;
vector<int> tree;
};
Refer:
树状数组解法

所有的奇数位置的数字和原数组对应位置的相同,偶数位置是原数组若干位置之和,若干是根据坐标的最低位 Low Bit 来决定的 ( x&-x )
[i, j] 区间和:sum[j]-sum[i-1]
class NumArray {
public:
NumArray(vector<int>& nums) {
data.resize(nums.size());
bit.resize(nums.size()+);
for(int i=;i<nums.size();i++){
update(i,nums[i]);
}
}
void update(int i, int val) {
int diff = val - data[i];
for(int j=i+;j<bit.size();j+=(j&-j)){
bit[j]+=diff;
}
data[i] = val;
}
int sumRange(int i, int j) {
return getSum(j+)-getSum(i);
}
int getSum(int i){
int res = ;
for(int j=i;j>=;j-=(j&-j)){
res+=bit[j];
}
return res;
}
private:
vector<int> data;
vector<int> bit; // 前面补0, 从1开始
};
Range Sum Query - Mutable 精简无递归线段树的更多相关文章
- [Leetcode Week16]Range Sum Query - Mutable
Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...
- 【刷题-LeetCode】307. Range Sum Query - Mutable
Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] 307. Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- leetcode笔记:Range Sum Query - Mutable
一. 题目描写叙述 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...
- 307. Range Sum Query - Mutable
题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...
- [LeetCode] 307. Range Sum Query - Mutable 解题思路
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] Range Sum Query - Mutable 题解
题目 题目 思路 一看就是单点更新和区间求和,故用线段树做. 一开始没搞清楚,题目给定的i是从0开始还是从1开始,还以为是从1开始,导致后面把下标都改掉了,还有用区间更新的代码去实现单点更新,虽然两者 ...
- LeetCode - 307. Range Sum Query - Mutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
随机推荐
- <Android Studio> 2.APP开机启动
开机启动,也就是App随着机器开机而启动,在很多工业场景中是非常常见的. 开机启动的基本原理就是监听系统启动相关的广播,然后启动App. 为了实现开机启动,我人为的分为几个步骤 1.创建broadca ...
- Odoo报表的report标签和报表格式定义
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826329.html 一:Report标签 report标签可用于定义一条报表记录.属性有: 1) ...
- Detectron2源码阅读笔记-(三)Dataset pipeline
构建data_loader原理步骤 # engine/default.py from detectron2.data import ( MetadataCatalog, build_detection ...
- 201871010104-陈园园 《面向对象程序设计(java)》第四周学习总结
201871010104-陈园园 <面向对象程序设计(java)>第四周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
- Django-Model操作数据库
查询 models.UserInfo.objects.all() models.UserInfo.objects.all().values('user') #只取user列 models.UserIn ...
- js日志组件封装
js日志组件~~ 1 function Logger(level) { if (!(this instanceof Logger)) { return new Logger(); } var ERRO ...
- 排序算法-冒泡排序(Java)
package com.rao.sort; import java.util.Arrays; /** * @author Srao * @className BubbleSort * @date 20 ...
- 用原生js实现,点击一个列表时,输出对应的索引
var ul = document.querySelector("ul"); ul.addEventListener("mousedown", mouseHan ...
- P3258 [JLOI2014]松鼠的新家题解
题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有\(n\)个房间,并且有\(n-1\)根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在"树& ...
- ZROI 暑期高端峰会 A班 Day1 序列数据结构
FBI Warning:本文包含大量人类的本质之一 CF643G 维护一个序列,可以区间赋值,求区间中出现超过 \(p\%\) 的数. 允许输出不对的数,允许重复输出,但是所有对的数都一定要输出.而且 ...