Hash Table
Translated from Chinese by an LLM.
0. Prerequisites
- Required: “Arrays /
vector”, “Time complexity analysis” - Strongly related: “
Hashfunctions”
1. Basic Concepts
- Definition:
Also called a
hash table- a data structure that stores data in key-value form.
- What problem it solves: Slow insert, delete, update, and query. A hash table achieves nearly time complexity.
- Core idea: Trade space for time.
- Key terms: Load factor: Resize: when , increase the bucket count.
2. Algorithm Flow / Derivation
- Step 1: Allocate a large contiguous block of memory.
- Step 2: Take an input
keyandhashit. - Step 3: Look up the memory at the
hashposition and read or write the value. - Step 4: Check the
bucket countandelement count; if the threshold is exceeded,resize.
Why this is correct:
- Every
keyhas a uniquehash.
Diagram / Manual example:
Insert
Delete
Search
Collision handling

3. Complexity Analysis
Ideal case
| Complexity | Best | Average | Worst |
|---|---|---|---|
| Time | |||
| Space |
- Where the complexity comes from:
- Feasible data range: limited by memory size.
4. Applicable Scenarios
- Suitable problem types: simplifying computation, heavy insert/delete/update/query.
- Data range: no restriction.
- When not to use / failure cases: insufficient memory.
rarely happens
5. Code Template
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
unordered_map<int,int> _map;
return 0;
}6. Key Points & Caveats
- Boundary conditions: None.
- Common pitfalls: Haven’t implemented one by hand yet - will update when I do.
- Optimization tips:
This itself is the optimization technique. Some problems can be optimized with
unordered_map. - Easily confused points:
mapis implemented with ared-black tree.unordered_mapis implemented with ahash table.
7. Example Problems
| Problem | Source | Difficulty | Link |
|---|---|---|---|
| [Template] Hash Table | Luogu | Orange | P11615 |
P11615:
- Problem:
Fast-read numbers and store them in a
hash table. Some numbers share congruence relations, causingTLE. - Approach:
Hand-write a
hash tablefor heavy insert/delete/update/query. Due to congruence relations, a bijection or a hand-writtenhash tableis needed.char buf[1 << 23], *p1 = buf, *p2 = buf; #define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) inline void rd(ull &x) { // Read a 64-bit unsigned integer x = 0; char ch = gc(); while (!isdigit(ch)) ch = gc(); while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = gc(); } // A trick: use this function to build a bijection and break congruence relations inline void fuck_rd(ull &x) { x = 0; ull len = 1; bool flag = false; char ch = gc(); while (!isdigit(ch)) ch = gc(); while (isdigit(ch)){ int cur = (ch ^ 48); if(cur){ x += len * (ch ^ 48); ch = gc(); if(flag) x -= len/10; len *= 10; flag = false; }else{ x += len; ch = gc(); if(flag) x -= len/10; len *= 10; flag = true; } } x += len; // x += len * (positive integer) prevents excessive collisions when the bijection is reversed if(flag) x -= len/10; } - Pitfall: A large number of numbers sharing congruence relations causes
unordered_mapto degrade to .
8. Related Algorithms / Extensions
- One-line summary: insert, delete, update, and query.