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

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

Hi,

Given an Array with N elements each element have 3 integers Say Ai,Bi,Ci.

you have to answer Q querys given in each query 3 integers X,Y,Z you have to say how many elements in the array satisfy Ai>=X , Bi>=Y Ci<=Z

N,Q<=10^5

I hope you help me or give me a hint, thanks.

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

»
12 лет назад, # |
Rev. 8   Проголосовать: нравится -15 Проголосовать: не нравится

You have to use segment tree and BS Tree their subtrees' roots being hashed (using perfect hashing !) and finally BS on answers while also balancing the red-black tree, and maintaining the fact that the sum is even if the number of subtrees' nodes are 2^p-1, where p is any prime number.

Also take into account that 64MB stack is not enough to recursively solve the problem. You gotta use an iterative method.

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

Can we use 3-D Binary Indexed Tree? We need to find number of points in the box {x, y, z | 0 <= x <= X, 0 <= y <= Y, 0 <= z <= Z}. To fill the tree we will to update N*log(N)*log(N)*log(N) points in 3-D which can be hashed.

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

You can just sort array (using your own compare function) and use binary search.

To better understanding imagine, that each element has only one integer.

Here is my solution.

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

    I'm afraid your solution is incorrect.

    It gives wrong answer for the test like this:

    1

    2 2 2

    1

    1 1 1

    It should be 0, but your solution outputs 1.

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

      But is it possible algorithm: sort + binary search for the first is greater than or equal? If not, tell why, please.

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

        I'm pretty sure it's impossible. Let's assume that all elements of the array have different a[i]. In this case, binary search will find the first element comparing a[i] only. But it doesn't use any information about b[i] and c[i](I mean that for some elements, a[i] might fit the constrains but b[i] or c[i] might not).

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

    Life of programmers is not that easy :) ,your code gives Wrong Answers

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

There's an offline solution which uses O((N + Q) * log(N + Q) + Q * (log N)^2) time and O(Q + N * log N) space.

Let's assume that each element of the array is a point in 3-D space.

1)At first, shrink all the Y-coordinates.

2)Then build a segment tree for Y-coordinate and store vector with all Z-coordinates of points which are covered by the segment represented by the tree's node. Moreover, keep a Fenwick tree in each node. Initially, all points should be in the tree and each Fenwick tree should contain 1 at all the positions.

3)After that, you may use sweep algorithm line to answer the queries. There should be two types of event: a point disappearing and a query. Events have to be sorted in non-descending order of X-coordinate.

To process the point(X, Y, Z) disappearing event, just modify one element in respective Fenwick tree which represents (Y, Z) point(adding -1 there) in the segment tree.

To process a query event, simply calculate the sum in (Y, 0, MAX_Y, Z) rectangle using the segment tree.