I was solving the problem : 1732D1 - Balance (Easy version). And my solution was same as i keep the track of the kth-MEX of the xth element when ever the request has been made. So for checking whether the kth element is present in the set or not, i used unordered_map<> by while adding the elements in the unordered_map<> when the add operation is processed. But i got TLE at test 36. Later when i checked other's solution they used ordered_map<> and used the .find() function to check whether the element is added to the set or not. And then i also used that .find() function and it got accepted.
Can anyone help tell me why by using mapping with unordered_map<> gives TLE and get Accepted while using .find() function?
Read this blog: https://codeforces.me/blog/entry/62393
There is performance difference between std::unordered_map and std::map in terms of finding elements and to understand this you need to know the internal implementation of these data Structures.
std::unordered_map
:std::map (ordered_map)
:So, why might you observe a TLE (Time Limit Exceeded) when using std::unordered_map::find but not when using std::map::find?
The most likely explanation is that your std::unordered_map is experiencing worst-case behavior due to hash collisions. In cases where there are many hash collisions (e.g., if your hash function is not well-distributed or if you have a lot of elements with the same hash), the std::unordered_map can degrade in performance and, in the worst case, become slower than a std::map for finding elements. This can lead to TLE errors.