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

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

I have a vector of pairs of length N. Now i have Q queries of type, given range let say L to R, find the minimum value of second element of pair whose first element is equal to given number K.

For example : vector of 6 elements [{2,5},{8,7},{2,3},{8,6},{2,1},{8,4}] Now for query : L=3 R=6 (1-based indexing) and K=8

Answer would be -> 4 which corresponds to this pair {8,4}

There are 10^5 queries and N can be upto 10^5.

can someone give me an idea how to approach this problem more efficiently?

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

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

Auto comment: topic has been updated by dreamplayDaddy (previous revision, new revision, compare).

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

Is k fixed?

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

We can try like this:

Make a vector for each value of K. The first value of the pair is the second element of the original pair, and the second value is the position in the original array. In the problem it will look like :

2->{5,1}{3,3}{1,5}

8->{7,2}{6,4}{4,6} (each vector is sorted on the basis of position(2nd value of pair))

then build a min-segment tree for each vector[K]. for each query, using lowerbound/upperbound, find the indexes for the search in the segment tree.

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

If you can solve it offline you could order the queries by increasing k.

Then, for every different k, add to the segment tree all pairs that have the first element equal to k. Now you could simply query the segment tree for the minimum element of range [l, r] as we know that the condition will be always met. As soon as we are done processing all queries of the same k, remove the pairs we previously added.

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

You can probably use square root decomposition. Partition the vector of pairs into $$$\sqrt{N}$$$ blocks.

For each block maintain a map which stores the minimum second element corresponding to the first element in the pair.

For each query you'll be going over $$$ O(\sqrt{N}) $$$ complete blocks and $$$ O(\sqrt{N}) $$$ elements that don't belong to complete block and have to be catered to one by one.

Complexity per Query will be $$$ O(\sqrt{N} log(N)) $$$ which can be reduced to $$$ O(\sqrt{N}) $$$ with coordinate compression ( costs $$$ O(Nlog(N)) $$$ worth of pre-processing) if elements of the pair are large or using lookup array if they are smaller.

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

There exists better solution for XORMIN, which doesn't use this and finds both answers at same time.

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

You can just use segment tree, where in every node you need to have a treap that contains all values in that subtree, and every node of the treap contains minimum of the second elements, now you can answer all queries in O(log^2n) time.