-
Notifications
You must be signed in to change notification settings - Fork 0
/
Segment_Tree_0.cpp
77 lines (63 loc) · 1.78 KB
/
Segment_Tree_0.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
// Segment tree
// This includes finding the sum of consecutive array elements
// a[l...r],or finding the minimum element in a such a range in O(logn) time.
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100 // Maximum value of n
//We store the Segment Tree simply as an array t[] with a size of four times the input size n
//(We need to store at most 4n vertices)
//The left child of a vertex at index i is stored as index 2i,
//and the right one at index 2i+1
int n,t[4*MAXN];
//The procedure for constructing the Segment Tree (recursive function)
//the boundaries tl and tr of the current segment
//In the main program ,It will be called with the parameter
//of the root vertex:v=1,tl=0;tr=n-1
void build(int a[],int v,int tl,int tr)
{
if(tl==tr){
t[v]=a[tl];
}else{
int tm=(tl+tr)/2;
build(a,v*2,tl,tm);
build(a,v*2+1,tm+1,tr);
t[v]=t[2*v]+t[2*v+1];
}
}
//Compute the sum of the segment a[l...r]
int sum(int v,int tl,int tr,int l,int r)
{
//The current vertex v and its boundaries tl and tr
if(l>r)
return 0;
if(l==tl&&r==tr)
{
return t[v];
}
int tm=(tl+tr)/2;
return sum(v*2,tl,tm,l,min(r,tm))+sum(v*2+1,tm+1,tr,max(l,tm+1),r);
}
//Modify a specific elememt in the array
//Recursively calls itself with one of the two child vertices
void update(int v,int tl,int tr,int pos,int new_val)
{
if(tl==tr){
t[v]=new_val;
} else {
int tm=(tl+tr)/2;
if(pos<=tm)
update(v*2,tl,tm,pos,new_val);
else
update(v*2+1,tm+1,tr,pos,new_val);
t[v]=t[2*v]+t[2*v+1];
}
}
int main()
{
int a[]={1,3,-2,8,-7};
build(a,1,0,4);
cout<<sum(1,0,4,2,4)<<endl;
update(1,0,4,3,100);
cout<<sum(1,0,4,2,4)<<endl;
return 0;
}