I recently read about segment trees and their implementation but was unable to decide what values should I put on tree’s node to get the desired answer. Like I was solving SPOJ question GSS1, and finally did it with the help of articles available related to this question, where people store four values on a node to get an answer, but I didn’t understand how can I approach for solving these type of questions.
Maybe a little bit explanation of a question related to segment tree could help me to understand better.
EDIT: If someone can provide links to the problem related to segment trees in increasing order of difficulty, that would be a great help.
I am also in same lines. New to segment trees and concept of trees altogether. From what i perceived so far, deciding what to store in nodes is exactly the thing segment tree revolves around. I mean, for the Q i practiced, deciding what to store in nodes and how to extract answer from it is all the Q proved to be about.
I suppose you have already tried the Q of type -
Minimum value in given range
Maximum value in given range
Sum of given range
If not, try these ones first, as these are the basic “cakewalk” ones in this category. Also, why dont you try Chef and Subarray from May long? Its a very good question on segment trees to practice. I recommend you solve it, and when you get stuck, you can ask in this thread for explanations. It will be a good Q to discuss, but see if you can solve it in one go.
All of these have something nice in common that you can apply for problems of this kind. Also, read these to learn about different variations that you might have to use based on the problem:
I found SPOJ GSS1 and Frequent to be good conceptually. If you know what to include in node, you will find all problems are solved with almost same algo.
The basic concept is that, the values stored must be such that parent and child nodes are correctly related. If you feel GSS1 taught you correct concepts, test them by attempting problem- SPOJ Frequent