I have implemented the code for build function in segment tree, but not getting desired output. Can u Pls tell what can be the probable reason ?
IDE : https://ide.codingblocks.com/s/197429
Segment Trees : Build function
But what was the error in my code ?
- Line 21:
build(a, mid+1, e, 2*index);
It should be : build(a, mid+1, e, 2*index+1);
You have to build the left and the right subtree.
return type of the buid should be int
else the min function would be calculating garbage value
line 25 2 * index+1 and 2 * index+2 was not mentioned
in main function start printing from 0 index and not 1
Got that. Thank you.
Also my query function is not giving desired output. For range 0-2 it should give ans as 1, but it’s giving -5. Can u pls rectify the error ? IDE :https://ide.codingblocks.com/s/197714
line 43 there needs to be udpate
make it to
2 * index+ 1 , 2 * index + 2
instead
2index , 2 index + 1
Also range_update function is not working properly. Pls check. IDE : https://ide.codingblocks.com/s/198726
@aman_ishan
u need to add the different in the current and the required value
like currently node value is 5
and the update function passes 10
then just 10-5 needs to be added in each function call to the nodes that are effected by this…
refer the below code it is well commented:
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.