\:precomputed tables of various functions.
\endlist
-\algn{Search in the Q-heap}
+\algn{Search in the Q-heap}\id{qhfirst}%
\algo
\algin A~Q-heap and an~integer~$x$ to search for.
\:$i\=R_X(x)+1$, using Lemma~\ref{qhrank} to calculate the rank.
\:If $x=x_i$, return immediately (the value is already present).
\:Insert the new value to~$X$:
\::$n\=n+1$
-\::$X[n]\=x$ and insert~$r$ at the $i$-th position in the permutation~$\varrho$.
+\::$X[n]\=x$ and insert~$n$ at the $i$-th position in the permutation~$\varrho$.
\:Update the $g_j$'s:
\::Move all~$g_j$ for $j\ge i$ one position up. \hfil\break
This translates to insertion in the vector representing~$G$.
\algout The updated Q-heap.
\endalgo
-\algn{Finding the $i$-th smallest element in the Q-heap}
+\algn{Finding the $i$-th smallest element in the Q-heap}\id{qhlast}%
\algo
\algin A~Q-heap and an~index~$i$.
\:If $i<1$ or $i>n$, return {\sc undefined.}
\algout The $i$-th smallest element in the heap.
\endalgo
+\para
+The heap algorithms we have just described have been built from primitives
+operating in constant time, with one notable exception: the extraction
+$x[B]$ of all bits of~$x$ at positions specified by the set~$B$. This cannot be done
+in~$\O(1)$ time on the Word-RAM, but we can implement it with ${\rm AC}^0$
+instructions as suggested by Andersson in \cite{andersson:fusion} or even
+with those ${\rm AC}^0$ instructions present on real processors (see Thorup
+\cite{thorup:aczero}). On the Word-RAM, we need to make use of the fact
+that the set~$B$ is not changing too much --- there are $\O(1)$ changes
+per Q-heap operation. As Fredman and Willard have shown, it is possible
+to maintain a~``decoder'', whose state is stored in $\O(1)$ machine words,
+and which helps us to extract $x[B]$ in a~constant number of operations:
+
+\lemman{Extraction of bits, Fredman and Willard \cite{fw:transdich}}\id{qhxtract}%
+Under the assumptions on~$k$, $W$ and the preprocessing time as in the Q-heaps,
+it is possible to maintain a~data structure for a~set~$B$ of bit positions,
+which allows~$x[B]$ to be extracted in $\O(1)$ time for an~arbitrary~$x$.
+When a~single element is inserted to~$B$ or deleted from~$B$, the structure
+can be updated in constant time, as long as $\vert B\vert \le k$.
+
+\proof
+See Fredman and Willard \cite{fw:transdich}.
+\qed
+
+\para
+This was the last missing bit of the mechanics of the Q-heaps. We are
+therefore ready to conclude this section by the following theorem:
+
+\thmn{Q-heaps, Fredman and Willard \cite{fw:transdich}}\id{qh}%
+Let $W$ and~$k$ be positive integers such that $k=\O(W^{1/4})$. Let~$Q$
+be a~Q-heap of at most $k$-elements of $W$~bits each. Then the Q-heap
+operations \ref{qhfirst} to \ref{qhlast} on~$Q$ (insertion, deletion,
+search for a~given value and search for the $i$-th smallest element)
+run in constant time on a~Word-RAM with word size~$W$, after spending
+time $\O(2^{k^4})$ on the same RAM by precomputing of tables.
+
+\proof
+Every operation on the Q-heap can be performed in a~constant number of
+vector operations and calculations of ranks. The ranks are computed
+in $\O(1)$ steps involving again $\O(1)$ vector operations, binary
+logarithms and bit extraction. All these can be calculated in constant
+time using the results of section \ref{bitsect} and Lemma \ref{qhxtract}.
+\qed
+
\endpart