操作:

单点更新,区间求和

区间求和:如sum [3,10) 需要对19,5,12,26节点求和即可。

观察可知,左端点为右子节点(奇数)时直接相加,右端点为左子节点(偶数)时直接相加,两边向中间移动并求其父节点。

  1. class NumArray {
  2. public:
  3. NumArray(vector<int> nums) {
  4. n = nums.size();
  5. tree.resize(n * ); // 满二叉树
  6. buildTree(nums);
  7. }
  8.  
  9. void buildTree(vector<int>& nums) {
  10. for (int i = n; i < n * ; ++i) {
  11. tree[i] = nums[i - n];
  12. }
  13. for (int i = n - ; i > ; --i) {
  14. tree[i] = tree[i<<] + tree[i<<|];
  15. }
  16. }
  17.  
  18. void update(int i, int val) {
  19. tree[i += n] = val;
  20. while (i > ) {
  21. tree[i / ] = tree[i] + tree[i^];
  22. i /= ;
  23. }
  24. }
  25.  
  26. int sumRange(int i, int j) {
  27. int sum = ;
  28. for (i += n, j += n; i <= j; i /= , j /= ) {
  29. if ((i & ) == ) sum += tree[i++];
  30. if ((j & ) == ) sum += tree[j--];
  31. }
  32. return sum;
  33. }
  34.  
  35. private:
  36. int n;
  37. vector<int> tree;
  38. };

Refer:

Codeforces blog

树状数组解法

所有的奇数位置的数字和原数组对应位置的相同,偶数位置是原数组若干位置之和,若干是根据坐标的最低位 Low Bit 来决定的 ( x&-x )

[i, j] 区间和:sum[j]-sum[i-1]

  1. class NumArray {
  2. public:
  3. NumArray(vector<int>& nums) {
  4. data.resize(nums.size());
  5. bit.resize(nums.size()+);
  6. for(int i=;i<nums.size();i++){
  7. update(i,nums[i]);
  8. }
  9. }
  10.  
  11. void update(int i, int val) {
  12. int diff = val - data[i];
  13. for(int j=i+;j<bit.size();j+=(j&-j)){
  14. bit[j]+=diff;
  15. }
  16. data[i] = val;
  17. }
  18.  
  19. int sumRange(int i, int j) {
  20. return getSum(j+)-getSum(i);
  21. }
  22.  
  23. int getSum(int i){
  24. int res = ;
  25. for(int j=i;j>=;j-=(j&-j)){
  26. res+=bit[j];
  27. }
  28. return res;
  29. }
  30. private:
  31. vector<int> data;
  32. vector<int> bit; // 前面补0, 从1开始
  33. };

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. maven 学习---Maven启用代理访问

    如果你的公司正在建立一个防火墙,并使用HTTP代理服务器来阻止用户直接连接到互联网.如果您使用代理,Maven将无法下载任何依赖. 为了使它工作,你必须声明在 Maven 的配置文件中设置代理服务器: ...

  2. 高性能的编程IO与NIO阻塞分析

    1.什么是阻塞,什么是非阻塞? 阻塞:结果返回之前,线程一直被挂起. 非阻塞:做一件事,尝试去做 2.传统IO模型 socket编程:

  3. Django使用Mysql已存在数据表的方法

    在mysql数据库中已经存在有数据的表,自己又不想删除,下面方法可以同步django中创建的表 1.最好将自己建的表名改为前缀和django自动创建表名前缀相同,不改也可以,但是后期表太多容易混乱 2 ...

  4. Spring Boot Admin 详解(Spring Boot 2.0,基于 Eureka 的实现)

    原文:https://blog.csdn.net/hubo_88/article/details/80671192 Spring Boot Admin 用于监控基于 Spring Boot 的应用,它 ...

  5. Python自动化:自动化发送邮件之SMTP

    自动发送邮件,作为自动化测试的流程之一,可以将运行后的测试报告自动发送至指定的对象,形成一次自动化的完整闭环,基于Python来实现的有关自动化发送邮件的内容,加上注释做了一个小小的整理. 话不多说直 ...

  6. 51nod 1594 Gcd and Phi(莫比乌斯反演)

    题目链接 传送门 思路 如果这题是这样的: \[ F(n)=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}\phi(gcd(i,j)) \] 那么我们可能会想到下 ...

  7. flask实战-个人博客-表单

    表单 下面我们来编写所有表单类,personalBlog中主要包含下面这些表单: 登录表单: 文章表单: 评论表单: 博客设置表单: 这里仅介绍登录表单.文章表单.分类表单和评论表单,其他的表单在实现 ...

  8. Java 为什么需要包装类,如何使用包装类?

    出处:https://cloud.tencent.com/developer/article/1362754

  9. Consul 学习资料

    资料 网址 Consul 入门指南 https://book-consul-guide.vnzmi.com/

  10. windows 上robot framework 读取sqlite3提示:OperationalError: unable to open database file错误

    原因:路径写的不正确. 正确的写法:'D:/Python27/163/demo.db' 或者 r 'E:\\rf-demos-master\\DatabaseDemo\\demo.db' Connec ...