I'm stuck in this problem:
http://acm.timus.ru/problem.aspx?space=1&num=1989
It gives me TLE on test 15, my code uses Hashing and Fenwick Tree, so it's O(mlogn). The time limit is only 0.5s. Is there a better solution? (an O(m) solution).
Thanks in advance.
I got accepted on this problem with almost the same approach as you suggested. The only difference is Segment Tree instead of Bit. You probably have a bug somewhere in your code.
You should post your code...
Here is my code, sorry that I put it here like this, right now I don't have access to ideone.
PD: I've updated my code, I realize that I don't need modular inverses (to compare the hashings) at all. But it still gives me TLE on test 16. In my PC with a full test case it runs in ~0.25s.
Any ideas why it gives me TLE? I found a solution on the internet which uses segment tree and it gives AC. I mean WTF?? (segment tree has a bigger constant that Fenwick Tree)
I think that you have TLE because use mod operation many times ( this operation is slow ). You can easily decrease search operation to O(1). Use array, at position i you store sum from 0 to i. Now sum in [L;R] is sum[R] — sum[L-1].
How can we handle the update operations?
It's my mistake :) I forgot for update operations...
Is there any way to improve the complexity of the modulo operation??