-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamicRangeSum.cpp
82 lines (73 loc) · 1.74 KB
/
dynamicRangeSum.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "bits/stdc++.h"
using namespace std;
#define ll long long
int getMid(int s, int e) {return s+(e-s)/2;}
ll buildStUtil(int *arr, int ss, int se, ll *st, int si)
{
if(ss==se) {
return st[si] = arr[ss];
}
int mid = getMid(ss, se);
return st[si] = buildStUtil(arr, ss, mid, st, 2*si+1)
+ buildStUtil(arr, mid+1, se, st, 2*si+2);
}
ll *buildSt(int *arr, int n)
{
int x = (int)(ceil(log2(n)));
int sz = 2*(int)pow(2,x)-1;
ll *st = new ll[sz];
buildStUtil(arr, 0, n-1, st, 0);
return st;
}
void updateQuery(ll *st, int ss, int se, int i, int diff, int pos)
{
if(i > se or i < ss) return;
st[pos] += diff;
if(se > ss){
int mid = getMid(ss, se);
updateQuery(st, ss, mid, i, diff, 2*pos+1);
updateQuery(st, mid+1, se, i, diff, 2*pos+2);
}
}
ll getSumQuery(ll *st, int qs, int qe, int ss, int se, int pos)
{
if(ss > qe or se < qs) return 0;
if(ss >= qs and se <= qe) return st[pos];
int mid = getMid(ss, se);
return getSumQuery(st, qs, qe, ss, mid, 2*pos+1)
+ getSumQuery(st, qs, qe, mid+1, se, 2*pos+2);
}
void solve()
{
ll n, q;
cin >> n >> q;
int arr[n];
for(int i=0; i<n; i++) cin >> arr[i];
ll *st = buildSt(arr, n);
int a, b, k, u;
int type;
while(q--)
{
cin >> type;
if(type == 1)
{
cin >> k >> u;
int diff = u - arr[k];
arr[k] = u;
updateQuery(st, 0, n-1, k, diff, 0);
}
else
{
cin >> a >> b;
b--;
cout << getSumQuery(st, a, b, 0, n-1, 0) << "\n";
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
}