This, of course, is not the most efficient space giving O (3n), but it satisfies the insertion, removal, and distance criteria listed above. The following uses a linked list, map, and unordered_map.
A list is used to maintain order. Usually inserting into a list in order is linear time, but using the key map to list_iterator we can insert constant time. The advantage of storing ordered data in a list is that it uses random access iterators, which means you can get constant time std :: distance call
The map is used to get hints about where to insert a node in the list. This is done by creating a new map entry for a given key, and then decreasing the iterator by 1. The value of this new iterator gives us the corresponding insertion position in the linked list.
Unordered_map gives us o (1) a search for random access list iterators, allowing us to get the delete time (logn) and the time at a distance.
class logn { public: using list_it = std::list<int>::iterator; void insert(int i) { const bool first = list.empty(); auto original_it = map.insert({i,list.end()}).first;
makar
source share