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

Автор metal_knight, 12 лет назад, По-английски

Recently in a national contest I've found an interesting problem. Given an array of N elements and Q queries with a range . You've to find how many numbers have the highest frequency in that range? That means if the maximum frequency is , then how many numbers have in that range?

Let's call the i'th element of the array is and for each query the range is .

I can't find any feasible solution. I think this problem can be solved using Square Root RMQ with complexity per query. But couldn't find a way. How to solve this type of problem with RMQ? Any idea or hints?

UPD: Mo's algorithm is really cool :D

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

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

I believe persistent segment tree is what you are looking for.

  • »
    »
    12 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    I read this. It's about persistent segment tree. But how to implement it in this problem? Can you please explain. Thanks a lot.

    • »
      »
      »
      12 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      realy!

      We go from begin array to end and do follow things:

      For each a[i] we keep its frequency on prefix. (its easy segment tree, with add in vertex)(we press all numbers).

      But we do it like persistent segment tree(we save all state). Then we may get segment on start array, with number's frequency. (It is subtract two tree).

      And ans is count max in the tree.

      My bad english... I may answer on your question.

      • »
        »
        »
        »
        12 лет назад, скрыть # ^ |
        ← Rev. 3  
        Проголосовать: нравится 0 Проголосовать: не нравится

        I just realize how Mo's algorithm work. I have also a solution which is pretty similar to you but using Mo's algo. Count the frequency for each query in a count array. Before count the frequency of current number, decrease the value of it's previous frequency by 1 (in BIT or Segment Tree). Then after counting it increase the value of new frequency by 1 (in BIT or Segment Tree). And the answer for each query is the max in BIT or Segment Tree.

        NB: I think Mo's Technique and Sqrt Decomposition is easy to understand. But I will read about persistent segment tree(I read it at e-maxx but not sure I understand it or not). Thanks for your help.

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

This problem can be somewhere to try to pass? I have an idea for it, which is based on finding the largest increasing subsequence.

»
12 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +8 Проголосовать: не нравится

A variation of Mo's Algorithm should work I think. :)

http://codeforces.me/blog/entry/7383

Edit: Beaten by CountZero. ^^

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

I just read the problem and finds a solution that works in exactly O(Nsqrt(N) + Q*(sqrt(N))). Idea is to use Mo's algorithm(offline processing). but before that we can use coordinate compression to map the element in the lower range so that we can use array indexed based hashing and can get faster access time.

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

I think you have already got the idea otherwise ask for it. I will be more happy in letting you learn this technique.

btw can i get the link to this problem :) .