forked from hacktoberfest2k20/DataStructures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Segment Tree for Sum.cpp
108 lines (93 loc) · 1.83 KB
/
Segment Tree for Sum.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Segment Tree for updating ith index with value val and find sum from range [l,r)
#include <bits/stdc++.h>
using namespace std;
#include <vector>
using namespace std;
#define ll long long
#define pp pair<ll, ll>
#define vii vector<ll>
struct segment
{
ll size;
vector<ll> v;
// initialize
void make(ll n)
{
size = 1;
while (size < n)
{
size *= 2;
}
v.assign(2 * size + 1, 0);
}
// set function to set ith index to val
void set(ll i, ll val, ll x, ll l, ll r)
{
if (r - l == 1)
{
v[x] = val;
return;
}
ll m = (l + r) / 2;
if (i < m)
{
set(i, val, 2 * x + 1, l, m);
}
else
{
set(i, val, 2 * x + 2, m, r);
}
v[x] = v[2 * x + 1] + v[2 * x + 2];
return;
}
void set(ll i, ll val)
{
set(i, val, 0, 0, size);
}
// find the sum from range [lx,rx)
ll sums(ll l, ll r, ll x, ll lx, ll rx)
{
// case 1
if (r <= lx || l >= rx)
return 0;
// case 2
if (l <= lx && r >= rx)
{
return v[x];
}
ll mid = (lx + rx) / 2;
ll sum1 = sums(l, r, 2 * x + 1, lx, mid);
ll sum2 = sums(l, r, 2 * x + 2, mid, rx);
return sum1 + sum2;
}
ll sum(ll l, ll r)
{
return sums(l, r, 0, 0, size);
}
};
int main()
{
ll n, m;
cin >> n >> m;
segment st;
st.make(n);
for (int i = 0; i < n; i++)
{
ll x;
cin >> x;
st.set(i, x);
}
while (m--)
{
ll a, b, c;
cin >> a >> b >> c;
if (a == 1)
{
st.set(b, c);
}
else
{
cout << st.sum(b, c) << "\n";
}
}
}