# spoj GSS1 problem

I have been trying to understand the solution of [GSS1][1] problem from last 2 days but still not been able to get why are we maintaining the suffixsum and prefixsum of every node. here is the link to the



[1]: http://www.spoj.com/problems/GSS1/
[2]: http://ideone.com/UgPrJT
3 Likes

http://ayurjain.blogspot.in/?m=1 This is a blog by ayur jain, It is well explained and better to understand what is happening in the code.

In GSS1, few of the things you need to consider are best left sum, best right sum, best sum and sum - code by ayur jain http://ideone.com/Qq25J5

PS i solved this problem after going through his editorial

3 Likes

Here’s another similar question regarding GSS1 problem : https://discuss.codechef.com/questions/94192/please-check-the-solution-to-spoj-gss1-problem

You might look into the code here : https://github.com/Shraeyas/SPOJ-Solutions/blob/master/gss1.cpp

3 Likes

Codechef: Crazy Malvika discovers Crazy Fibonacci function

Asking questions on a new thread without enough karma is impermissible here, so I can’t. Please look into the question.

I read the CHN08 - Editorial, but I still can’t understand enough of it.

3 Likes

Can you please explain me concept of prefixsum and sufixsum for every node?? Means why and how are we maintaining those two sums

4 Likes

@anno In an array prefix sum comes from left subtree and suffix sum comes from right subtree, therefore to calculate the best sum we need store max(max(A.best,B.best),A.suffix+B.prefix)

Go through the image I’ve calculated what is actually happening in code
We take tree[n].prefix=max(tree[2n+1].prefix,tree[2n+1].sum+B.prefix) because max sum can be in left sub tree or left subtree + left subtree of right sub tree and vice versa for tree[n] suffix

This pic is just to depict the meaning, you can paper run the program on these values to better understand the codes.

8 Likes

@anno I’ve uploaded pics, prefix sum is max of ( (sum of left subtree + left node sum of right subtree) and left node sum of left subtree)
suffix sum is max of (( sum of right subtree+right node sum of left subtree) and right node sum of right sub tree)

Can somebody tell why I am getting WA?

My code - http://ideone.com/zUnx5g

3 Likes

1 Like

It’s just the normal approach but iterative instead of recursive.

1 Like

Here’s my attempt at explaining the approach. The problem is the maximum sum subarray problem, which you want to solve by a divide and conquer approach, which is what a segment tree does.

For a given range A, you split it into two pieces, the left half L and the right half R. Naturally the maximum sum subarray in A can lie either 1) completely in L, or lie 2) completely in R, or 3) cross over from L to R. Solve recursively for the two halves. We get the maximum sum subarrays for the left half as L.maxsum and the right half as R.maxsum, and now we know the first 2 of the 3 options just like that *snaps fingers*
How do we get the third option? A range that extends from L to R can be broken into a suffix of L and a prefix of R. So if we know the maximum suffix sum in L as L.suffixsum and the maximum prefix sum in R as R.prefixsum, the sum of those 2 values will give us the maximum sum subarray extending from L to R, which covers option 3. So we get

tree[index].maxsum = max(max(tree[2\*index+1].maxsum, tree[2\*index+2].maxsum),
tree[2\*index+1].suffixsum + tree[2\*index+2].prefixsum);

Now since we have incorporated 2 new values (prefixsum and suffixsum), the parent range of A will also require these from A. So now we need to calculate A.prefixsum and A.suffixsum. The maximum sum prefix of A can be 1) the maximum sum prefix of L, or 2) cover the whole of L and extend into R. Option 1 is simply L.prefixsum, and option 2 is \sum L + R.prefixsum. Similarly, the maximum sum suffix of A is either R.suffixsum or \sum R + L.suffixsum. So now it seems we need another value to represent a range, which is the sum of that range. So we have

tree[index].prefixsum = max(tree[2\*index+1].prefixsum,
tree[2\*index+1].sum + tree[2\*index+2].prefixsum);
tree[index].suffixsum = max(tree[2\*index+2].suffixsum,
tree[2\*index+2].sum + tree[2\*index+1].suffixsum);

And for the sum it is simply

tree[index].sum = tree[2\*index+1].sum + tree[2\*index+2].sum;

Well, that covers the whole thing, I hope it is clear why we require the 4 values in one node of the segment tree. Also you can see that the calculation of tree[index].maxsum in the link you provided can be simplified as I have described above. Feel free to ask if anything is not clear

19 Likes

It is failing for this case

10

-1 9 2 6 -4 3 5 2 -6 1

1

4 7

4 Likes

Thanks a lot @shraeyas

1 Like

1 Like

@kodeerder Since f (x) = f (x-1) + f (x+1) so that =》f (x+1) = f (x) - f (x-1), use the concept of generating fibonacci numbers to solve it for f (x+1)

2 Likes

Exactly, it should be 10