Wa in GSS1 spoj Getting WA on 9th test case link to question : http://www.spoj.com/problems/GSS1/ Answer : http://ideone.com/zcPCr3 asked 21 Jun '17, 20:30 ![]()
|
Hey @vijju123 , I am also stuck on the same question. I have implemented the same thing that you have described in previous answers still I am getting WA. Could you please help! Code:
answered 08 Dec '17, 22:28 ![]()
I cannot have a look right now dear, in middle of exams till 14th :(
BTW, Are you sure that you are printing answer of each query on a new line??
(08 Dec '17, 23:00)
ok...just have a look at it when you are free. And regarding that newline , removed fast io code, so changed it to printf and scanf. P.S.: Also tried submitting the above code...getting tle XD
(09 Dec '17, 10:19)
|
@vijju123 Cleared Bro. Thanks a lot for taking the time and clearing my doubt.Will you recommend me any other good problems on Segment Trees? answered 07 Nov '17, 12:40 ![]()
I think K-Frequency was a nice problem. It uses same concept, and is good to make sure that you got the concept idea. Then go for PRMQ from long contest (It appeared in one of long contest held b/w April-June- cant remember exact deets atm)
(07 Nov '17, 12:48)
Sure bro. Thanks!
(07 Nov '17, 12:55)
|
Ok, so you want the reasoning behind it? Firstly, I assume that you know why MaxSum of Left Node+ MaxSum Of Right node is wrong. Now, say our nodes have information about sub-arrays- L={-20,10,10} (index say, 0 to 2) and R={10,10,-20} (index say, 3 to 5). Now our parent node stores by combining results of L and R. So parent node stores result for {-20,10,10,10,10,-20}. Ofc, the answer for query is 10+10+10+10=40. But look at values. For L, Sum=0, Maxsum=20, PrefixSum=0, Suffixsum=20. For R,Sum=0,Maxsum=20,PrefixSum=20,SuffixSum=0; Now when we combine segments L and R, we get collective segment {-20,10,10,10,10,-20}. Focus on how this segment {10,10,10,10} got formed. Isnt it the "Best Suffix sum of node L + Best Prefix Sum of Node R"? If you argue for Maxsum of L + Max sum of R, then an easy counter example is {10,10,-200,-200,10,10}. Maxsum of L + Maxsum of R gives 20+20=40, but its not possible because positions must be contiguous. But when we add suffix and prefix sum, then its obvious that the positions are contiguous, for we calculated those 2 that way! Still any doubt that why its Best Suffix Sum of L + Best Prefix Sum of R? answered 07 Nov '17, 01:17 ![]()
|
@vijju123 Please find my mistake, I wrote the getMaxSum function as you mentioned above
answered 06 Nov '17, 19:15 ![]()
Look at your getMAxsum function. Now look at qs,qe , and l and r. You interchanged roles of qe,qs,l and r. Look here-
I think you interchanged positions of qs,qe ,l and r in function definition
(06 Nov '17, 23:37)
No, I haven't. qs->query starting, qe->query ending, l and r are initially set to 0 and n-1. I didn't change the order.
(06 Nov '17, 23:49)
Okay, I got confused by the names XD. From analysis of your code over outputs, I think you are doing it as "Max of- MaxSum of left node, MaxSum of right node, Maxsum of LeftNode+Maxsum of Right Node" But this is wrong. Eg- for this case-
It considers max sum of left node, 6, then max sum of right node, 11, and their sum 17. Your code gives 17 as output here, but its not possible as theres a -200 in between. Correct expression is
(07 Nov '17, 00:03)
Can you give me the expression that I should include in my code?
(07 Nov '17, 00:10)
You need to do some tweaking in order to get the
(07 Nov '17, 00:12)
Not getting bro. Please explain why should we take max(leftMax,rightMax,sum of leftMaxSuffix+rightMaxPrefix)
(07 Nov '17, 00:43)
showing 5 of 6
show all
|