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

Блог пользователя MR.AC

Автор MR.AC, 2 года назад, По-английски

I came across an issue when solving problem C in today's contest. I was trying to find the distance between two pointers (by subtracting) in a set to determine the number of elements between those two pointers. However, I got an error. Is there a way to find this distance?

Submission (under "if(t == 3)")

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

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

let's consider that you have two pointers , the left one in position l and the right one in position r , then if you want the number of elements there (including the pointers) it is r-l+1 , if it was without them it is r-l-1 , and if it was without one of them it is r-l

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

    but you have to make sure that l<r or l<=r (as you want) so it is a positive value

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

      Oh my bad I knew that but the issue was not about that, it was about a compilation error.

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

use ordered set(policy based set)

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

    That might be a good idea. If I am trying to find elements in range L to R, should I do something like order_of_key(R+1) — order_key(L)? (reminds me of prefix sums)

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

      This correctly gives #of rooks between rows L,R, but that's different than "at least one rook in each row in range [L,R]" which is what the problem asks

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

        However, I am using a set combined with a multiset to ensure that only distinct rooks are stored. (Please check my most recent submission) Therefore, in my case, it can be used to check if the condition is satisfied.

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

You can't do it with a set, because a set doesn't have random access iterators.

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

    Okay, thanks for clarifying. It must have something to do with an underlying binary structure that prevents linear access.

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

      if you want to solve the problem use ordered_set or a fenwick tree/segment tree.

      this is my solution using a fenwick tree 157240227.

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

        Will take a look. I definitely need to learn more about those two...

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

std::distance, works in linear time.

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

    I knew there had to be something! Thank you.

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

      Note that it works in linear time for each call of the function, so if you were to use it in your solution, it still would not pass because the total complexity would be $$$O(n^2)$$$. I'm 99% sure that the implementation for std::distance with not random access iterators just increments the smaller one repeatedly until it reaches the bigger iterator...

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

        Yeah, I tried it and it got TLE on test case 5. But I am sure it will be useful elsewhere!

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

You can't do this with set. You can use ordered_set which requires some installation. Or if you are first inserting then doing queries, i.e You don't insert anything after you make first query, you can use Coordinate Compression and then use Fenwick Tree/Segment tree