Overview
This module provides advanced data structures optimized for competitive programming, including specialized stacks and queues with min/max queries, tries, and Mo’s algorithm for range queries.Stack with Min/Max Queries
A stack that supports retrieving the minimum or maximum element in O(1) time.Methods
Add element to the top of the stack
Remove element from the top of the stack
Return the top element without removing it
Return the min/max of all elements in the stack in O(1) time
- All operations: O(1)
- Space: O(n)
Queue with Min/Max Queries
A queue that supports retrieving the minimum or maximum element in O(1) amortized time.- Push: O(1) amortized
- Pop: O(1) amortized
- Min/Max query: O(1)
- Space: O(n)
Trie Data Structures
Dynamic Trie
A trie (prefix tree) for efficient string operations.Insert a word into the trie
Check if a word exists in the trie
Check if any word in the trie starts with the given prefix
- Insert: O(L) where L is word length
- Search: O(L)
- StartsWith: O(L)
- Space: O(ALPHABET_SIZE * L * N) where N is number of words
Trie Automaton
A trie with failure links for pattern matching (similar to Aho-Corasick). Complexity:- Build: O(total length of all patterns)
- Query: O(text length + number of matches)
Mo’s Algorithm
An algorithm for answering range queries offline by reordering them optimally.- Time: O((N + Q) * √N) where Q is number of queries
- Space: O(N + Q)
Median Data Structure
Maintains a dynamic set of numbers and efficiently computes the median.Add a number to the data structure
Return the median of all numbers added so far
- Add: O(log n)
- Find Median: O(1)
- Space: O(n)
Tree Order Statistic
Supports finding the k-th smallest element and rank queries using policy-based data structures.Methods
find_by_order(k)- Returns iterator to k-th smallest element (0-indexed)order_of_key(x)- Returns number of elements strictly less than x
- Insert/Delete/Find: O(log n)
- Order statistics: O(log n)
Min/Max Heap with Map
A heap that supports efficient updates using a map for tracking elements.- Insert: O(log n)
- Get Min/Max: O(log n)
- Remove: O(log n)
- Update: O(log n)
Available Data Structures
| Data Structure | File | Key Feature |
|---|---|---|
| Min/Max Stack | data_structure_stack_min_max.cpp | O(1) min/max queries |
| Min/Max Queue | data_structure_queue_min_max.cpp | O(1) amortized queries |
| Dynamic Trie | data_structure_trie_dynamic.cpp | Prefix queries |
| Trie Automaton | data_structure_trie_automaton.cpp | Pattern matching |
| Mo’s Algorithm | data_structure_mos_algorithm.cpp | Offline range queries |
| Median Finder | data_structure_median.cpp | Dynamic median |
| Order Statistic Tree | data_structure_tree_order_statistic.cpp | k-th element queries |
| MinMax Heap (Map) | data_structure_minmax_heap_map.cpp | Updatable heap |
| MinMax Heap (Multiset) | data_structure_minmax_heap_multiset.cpp | Multiset-based heap |
See Also
Range Query Structures
Segment trees, Fenwick trees, and sparse tables
Graph Algorithms
Graph data structures and algorithms