1+ class SegmentTree :
2+ def __init__ (self , values ):
3+ self .valarr = values
4+ self .arr = dict ()
5+
6+ #start is the starting index of node.
7+ #end is the ending index of node.
8+ #l is the lower bound of given query.
9+ #r is the upper bound of given query.
10+
11+ def buildTree (self , start , end , node ):
12+ if start == end :
13+ self .arr [node ] = self .valarr [start ]
14+ return
15+ mid = (start + end )// 2
16+ #Building the left subtree of the node.
17+ self .buildTree (start , mid , node * 2 )
18+ #Building the right subtree of the node.
19+ self .buildTree (mid + 1 , end , node * 2 + 1 )
20+ #Assign the value of node as the sum of its children.
21+ self .arr [node ] = self .arr [node * 2 ]+ self .arr [node * 2 + 1 ]
22+
23+ def rangeQuery (self , node , start , end , l , r ):
24+ #When start and end index of the given node lies between the query range[l, r].
25+ if (l <= start and r >= end ):
26+ return self .arr [node ]
27+ #When the start and end index of the given node lies completely outside of the query range[l, r].
28+ if (end < l or start > r ):
29+ return 0
30+ #In case of overlapping of the regions of the start and end index of node and query range[l, r].
31+ mid = (start + end )// 2
32+ return self .rangeQuery (2 * node , start , mid , l , r ) + self .rangeQuery (2 * node + 1 , mid + 1 , end , l , r )
33+
34+ def update (self , node , newvalue , oldvalue , position , start , end ):
35+ #If position where the given value to be inserted lies within start and end index of the node.
36+ if start <= position <= end :
37+ self .arr [node ] += (newvalue - oldvalue )
38+ #Updating all those nodes where position lies within its start and end index.
39+ if start != end :
40+ mid = (start + end )// 2
41+ self .update (node * 2 , newvalue , oldvalue , position , start , mid )
42+ self .update (node * 2 + 1 , newvalue , oldvalue , position , mid + 1 , end )
43+
44+ #Code to run the above functions
45+ if __name__ == '__main__' :
46+ l = list (map (int , input ("Enter the elements of the array separated by space:\n " ).split ()))
47+ st = SegmentTree (l )
48+ st .buildTree (0 , len (l )- 1 , 1 )
49+
50+ #I have assumed 1 as the base index instead of 0.
51+ baseindex = 1
52+ endindex = len (l )
53+
54+ #To print the constructed segment tree.
55+ print (st .arr )
56+
57+ #To print the sum of numbers between index 3 and 5.
58+ print ("Sum of numbers from index 3 and 5 is: " , st .rangeQuery (1 , baseindex , endindex , 3 , 5 ))
59+
60+ #Updating 3rd element of the array to 10.
61+ updateindex = 3
62+ updatevalue = 10
63+ st .update (1 , updatevalue , l [updateindex - 1 ], updateindex , baseindex , endindex )
64+
65+ #To print the sum of numbers between index 3 and 5 after updation
66+ print ("Updated sum of numbers from index 3 and 5 is: " , st .rangeQuery (1 , baseindex , endindex , 3 , 5 ))
0 commit comments