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 ...
随机推荐
- SpringJDBC源码解析
读完本篇文章需要很长很长时间.... 传统JDBC 相信大家对传统的jdbc已经很熟悉了,无非就是下面这个流程 1234567891011 //1.加载驱动程序Class.forName(" ...
- Date工具类
总结了下项目中常用的时间转化方法,目前就这么点啦,以后再慢慢添加,先储备起来,免得丢啦. package com.example.keranbin.testdemo; import android.u ...
- metrics-server 安装问题解决
参考: https://www.qikqiak.com/post/install-metrics-server/ git clone https://github.com/kubernetes- ...
- k8s控制器资源(五)
Pod pod在之前说过,pod是kubernetes集群中是最小的调度单元,pod中可以运行多个容器,而node又可以包含多个pod,关系如下图: 在对pod的用法进行说明之前,有必要先对docke ...
- Python从零开始——基础语法
- HAProxy的调度算法
HAProxy通过固定参数balance指明对后端服务器的调度算法,该参数可以配置在listen或backend选项中. HAProxy的调度算法分为静态和动态调度算法,但是有些算法可以根据参 ...
- Python的字符串与字节码转换
一张图弄懂python的字符串与字节码转换
- Linux(Centos7)安装Oracle11.2.0数据字典初始化,监听,网络,创建用户等部分配置
#创建数据字典和pl/sql包 @/u01/app/oracle/product/11.2.0/db_1/rdbms/admin/catalog.sql; @/u01/app/oracle/produ ...
- Python并发编程之进程通信
''' 进程间的通信 ''' """ multiprocessing模块支持进程间通信的两种主要形式:管道和队列 都是基于消息传递实现的, ""&qu ...
- Ubuntu18.04安装redis-server启动出错
虽然报错原因可能是 redis-server.service: Can't open PID file /var/run/redis/re Aug 26 15:43:25 iZ2ze6ddwhet60 ...