Блог пользователя Namritaansh02

Автор Namritaansh02, история, 3 года назад, По-английски

This is the problem. I am using sparse table and implementing it exactly as given in the editorial but I am getting, TLE on test 3. I have been stuck for 2 days now on this:-(

Can anyone point out the mistake? Thanks.

This is my submission : 124743771

  • Проголосовать: нравится
  • +5
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится +27 Проголосовать: не нравится

Let's look at the last for cycle in your solution. For every i your solution bruteforces all possible values of j (in increasing order). If array is something like this: $$$[2, 4, 6, 8, 10, ..., 2n]$$$ then for every i the correct j is n — i. In total, there would be $$$n + (n - 1) + ... + 1 = \frac{n(n - 1)}{2} = O(n^2)$$$ checks, it is too slow.

Instead of this, you should binary search on j. Then you wouldn't check more than $$$O(n\log{n})$$$ indices, that is much better.

NOTE: You might want to optimize your power function. It works in $$$O(\log{x})$$$, so if you will write binary search, the total complexity will be $$$O(n\log^2{n})$$$. It could also TLE. You can precompute all the logarithms in some array, thus deleting extra log factor.