I have n two dimensional points and some rectangle.For each rectangle I have to answer how much point is inside of that rectangle? How can I answer this with complexity log(n)?
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
I have n two dimensional points and some rectangle.For each rectangle I have to answer how much point is inside of that rectangle? How can I answer this with complexity log(n)?
Name |
---|
Find count point in rectangle is a standart problem.
It problem can solved with segment tree.
In each vertex we must save all vertex of need segment in sort array.
Then, we find need segment for one coordinate and make request in segment tree in need segment.
More information on Russian language: http://e-maxx.ru/algo/segment_tree#15
The simplest solution came to my head is the solution using persistent segment tree. Imagine that for each X coordinate you have a segment tree in which tree[Y] is the number of points(x, y) that have y == Y and x <= X. Then if you want to know how much points are inside of a rectangle with upper left vertex (X1, Y1) and lower right vertex (X2, Y2), the answer will be the sum from Y1 to Y2 in a segment tree with X == X2 minus the sum from Y1 to Y2 in a segment tree with X == X1 — 1. This solution takes O(Nlog(MaxY)) time and O(N * MaxY) memory where N is the number of points plus the number of rectangles. But if you use persistent segment tree, you'll decrease the memory usage down to O(Nlog(MaxY))
Ox, Oy and rectangle edges are parallel, you can use 2D fenwick tree.
See this.