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

Автор ganeshk2, история, 9 лет назад, По-английски

I recently attempted this problem https://www.codechef.com/COOK77/problems/CHEFNUMK
using MO's algorithm (offline sqrt decomp) but got TLE. Almost everyone used the same method to reach AC. Changing values of blocks affects the complexity is known, and i tried a few values. Comparing the following code
https://www.codechef.com/viewsolution/12297863
with my code
https://www.codechef.com/viewsolution/12299180
doesn't show much difference, help ?

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

»
9 лет назад, скрыть # |
 
Проголосовать: нравится +24 Проголосовать: не нравится

You don't have to keep a BIT or any type of structure for answering. Just keep an array nr[i] = number of values in [l,r] at the actual step which have frequencies >= i. It's pretty easy to see how this array changes when you move with left or right.

To give you an example, if you move right to right + 1 and a[right + 1] = x then nr[freq[x] + 1]++ and freq[x]++. freq[x] = frequency of value x.

Having this array is pretty easy to answer the question. Is number of different values in [l,r]-nr[k + 1].