Why do we require a 4*n memory for each of the answer array and the flag array in segment tree with lazy propogation.Can not we work with 2*n memory for each of these arrays and also can not we do this thing that instead of using a flag in the parent node for indicating that it's children needs to be updated we set the flag in the children itself indicating that these needs to be updated.
Please can anyone explain why do we need 4*N memory for segment tree construction ?
Actually we don't. It just makes the things easier.
Actually we just need
2N - 1
nodes, where N >= n is a power of 2In the worst case it is 4*n if n = 2k + 1
so your bound is actually higher (n = 2k + 1, N = 2k + 1, 2N - 1 = 2k + 2 - 1 = 4n - 5) :D
n = 2k + 1 ------------ 4n = 2(k + 2) + 4 ------------ 2(k + 2) - 1 = 4n - 5
Really higher :D
this is what i do. here is my code to solve HORRIBLE on SPOJ. if u dont understand it, u can post a comment here.
ok i have understood that but i have tried to solve a question by this method but it's giving runtime error. can you please help me in this? i am tired of fixing the bug but still not succeded here is the problem link :http://mirror.codeforces.com/problemset/problem/242/E and here is my code:http://mirror.codeforces.com/submissions/vis10326#.i will be greatly thankful to you
format ur code a bit better (a few spaces and tabs in each line should be enough).
also, give a link to ur submission in question, not ur submissions page.
Do you have real eyes?