MR.AC's blog

By MR.AC, 23 months ago, In English

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)")

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
23 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

use ordered set(policy based set)

  • »
    »
    23 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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)

    • »
      »
      »
      23 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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

      • »
        »
        »
        »
        23 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        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.

»
23 months ago, # |
  Vote: I like it +3 Vote: I do not like it

std::distance, works in linear time.

  • »
    »
    23 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I knew there had to be something! Thank you.

    • »
      »
      »
      23 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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...

      • »
        »
        »
        »
        23 months ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

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

»
23 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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