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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 160 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 151 |
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.