题目
307.区域和检索 - 数组可修改
题目大意
给你一个数组 nums ,请你完成两类查询。
- 其中一类查询要求 更新 数组
nums下标对应的值 - 另一类查询要求返回数组
nums中索引left和索引right之间( 包含 )的nums元素的 和 ,其中left <= right
实现 NumArray 类:
NumArray(int[] nums)用整数数组nums初始化对象void update(int index, int val)将nums[index]的值 更新 为valint sumRange(int left, int right)返回数组nums中索引left和索引right之间( 包含 )的nums元素的 和 (即,nums[left] + nums[left + 1], ..., nums[right])
样例
示例 1:
输入:
["NumArray", "sumRange", "update", "sumRange"]
[[[1, 3, 5]], [0, 2], [1, 2], [0, 2]]
输出:
[null, 9, null, 8]
解释:
NumArray numArray = new NumArray([1, 3, 5]);
numArray.sumRange(0, 2); // 返回 1 + 3 + 5 = 9
numArray.update(1, 2); // nums = [1,2,5]
numArray.sumRange(0, 2); // 返回 1 + 2 + 5 = 8
数据规模
提示:
- 1 < = n u m s . l e n g t h < = 3 ∗ 1 0 4 1 <= nums.length <= 3 * 10^41<=nums.length<=3∗104
-100 <= nums[i] <= 1000 <= index < nums.length-100 <= val <= 1000 <= left <= right < nums.length- 调用
update和sumRange方法次数不大于 3 ∗ 1 0 4 3 * 10^43∗104
思路
单点修改统计区间和问题:直接套用树状数组模板即可。
vector<int>sum用于统计区间和;函数add用于将x下标之后的区间全部加k kk;函数query统计区间[ 1 , x ] [1,x][1,x]的总和。首先将数组nums的元素全部加入到树状数组中,然后对于update操作先将i n d e x + 1 index+1index+1(默认从1 11开始)之后的区间全部加v a l − a [ i n d e x ] val-a[index]val−a[index],对于sumRange函数统计区间和:q u e r y ( r i g h t + 1 ) − q u e r y ( l e f t ) query(right+1)-query(left)query(right+1)−query(left)。
代码
// short int long float double bool char string void
// array vector stack queue auto const operator
// class public private static friend extern
// sizeof new delete return cout cin memset malloc
// relloc size length memset malloc relloc size length
// for while if else switch case continue break system
// endl reverse sort swap substr begin end iterator
// namespace include define NULL nullptr exit equals
// index col row arr err left right ans res vec que sta
// state flag ch str max min default charray std
// maxn minn INT_MAX INT_MIN push_back insert
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
typedef pair<int, string>PIS;
const int maxn=3e4+50;//注意修改大小
long long read(){long long x=0,f=1;char c=getchar();while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}while(isdigit(c)){x=x*10+c-'0';c=getchar();}return x*f;}
ll qpow(ll x,ll q,ll Mod){ll ans=1;while(q){if(q&1)ans=ans*x%Mod;q>>=1;x=(x*x)%Mod;}return ans%Mod;}
class NumArray {
public:
vector<int>a;
int n;
vector<int>sum;
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int k)
{
while(x<=n)
{
sum[x]+=k;
x+=lowbit(x);
}
}
int query(int x)
{
int res=0;
while(x)
{
res+=sum[x];
x-=lowbit(x);
}
return res;
}
NumArray(vector<int>& nums):a(nums),sum(nums.size()+1) {
n=a.size();
for(int i=1;i<=n;i++)add(i,a[i-1]);
}
void update(int index, int val) {
add(index+1,val-a[index]);
a[index]=val;
}
int sumRange(int left, int right) {
return query(right+1)-query(left);
}
};
/**
* Your NumArray object will be instantiated and called as such:
* NumArray* obj = new NumArray(nums);
* obj->update(index,val);
* int param_2 = obj->sumRange(left,right);
*/
版权声明:本文为Phoenix_ZengHao原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。