Can anyone help me with this problem ?
I have followed the idea of this problem to solve the problem.
But the complexity becomes Q * NlogN , which gives TLE.
Any optimized idea ?
# | 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 | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Can anyone help me with this problem ?
I have followed the idea of this problem to solve the problem.
But the complexity becomes Q * NlogN , which gives TLE.
Any optimized idea ?
Name |
---|
Have you submitted the solution? The Time Limit is 15s, so try to submit it.
Yes!! I have submitted it during the contest.
If the queries are of type
[i..j]
you can try using Mo's Algorithm to sort the queries: hereThis allows solving the Q queries with O(N sqrt(N)) insertions / deletions of points.
Now, you have to think of a Data Structure that should be able to insert points, delete points, and query how many points are within Manhattan distance <= d. I believe you could use a QuadTree for that matter, and just keep the required number dynamically at each step.
For example:
When inserting a point, first query how many points are within Manhattan distance <= d from that point (which can be done using a QuadTree or KD-Tree -> see if a whole region intersects with the needed square, if it is completely within the square then just add all the points in the region -- QuadTrees should help you do that, if not recurse), and add this number to TOTAL. When deleting a point, query the same thing and then subtract from TOTAL (excluding the point itself, of course). I believe 2d range trees have a complexity of
O(sqrt(N))
, although I am not very sure. Total complexity should beO(N*Q)
, although in practice it should go better :).If you do not understand what square I am reffering to, here is a drawing:
Check something else out -> if you can make you QuadTree represent 45-degrees-rotated squares instead of normal squares, you are golden — the queries get very fast, you just have to check squares against squares. Rotating all the points 45 degrees against the origin would also be an option, although the precision issue that this option gives should make you very cautious in your implementation