操作:

单点更新,区间求和

区间求和:如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. JSON.stringify & JSON.parse 简析

    以前用到JSON的场景也不少,但是没有仔细的研究过,这几天趁着一个需求用到了,就整理了一下相关用法. 一. JSON.stringify() 1. 语法  JSON.stringify(value[, ...

  2. pdf.js实现图片在线预览

    项目需求 前段时间项目中遇到了一个模块,是关于在线预览word文档(PDF文件)的,所以,找了很多插件,例如,pdf.js,pdfobject.js框架,但是pdfobject.js框架对于IE浏览器 ...

  3. 前端Q的小小小里程碑

    很多关注我的人都不太了解前端Q这个名字的由来,这篇文章就来讲讲前端Q的前世今生,顺便送大大福利(文末有惊喜),哈哈-- 恭喜!前端Q总用户数突破千啦~~ 前端Q前世 前端Q这个公众号,其实很早很早的时 ...

  4. cs1.6 人物地址查询

    打开游戏 控制台命令 增加一个BOT:bot_add 增加一个警察:bot_add_ct 增加一个匪徒:bot_add_t 让BOT站在原地不动:bot_stop 1 "-"减号: ...

  5. Android 一个TextView中设置多种不同大小的字体,设置超链接

    以前项目中要是遇到这样的UI设计,都是傻不拉唧的分为三个TextView来实现,今天在微信中无意中看了一篇公众号文章,发现原来只要一个TextView就可以搞定啦,人生最悲哀的事情莫过于工作了这么久啦 ...

  6. App过大

    最近开发中遇到一个报错信息 如下 Error:Cannot fit requested classes in a single dex file.Try supplying a main-dex li ...

  7. Kubernetes概念之mater、node

    很久没写博客了,终于把重心找回来了,不过没有以前有斗志.有理想.有目标了.慢慢来.你若问我我最近几年的规划是什么,还真不知道.突然发现摧毁一个人真的很简单.k8s也是一遍一遍的从入门到放弃,还是要好好 ...

  8. Cloud Alert 实现告警智能降噪,成功规避告警风暴

    # 前言 睿象云前段时间发表了一篇[< Zabbix 实现电话.邮件.微信告警通知的实践分享>](https://www.toutiao.com/i6734876723126469127/ ...

  9. MySQL Execution Plan--文件排序(file sort)

    在MySQL处理ORDER BY语句时,如果查询无法利用索引的有序性,则需要额外操作对数据进行排序.在MySQL中有三种排序算法: 1.快速排序(Quick Sort),对冒泡排序的一种改进,基本思想 ...

  10. sqlmap选项卡介绍及常用语句

    sqlmap的使用方式:python sqlmap.py [options]: sqlmap中一共有以下十六个选项卡: 1.帮助选项卡: 2.Target(目标选项卡): 3.Request(请求选项 ...