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

The update(i, val) function modifies nums by updating the element at index i to val.

Example:

Given nums = [1, 3, 5]

sumRange(0, 2) -> 9
update(1, 2)
sumRange(0, 2) -> 8

Note:

  1. The array is only modifiable by the update function.
  2. You may assume the number of calls to update and sumRange function is distributed evenly.

题解:树状数组最基本的用法(点更新,区间查询)。需要注意的是这里的点更新不是把点加一个值,而是完全更新一个点的值,这就需要不仅仅更新树状数组,原数组的值也应该更新,因为可能下一个还会更新这个点,那么就需要知道它之前的值是多少。

class NumArray {
public:
NumArray(vector<int> &nums) {
n = nums.size();
c=vector<int>(n+,);
a=vector<int>(n+,);
for(int i=;i<=n;i++){
update(i-,nums[i-]);
}
} int lowbit(int x){
return x&-x;
}
void update(int i, int val) {
int old = a[i+];
for(int k=i+;k<=n;k+=lowbit(k)){
c[k]-=old;
c[k]+=val;
}
a[i+]=val;
}
int sum(int x){
int s=;
while(x>){
s+=c[x];
x-=lowbit(x);
}
return s;
} int sumRange(int i, int j) {
return sum(j+)-sum(i);
} private:
int n;
vector<int>c;
vector<int>a;
}; // Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.update(1, 10);
// numArray.sumRange(1, 2);

leetcode 307. Range Sum Query - Mutable(树状数组)的更多相关文章

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

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

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

  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】307. Range Sum Query - Mutable

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

  6. [Leetcode Week16]Range Sum Query - Mutable

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

  7. Leetcode 2——Range Sum Query - Mutable(树状数组实现)

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

  8. 307. Range Sum Query - Mutable

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

  9. leetcode 307 Range Sum Query

    问题描述:给定一序列,求任意区间(i, j)的元素和:修改任意一元素,实现快速更新 树状数组 树状数组的主要特点是生成一棵树,树的高度为logN.每一层的高度为k,分布在这一层的序列元素索引的二进制表 ...

随机推荐

  1. poj 1328 Radar Installation 【贪心】【区间选点问题】

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54798   Accepted: 12 ...

  2. hdu2141Can you find it?

     给你四个集合.要你从这四个集合中 各取出一个数出来,推断,取出的前三个数的和 是否等于第四个数. 数据比較大.我的做法是将 前两个集合全部数全部和的情况取出来, 然后二分查找第四个集合和第三集合 ...

  3. 强制重启Linux系统的几种方法

    实际生产环境中某些情况下 Linux 服务器系统在出现致命错误需要远程进行重启,通过常规的 reboot.init 6 等方法无法正常重启(例如重启时卡在驱动程序里等情况),这时就需要通过下面介绍的几 ...

  4. Nook 2 Root

        最后我还是忍不住root了它,用了差一点够一个月 1.备份2.root 3.装软件=====================================================1. ...

  5. [Android Studio 权威教程]最有用的快捷键

    上篇中我们讲了Android Studio怎样加入插件.这篇我们讲讲AS的快捷键.这里我说明的快捷键都是最最有用的,希望刚刚加入AS的朋友尽快的熟悉一下这几个快捷键,这样能够帮助你提高coding的效 ...

  6. java 中 wait和notify的用法

    package com.test; public class OutputThread { public static Object lockObj=new Object(); public stat ...

  7. go web的简单服务器

    1)简单web服务器: package main import ( "fmt" "net/http" ) func sayHelloName(w http.Re ...

  8. Mysql 索引增加与删除

    [1]索引 索引,通俗理解,即目录. 之前说过,计算机是对现实世界的模拟.目录应用在数据库领域,即所谓的索引. 目录的作用显而易见,所以建立索引可以大大提高检索的速度. 但是,会降低更新表的速度,如对 ...

  9. IntelliJ idea webstrom Visual Studio Code vscode 设置cmder为默认终端 Terminal

    1.系统环境win10 2.确保环境变量中存在CMDER_ROOT,没有的话新增一个.地址为*:\***\cmder . idea.webstrom:设置中搜索terminal,shell path  ...

  10. Elasticsearch集群UNASSIGNED

    Elasticsearch集群UNASSIGNED http://shineforever.blog.51cto.com/1429204/1859734 http://www.searchtech.p ...