Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

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

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

I tried to implement the problem using segment tree and multisets. I got a verdict of TLE. How can I optimize my code? Or is there a better solution?

Problem link: http://www.spoj.com/problems/KQUERY2/en/

Solution link: http://ideone.com/gDAmUW

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

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

Both N and A[i] are quite small, so maybe something like sqrt decomposition with fenwick tree in each block would pass.

  • »
    »
    8 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    It seems that u will get ~1000 operations for each query with this approach. Idk, mb 2 * 10^8 will pass in 0.85s on spoj

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

You can try using an array instead of the multiset to get O(log^2 N) complexity per query.

Depending on how the multiset is implemented you will get extra O(log N) factor of a big constant.

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

    If I use an array will it not cause me a MLE? I mean I need to create a 2D one. Or am I not getting your idea? If it's not too much of a trouble can you please elaborate a bit?

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

      i believe he ment to do following: in each node of ur seg.tree store sorted array of elements of its subtree instead of multiset. This doesnt work anyway i believe.

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

    If u use array instead of multiset u'll get like O(nlog^2(n)) rep update query, wont u? i mean u'll have to resort arrays all the time.

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

your approach is incorrect

int id = distance(tree[node].begin(), it);

this works in O(n)

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

I think u should use 2-D BIT

  • »
    »
    8 лет назад, # ^ |
      Проголосовать: нравится +5 Проголосовать: не нравится

    Lol the memory limits are so lenient that you don't even need maps — you can literally just do

    short bit[30010][10010];
    

    and still have over half the memory to spare

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

You can find the solution here: http://codeforces.net/blog/entry/18470