Hello..
I was solving this problem And i am getting TLE on test 29 but not sure why?
i believe the only reason is using map< pair<int, int>, int> but still the complexity should be log n for insertion and finding the item or am i wrong?..
The code is simple just look for the middle point and add the count of it to answer but not sure if that's the reason.
If anyone can confirm the complexity of using map< pair<int, int>, int> would be nice.
Thanks in advance.
.
Yes thank you so much for that!!..
But do you think that you know what's wrong with using map? complexity should pass i think?
.
Please do not use unordered_map except if you know what you are doing. Unordered_map is a very dangerous data structure. The reason that is because it stores its contents by hashing. Hashing is a method to convert large values into small ones. But in the process, sometimes two large values when they change into small values, they can have the same small values. In that case, if you are searching for that value, it will take $$$O(2)$$$ this time not only $$$O(1)$$$. Now, if you are in a recent contests(mostly educational rounds and Div 3/4), there is hacking. People can easily hack you by making all numbers collide with each other causing an explosion of $$$O(n)$$$ search time. Refer to this blog by neal where he explains how to secure your unordered_map. Also, you don't need map or unordered_map to solve that problem. Think how you can use frequency arrays. If there is a negative value, you can shift it so it can become $$$>=0$$$. Frequency arrays will be the fastest between unordered_map and map as it uses stack memory instead of heap.
Using a map here is not efficient, better use a 2D array. And there is no need to count the points, since "It is guaranteed that all given points are different.".
Instead move all index values to positive range by adding 1000 to x and y, so the values are within range 0,2000. Then you can use a
vector<vector<bool>> vis(2001, vector<bool>(2001));
to store the info if an point exists or not.Edit: Note also that the time consumption is not about searching the map, it is because you implicitly insert a huge amout of points into the map while searching for the middle points. It can be up to aprox 4e6 middle points checks, and each one creates an entry in the map.
Thank you, I actually didn't notice the It is guaranteed that all given points are different. somehow..
And yes i totally agree with you and i used a 2D array to get AC, But still i want to know the reason behind TLE because it should be n^2 log n.
Suppose if the problem was with constraints -1e18 <= x <= 1e18 ?
See the edit note, you can use map.find instead of the [{x,y}] notation to not create a new entry on every search.
.
Look at this code ----- >
Tips : 1.I think there is no need of map 2.As all points are distinct and we fix two points the third point gets fixed so we use binary_search() to search the third point
Thanks, I guess you are correct, but binary search takes O(logn), but if we add the points to a map and the check if they exist it takes O(1). That is why I am trying to used map or did I miss something?
Thanks!
Yes, you missed something, map(ordered) takes
O(log n)
to insert a element so even accesing is fast insertion is slow. And if you use unordered_map then read this https://mirror.codeforces.com/blog/entry/80406?#comment-665996 correct me if i am wrong