forked from Ayu-hack/Hacktoberfest2024-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Segment.cpp
104 lines (83 loc) · 1.97 KB
/
Segment.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
// C++ code for segment tree with sum
// range and update query
#include <bits/stdc++.h>
using namespace std;
vector<int> A, ST;
void build(int node, int L, int R)
{
// Leaf node where L == R
if (L == R) {
ST[node] = A[L];
}
else {
// Find the middle element to
// split the array into two halves
int mid = (L + R) / 2;
// Recursively travel the
// left half
build(2 * node, L, mid);
// Recursively travel the
// right half
build(2 * node + 1, mid + 1, R);
// Storing the sum of both the
// children into the parent
ST[node] = ST[2 * node] + ST[2 * node + 1];
}
}
void update(int node, int L, int R, int idx, int val)
{
// Find the lead node and
// update its value
if (L == R) {
A[idx] += val;
ST[node] += val;
}
else {
// Find the mid
int mid = (L + R) / 2;
// If node value idx is at the
// left part then update
// the left part
if (L <= idx and idx <= mid)
update(2 * node, L, mid, idx, val);
else
update(2 * node + 1, mid + 1, R, idx, val);
// Store the information in parents
ST[node] = ST[2 * node] + ST[2 * node + 1];
}
}
int query(int node, int tl, int tr, int l, int r)
{
// If it lies out of range then
// return 0
if (r < tl or tr < l)
return 0;
// If the node contains the range then
// return the node value
if (l <= tl and tr <= r)
return ST[node];
int tm = (tl + tr) / 2;
// Recursively traverse left and right
// and find the node
return query(2 * node, tl, tm, l, r)
+ query(2 * node + 1, tm + 1, tr, l, r);
}
// Driver code
int main()
{
int n = 6;
A = { 0, 1, 3, 5, -2, 3 };
// Create a segment tree of size 4*n
ST.resize(4 * n);
// Build a segment tree
build(1, 0, n - 1);
cout << "Sum of values in range 0-4 are: "
<< query(1, 0, n - 1, 0, 4) << "\n";
// Update the value at idx = 1 by
// 100 thus becoming 101
update(1, 0, n - 1, 1, 100);
cout << "Value at index 1 increased by 100\n";
cout << "sum of value in range 1-3 are: "
<< query(1, 0, n - 1, 1, 3) << "\n";
return 0;
}