操作:

单点更新,区间求和

区间求和:如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:

Codeforces blog

树状数组解法

所有的奇数位置的数字和原数组对应位置的相同,偶数位置是原数组若干位置之和,若干是根据坐标的最低位 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 精简无递归线段树的更多相关文章

  1. [Leetcode Week16]Range Sum Query - Mutable

    Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...

  2. 【刷题-LeetCode】307. Range Sum Query - Mutable

    Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...

  3. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  4. [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 ...

  5. leetcode笔记:Range Sum Query - Mutable

    一. 题目描写叙述 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), ...

  6. 307. Range Sum Query - Mutable

    题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...

  7. [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 ...

  8. [LeetCode] Range Sum Query - Mutable 题解

    题目 题目 思路 一看就是单点更新和区间求和,故用线段树做. 一开始没搞清楚,题目给定的i是从0开始还是从1开始,还以为是从1开始,导致后面把下标都改掉了,还有用区间更新的代码去实现单点更新,虽然两者 ...

  9. 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 ...

随机推荐

  1. Java开发环境系列:一篇能解决你99%问题的排雷日记

      安装 https://archive.apache.org/dist/tomcat/ 推荐使用免安装版的Tomcat(放在没有中文和空格的目录下),前提是已经安装了JDK并配置了环境变量.Linu ...

  2. Mybatis映射文件标签(关于sql)

    Mybatis映射文件 1.接口的全限定名和映射文件的namespace一致 <mapper namespace="com.offcn.dao.UserDao"> 2. ...

  3. make CMake 来龙去脉

    理论上说,任意一个C++程序都可以用g++来编译. 大家都知道,写程序大体步骤如下: 1.用编辑器编写源代码,如.c文件. 2.用编译器编译代码生成目标文件,如.o. 3.用链接器连接目标代码生成可执 ...

  4. 隐马尔科夫模型(Hidden Markov Models) 系列之四

    转自:http://blog.csdn.net/eaglex/article/details/6430389 前向算法(Forward Algorithm) 一.如果计算一个可观察序列的概率?   1 ...

  5. HAProxy的调度算法

      HAProxy通过固定参数balance指明对后端服务器的调度算法,该参数可以配置在listen或backend选项中.   HAProxy的调度算法分为静态和动态调度算法,但是有些算法可以根据参 ...

  6. Linux服务器安全加固

    关于对公司网站服务器安全加固的一些想法及思路: 一.修改密码和ssh登录端口,并且尽可能的用密钥对登录,禁止用密码登录(主要针对Linux)二.修改/etc/hosts.allow 设置仅仅允许某几台 ...

  7. vue - 基础(3)

    1.数据的双向绑定 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  8. dedecms去掉标题长度限制

    dedecms文章标题默认显示字数为60字节(30个汉字),如果想要显示更多要如何操作呢?两步解决问题,随ytkah一起来看看吧.1.后台找到“系统设置-系统基本参数-其他其他选项”(如下图)这时你可 ...

  9. mobx 学习笔记

    Mobx 笔记 Mobx 三板斧,observable.observer.action. observable: 通过 observable(state) 定义组件的状态,包装后的状态是一个可观察数据 ...

  10. .eslintrc的配置

    { // 环境定义了预定义的全局变量. "env": { //环境定义了预定义的全局变量.更多在官网查看 "browser": true, "node ...