Binary Indexed Tree-307. Range Sum Query - Mutable
2024-09-29 13:02:15
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)
{
sums.push_back();
for (int i = ; i < nums.size(); i++)
{
sums.push_back(sums[i] + nums[i]);
values.push_back(nums[i]);
}
} void update(int i, int val)
{
int diff = val - values[i];
for (int k = i + ; k < sums.size(); k++)
sums[k] = sums[k] + diff;
values[i] = val;
} int sumRange(int i, int j)
{
return sums[j + ] - sums[i];
}
private:
vector<int> sums;
vector<int> values;
};
最新文章
- 通过innobackupex实现对MySQL的增量备份与还原
- 长轮询(long polling)
- 前端工程化开发之yeoman、bower、grunt
- Android -- 初探MVP模式
- tomcat异常: Cannot get a connection, pool exhausted
- bzoj 2241: [SDOI2011]打地鼠
- cocos2d-x3.2中怎样优化Cocos2d-X游戏的内存
- Installshield停止操作系统进程的代码--IS5版本适用
- 老李分享:网页爬虫java实现
- Python中serial的使用
- C# 中将月份格式化为英语缩写格式
- Windows 常用的 CMD 命令
- Cannot invoke Tomcat manager: socket write error
- mac系统下安装Windows(7,8,10都一样的步骤)
- Oracle- 复杂查询及总结
- html5 &; input &; accept attribute
- vs2013发布网站合并程序是出错(ILmerge.merge:error)
- Centos6安装oracle10g
- Monkeyrunner 简介及其环境搭建
- (19)zabbix Applications使用介绍