I tried solving D-Query http://www.spoj.com/problems/DQUERY/ using Mo's algorithm, but my code keeps giving Segmentation Fault, have been trying for the past hour and still can't debug it. Would appreciate it if someone can help. Thanks. Here is my code. http://ideone.com/o9qLUW asked 14 Jun '17, 16:34 ![]()
|
Your solution looks correct, but just three comments:
I believe these last steps will be enough to get your code to pass :) answered 14 Jun '17, 18:08 ![]()
1
Thank you so much it finally worked. Never thought the compare function would be the main culprit.
(14 Jun '17, 18:29)
|
Let the 1st query be 1 3,then initially your curL=0 and you are accessing cnt[a[curL]] but you are taking input as 1-based and hence a[0] would be some garbage value.You have declared cnt array of size 1000005.If a[0] is greater than 1000005 then you are going out of bounds which will result is segmentation fault. BTW,here is my accepted solution if you have any doubt feel free to comment https://pastebin.com/mRqp9ha1 answered 14 Jun '17, 16:56 ![]()
It still gives segmentation fault after I initialized a[0] as 0, also since I have declared it globally it should have already been 0 initially to begin with.
(14 Jun '17, 17:02)
try to compare your code with my code
(14 Jun '17, 17:07)
|