About Segment Trees
This is my first Blog on a Programming topic and well as you can see,i am starting with a challenging task to explain segment trees
So i will start with a problem that i came across in a recent competition and this would obviously lead you to the idea of using segment trees Here
Not sure if you actually read the problem or not but here it goes:
Motivation:In an array,if you are continuously asked to output f(array[i,j]) where f can be any function (eg:here,f is both maximum and minimum) then this stuff is very useful indeed
You may not like that but it is not the best technique for finding the value for any f.for example,if f is sum[i,j] then it has an O(1) technique for finding sum after a preprocess of O(N) .But still it is a lot better than using naive techniques such as finding f(array[i,j]) evertime you are asked.
Well the rest part is hopefully easy and just requires constructing the tree and using the query function.If you are lazy like me then you can just copy the code from here
1 2 3 | |
for constructing a tree
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
for query
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 | |