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 | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
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