Given n integer coordinates of the form (x, y) and Q queries in which a pair (a, b) is given. For each query output the number of coordinates whose x<=a AND y<=b.
Q<100000 N<1000 1<= x, y, a, b <= 1000
№ | Пользователь | Рейтинг |
---|---|---|
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 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
Given n integer coordinates of the form (x, y) and Q queries in which a pair (a, b) is given. For each query output the number of coordinates whose x<=a AND y<=b.
Q<100000 N<1000 1<= x, y, a, b <= 1000
Название |
---|
2D partial sum DP will do the job.
DP[i][j] - the number of points with x <= i and y <= j
. The recurrence is easy.DP[i][j] = DP[i-1][j] + DP[i][j-1] - DP[i-1][j-1]
.Thanks :)
How to solve if there is query is of OR form i.e. (x<=a || y<=b)?
Using 2D prefix sum you could actually answer any query of type — "How many points exists in range xi <= x <= xf && yi <= y <= yf." After building your 2D Prefix Table you would need to get the sum in a given rectangle. That can be done using the following formula: dp[xf][yf] — dp[xf][yi — 1] — dp[xi — 1][yf] + dp[xi — 1] + dp[yi — 1].
sort by X, fenwick tree by Y, NlogN
$$$Q \log N$$$ or how do you answer queries?
Of course, N in my message is (N+Q)
I am stuck in a similiar problem actually. And 2D Prefix Sum can't be done due to Memory restrictions. Could you explain a little further how Sorting + Fenwick would work ? If I sorted by X and the Fenwick by Y, how would I be sure that the corresponding Y in the Fenwick belong to the points in the range [0 — X] ? Thanks !
Try problem 652D - Nested Segments (it is almost the same), read editorial and comments below the editorial.
You can strengthen the problem into an online version: dynamically insert and query. For that, use 2D BIT.