Well at least the sigsegv is gone.
When you’re stuck try writing a test case generator and compare the output of your program to one of the solutions of the contest that got AC.
When you found a test case that gets you differing output the debugging “fun” can begin. 
And when this doesn’t get you anywhere clean up your code and ask here again. The code you linked is a mess. Give your variables meaningful names, not k, co, fg etc. Use standard c++ and not dirty hacks like variable length arrays (scanf("%d%d",&n,&q);int a[n]; - this is not legal c++).