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


[2]. can you please explain me the code from line 36 to 48. i have already searched a lot on google but still not been able to get it. please help.


  [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

Delete this post and ask in a new thread now. :slight_smile:

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
alt text

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

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

@mathecodician can you please explain your approach?

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* :stuck_out_tongue:
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 :slight_smile:

19 Likes

It is failing for this case

10

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

1

4 7

The answer should be 8 while your code gives 6.

4 Likes

Thanks a lot @shraeyas

1 Like

Shouldn’t the answers be 10??

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 :slight_smile:

Sorry my bad

1 Like

Thank you @meooow i am now very much clear this Thank you once again

1 Like

Thank you @neilit1992 .your photos helped a lot Thank you once again

Can u tell now what’s wrong - http://ideone.com/126Z36

1 Like