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 | 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 | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
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