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.
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:
- The array is only modifiable by the update function.
- 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(树状数组)的更多相关文章
- [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@ [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] 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 - 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】307. Range Sum Query - Mutable
		Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ... 
- [Leetcode Week16]Range Sum Query - Mutable
		Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ... 
- 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), ... 
- 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
		问题描述:给定一序列,求任意区间(i, j)的元素和:修改任意一元素,实现快速更新 树状数组 树状数组的主要特点是生成一棵树,树的高度为logN.每一层的高度为k,分布在这一层的序列元素索引的二进制表 ... 
随机推荐
- Android · 广告走灯
			layout <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:and ... 
- JS批量获取参数构建JSON参数对象
			在做系统的时候,往往查询条件是被严格指定的,大量的查询条件,一两个页面还可以通过dom去一个一个获取,再构建参数对象,请求后台接口. 这里给大家讲一个批量获取前端参数,构建参数对象. <form ... 
- Hadoop--设置单一节点集群
			目的 这篇文档描述如何安装和配置一个单一节点的Hadoop,以便你可以快速使用hadoop mapreduce和Hadoop Distributed File System (HDFS)的一些简单操作 ... 
- Android 属性动画框架 ObjectAnimator、ValueAnimator ,这一篇就够了
			前言 我们都知道 Android 自带了 Roate Scale Translate Alpha 多种框架动画,我们可以通过她们实现丰富的动画效果,但是这些宽家动画却有一个致命的弱点,它们只是改变了 ... 
- Jenkins--Run shell command in jenkins as root user?
			You need to modify the permission for jenkins user so that you can run the shell commands. You can i ... 
- WPF之DataTemplateSelector技巧
			WPF中如何通过一个属性来控制对象的模板,属性值改变时对象的模板会跟随改变? 两个关键点 1 属性/对象更改通知 方法一:继承INotifyPropertyChanged接口,当属性值更改时需要让 ... 
- doT.js具体使用介绍
			官网: http://olado.github.iodoT.js具体使用介绍 用法: {{= }} for interpolation {{ }} for evaluation {{~ }} for ... 
- python 基础 1.2--pycharm 的安装及使用
			一. windows 先安装pycharm. PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,其提供了一个带编码补全,代码片段,支持代码折 ... 
- Darwin做直播时对ReflectorSession引用数的控制
			在之前的博客中,我们提到了如何用Darwin&live555实现直播过程,那么更进一步,当直播结束时,我们需要关闭所有正在收看的客户端,并且delete转发会话ReflectorSession ... 
- Vue中表单校验
			1.安装校验插件vee-validate npm install vee-validate --save 2.在main.js中引用插件 // 表单校验 import VeeValidate, { V ... 
