Map vs Unordered_map

Revision en1, by __dopamIne, 2025-08-12 16:26:37

In competitive programming, we often use map or unordered_map. However, choosing the wrong container can sometimes cause our code to hit a Time Limit Exceeded (TLE). Today, I will share with you why the unordered_map get TLE ?

1.Hash collisions → Worst case O(n) instead of O(1). 2.Adversarial inputs → Test cases designed to break default hash. 3.String hashing cost → Large strings slow down lookups. 4.Higher constants → More memory & overhead than map.

https://mirror.codeforces.com/problemset/problem/2131/C

In this problem, using unordered_map in the same code will lead to a TLE, whereas replacing it with map in the exact same scenario will result in an Accepted verdict.

So always use a custom hash if you don't want to get blown up.

Tags data structures

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English __dopamIne 2025-08-12 16:26:37 777 Initial revision (published)