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 ...
随机推荐
- Spring所有注解大揭秘
声明bean的注解 @Component 组件,没有明确的角色 @Service 在业务逻辑层使用(service层) @Repository 在数据访问层使用(dao层) @Controller 在 ...
- 记录下vue 中引用echarts 出现 "TypeError: Cannot read property 'getAttribute' of undefined"问题
今天做项目,用echarts展示数据 ,自己测试 先测试 了下.写的代码html: <div ref="myChart" style="height:300px;w ...
- CSSS选择器总结
title: CSSS选择器总结 date: 2018-07-30 20:11:07 tags: css --- 在css的学习中有一个很容易让人混乱的就是css选择器,因为选择器有很多种,而且在使用 ...
- 金蝶BOS元模型分析
对一些需求变化多样的产品而言,做好可变性设计是非常重要的.国外做得好的有Siebel,国内有金蝶的BOS,实际上金蝶的BOS很多理念跟Siebel是相似的,呵呵...他们都是采用MDD的方式来解决可变 ...
- sqlmap基本信息及参数使用方法
当给sqlmap这么一个url的时候,它会: 1.判断可注入的参数 2.判断可以用那种SQL注入技术来注入 3.识别出哪种数据库 4.根据用户选择,读取哪些数据 sqlmap支持五种不同的注入模式: ...
- (原)pytorch中使用TensorRT
转载请注明出处: https://www.cnblogs.com/darkknightzh/p/11332155.html 代码网址: https://github.com/darkknightzh/ ...
- Bash Shell中的特殊位置变量及其应用
Bash Shell中的特殊位置变量及其应用 众所周知bash shell中有许多特殊的位置变量,灵活使用它们可以更好地发挥Shell脚本的功用. 即位置变量:$1,$2,...来表示,用于让脚本在脚 ...
- 【若泽大数据】玩转大数据之Spark零基础到实战
https://www.bilibili.com/video/av29407581?p=1 若泽大数据官网 http://www.ruozedata.com/ tidb 系列三:有了sparkjdbc ...
- MATLAB之心形图绘制
一.静态心形图绘制 (1)效果展示 (2)静态心形原始代码 clc; clear all; ; % 均布三位坐标 x=-:; y=-:; z=-:; [x,y,z]=meshgrid(x,y,z); ...
- Java多线程编程核心技术-第5章-定时器 Timer-读书笔记
第 5 章 定时器 Timer 定时 / 计划功能在移动开发领域使用较多,比如 Android 技术.定时计划任务功能在 Java 中主要使用的就是 Timer 对象,他在内部使用多线程的方式进行处理 ...