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 ...
随机推荐
- maven 学习---Maven启用代理访问
如果你的公司正在建立一个防火墙,并使用HTTP代理服务器来阻止用户直接连接到互联网.如果您使用代理,Maven将无法下载任何依赖. 为了使它工作,你必须声明在 Maven 的配置文件中设置代理服务器: ...
- 高性能的编程IO与NIO阻塞分析
1.什么是阻塞,什么是非阻塞? 阻塞:结果返回之前,线程一直被挂起. 非阻塞:做一件事,尝试去做 2.传统IO模型 socket编程:
- Django使用Mysql已存在数据表的方法
在mysql数据库中已经存在有数据的表,自己又不想删除,下面方法可以同步django中创建的表 1.最好将自己建的表名改为前缀和django自动创建表名前缀相同,不改也可以,但是后期表太多容易混乱 2 ...
- Spring Boot Admin 详解(Spring Boot 2.0,基于 Eureka 的实现)
原文:https://blog.csdn.net/hubo_88/article/details/80671192 Spring Boot Admin 用于监控基于 Spring Boot 的应用,它 ...
- Python自动化:自动化发送邮件之SMTP
自动发送邮件,作为自动化测试的流程之一,可以将运行后的测试报告自动发送至指定的对象,形成一次自动化的完整闭环,基于Python来实现的有关自动化发送邮件的内容,加上注释做了一个小小的整理. 话不多说直 ...
- 51nod 1594 Gcd and Phi(莫比乌斯反演)
题目链接 传送门 思路 如果这题是这样的: \[ F(n)=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}\phi(gcd(i,j)) \] 那么我们可能会想到下 ...
- flask实战-个人博客-表单
表单 下面我们来编写所有表单类,personalBlog中主要包含下面这些表单: 登录表单: 文章表单: 评论表单: 博客设置表单: 这里仅介绍登录表单.文章表单.分类表单和评论表单,其他的表单在实现 ...
- Java 为什么需要包装类,如何使用包装类?
出处:https://cloud.tencent.com/developer/article/1362754
- Consul 学习资料
资料 网址 Consul 入门指南 https://book-consul-guide.vnzmi.com/
- windows 上robot framework 读取sqlite3提示:OperationalError: unable to open database file错误
原因:路径写的不正确. 正确的写法:'D:/Python27/163/demo.db' 或者 r 'E:\\rf-demos-master\\DatabaseDemo\\demo.db' Connec ...