Page 1 of 4
11402 - Ahoy, Pirates!
Posted: Sun Jan 20, 2008 4:11 pm
by tobby
Do you have a very fast algorithm? Do we need some advanced data structure? Please give me some ideas.
Posted: Sun Jan 20, 2008 4:23 pm
by rio
I haven't tried this problem yet, but I think segment tree would work.
EDIT: Got AC using segment tree.
-----
Rio
Posted: Sun Jan 20, 2008 5:46 pm
by tobby
Can you tell me how to do the "inversion" fast with segment tree?
Posted: Sun Jan 20, 2008 6:29 pm
by Hadi
tobby wrote:Can you tell me how to do the "inversion" fast with segment tree?
You don't need to do the inversion to all nodes in the subtree. Storing some information in each node: At query time, you can decide what extra actions you should do for a node to find the correct result for that node based on the information stored at its ancestors.
EDIT: I think last problem of contest #3 of
http://www.hsin.hr/coci/ has a similar solution, although it is simpler than this one.
Posted: Sun Jan 20, 2008 6:44 pm
by tobby
It sounds not easy. I think I will come back to this problem later.

Re: 11402 - Ahoy, Pirates
Posted: Tue Apr 26, 2011 1:15 pm
by chengouxuan
can any one explain how to solve it using segmen tree in more details? i solve it by compress all pirates into a table of sqrt(n) X sqrt(n) with time O(sqrt(n)) and my algorithm runs 3.8 seconds.
Re: 11402 - Ahoy, Pirates
Posted: Fri May 13, 2011 12:48 pm
by patsp
@chengouxuan
I tried it with segment tree but got TLE. I don't know how to do the inversion fast.
Can you explain how you solved it with sqrt(n) X sqrt(n) table??
Re: 11402 - Ahoy, Pirates
Posted: Mon May 16, 2011 9:00 am
by chengouxuan
patsp wrote:@chengouxuan
I tried it with segment tree but got TLE. I don't know how to do the inversion fast.
Can you explain how you solved it with sqrt(n) X sqrt(n) table??
this is my algorithm's outline.
there are O(sqrt(n)) rows. at each query, you shouldn't process all elements in all rows that are included in this query. just record this operation on each "totally included" row. there are at most two not "totally included" rows, you can process all elements on these two rows.
there are O(sqrt(n)) row-record operations and O(2) row-processing operations. since recording a single row costs O(1) time, and processing all element in a row costs O(sqrt(n)) time, each query costs O(sqrt(n)) X O(1) + O(2) X O(sqrt(n)) = O(sqrt(n)) time.
i'm asian, sorry for my poor english, here is my email:
chengouxuan@21cn.com, i'm willing to send you my source code(400+ lines). btw, finally i solved it with segment tree, but this solution runs about 4.0 seconds.
Re: 11402 - Ahoy, Pirates
Posted: Fri May 27, 2011 6:38 pm
by howardcheng
It looks like there are cases in which M > 100, violating the problem statement.
11402 Ahoy Pirates
Posted: Sun Sep 30, 2012 1:08 pm
by devashishtyagi
I tried solving problem number 11402 (Ahoy Pirates) using segment trees but I am recieving runtime error on submissions. I have gone through the code and haven't found any obvious mistakes as of yet. My code can be found here
http://pastebin.com/ZVVRvj8s.
Thanks in advance.
Re: 11402 - Ahoy, Pirates
Posted: Sun Sep 30, 2012 4:59 pm
by devashishtyagi
I am getting a runtime error on submission of my solution. Any reason why this could happen? My code can be found
http://pastebin.com/1VhpiUDp.
Re: 11402 - Ahoy, Pirates
Posted: Mon Oct 01, 2012 11:31 pm
by brianfry713
Re: 11402 - Ahoy, Pirates
Posted: Fri Jul 05, 2013 9:21 am
by darkstallion
Hello guys! I need help for this problem. I made a solution for this problem but I keep getting WA. Can someone give me the test case that make my solution fails or can someone show me where my code flaws are. Here is my solution
http://pastebin.com/M7h109Cx
Thanks a lot !

Re: 11402 - Ahoy, Pirates
Posted: Fri Jul 05, 2013 11:13 pm
by brianfry713
Don't use getchar() and count on it to be a newline. Maybe there is extra whitespace at the end of a line.
Re: 11402 - Ahoy, Pirates
Posted: Mon Jul 08, 2013 6:29 am
by darkstallion
Thanks for your response! I have tried your suggestion and use getline instead of getchar. But the result is still WA. I think the problem is in my segment tree. But I can't find it.