-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMutable.cpp
65 lines (65 loc) · 1.81 KB
/
Mutable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<bits/stdc++.h>
using namespace std;
class NumArray{
public:
vector<int>a;
vector<int>tree;
int n;
NumArray(vector<int>nums){
a=nums;
n=nums.size();
tree.resize(4*n);
build(0,n-1,1);
}
void build(int first,int last,int node){
if (first==last){
tree[node]=a[first];
return;
}
int mid=(first+last)/2;
build(first,mid,2*node);
build(mid+1,last,2*node+1);
tree[node]=tree[node*2+1]+tree[node*2];
}
int sumRange(int left,int right){
return sumRangeFunction(left,right,0,n-1,1);
}
int sumRangeFunction(int query_left,int query_right,int node_left,int node_right,int node){
if (query_left<=node_left && query_right>=node_right)
return tree[node];
if (query_left>node_right || query_right<node_left)
return 0;
int mid=(node_left+node_right)/2;
return sumRangeFunction(query_left,query_right,node_left,mid,2*node)+sumRangeFunction(query_left,query_right,mid+1,node_right,2*node+1);
}
void update (int index , int val){
updateFunction(index,val,0,n-1,1);
}
void updateFunction(int index,int val,int first,int last,int node){
if (first==last){
tree[node]=val;
return ;
}
int mid=(first+last)/2;
if (index<=mid)
updateFunction(index,val,first,mid,2*node);
else
updateFunction(index,val,mid+1,last,2*node+1);
tree[node]=tree[2*node]+tree[2*node+1];
}
};
int main(){
int n;
cin>>n;
vector<int>nums;
for(int i=0;i<n;i++){
int b;
cin>>b;
nums.push_back(b);
}
NumArray * final= new NumArray(nums);
cout<<final->sumRange(0,2);
final->update(1,2);
cout<<final->sumRange(0,2);
return 0;
}