http://www.cplusplus.com/forum/general/112111/
I understand that you don't have access to the testcases used by the server, however you should create your own.
Try the limits of the problem, try corner cases, try random cases.
You have an invalid access in the array `marked'.
Your algorithm would visit the numbers from 2 to `r2', `marked' may only hold 100000 elements, however the input can be a lot bigger than that.