In yesterday's Div 3 contest in problem D, I used unordered_map to store elements, thinking it would be faster, but someone cracked my code, and my friend who had almost the same code, but used map instead of unordered_map, could not be cracked. Why was my code hacked, because unordered_map is faster than map, right? I have not been able to reach a clear answer to this question. So I ask from the respected Codeforces community, why did this happen? Thanks in advance for your answer.
Auto comment: topic has been translated by timur_jalgasbaev (original revision, translated revision, compare)
unordered_map is basically O(1) for "random" inputs, but since the problemsetters/(people who make hacks) know that people will try using it, they can basically make an input that will "break" the unordered map and make it O(n) per access. Map on the other hand is always O(logn), and so its alot more safe to use.
Thank you for help.
I did the exact same thing. This article explains very well why the
unordered_map
fails and also discuss the solution to avoid such hacks in the future.Auto comment: topic has been updated by timur_jalgasbaev (previous revision, new revision, compare).
Using gp_hash_table<int,int>mp works better than unordered_map because in the worst case, gp_hash_table operates in O(1), while unordered_map can degrade to O(n).Therefore, gp_hash_table is a better choise than unordered_map. If you use gp_hash_table, you need to include the following headers:
The article referenced above says it is even easier to hack than unordered_map.