Manuscript Offprint
arXiv:2610.38327Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach
Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.
Abstract
Given a 1D array over a total order and a query position in the array, a next larger value query returns the nearest position to the right whose value is larger than the value at the query position. Next smaller value queries and previous larger/smaller value queries are defined analogously. These queries are closely related to the k-th range minimum/maximum problem, which asks for the position of the k-th leftmost minimum/maximum value when the query range contains multiple minimum/maximum values.
In this paper, we consider the encoding problem for answering any arbitrary subfamily of these four queries, where the queries must be answered without accessing the input array. We give a unified approach for obtaining space lower bounds for such encodings. We also obtain data structures for the query subfamilies that match the lower bounds up to lower-order additive terms, while supporting the queries in constant time. Our results generalize and improve several previous results on this problem [Gawrychowski and Nicholson, ICALP-2015; Jo and Satti, TCS-2016; Tsur, IPL-2019; Jo and Kim, Algorithmica-2025].
In addition, these results give tight bounds on the encoding complexity of all subfamilies of these queries for (1) permutations, (2) arrays without consecutive equal elements, and (3) general arrays.
2012 ACM Subject Classification Theory of computation → Data structures design and analysis
Keywords and phrases Encoding data structures, Colored 2d min/max heaps, Generating trees, Quarter-plane walks
1 Introduction
Given an array over a total order, and a query position , we consider the following queries:
-
, the previous smaller value query, returns , or 0 if no such position exists.
-
, the previous larger value query, returns , or 0 if no such position exists.
-
, the next smaller value query, returns , or if no such position exists.
-
, the next larger value query, returns , or if no such position exists.
We also consider the following range queries. Given an interval and a positive integer , let be the positions of the minimum element in , and let be the positions of the maximum element in .
-
The -th range minimum query, denoted by , returns if , and otherwise.
-
The -th range maximum query, denoted by , returns if , and otherwise.
We consider the above queries on the following classes of arrays:
-
, where denotes the set of all permutations of length .
-
, where denotes the set of all arrays such that for every .
-
, where denotes the set of all arrays of length .
These queries are widely studied primitives for accessing the order structure of an array. Previous/next smaller value queries are used to support navigational queries on compressed suffix trees [22]. They also arise in the all-nearest-smaller-values problem, which has been studied in the context of parallel algorithms [1]. Also, as a special case of -th range minimum queries, the problem to find the median among them (range minima in the middle queries) was considered by Fischer and Heun [10] for efficient construction of compressed suffix arrays.
We study the problem of designing data structures for each subfamily of on each of the three array classes. We work in the encoding model [23]: after preprocessing, the input array is discarded, and all queries must be answered using only the stored representation. In this model, if a query family distinguishes equivalence classes of input objects, then any representation requires at least bits; we call this the information-theoretic lower bound (ITLB). Here, two objects are equivalent if the queries in the family give identical answers on them. A representation matching the ITLB up to lower-order terms is called succinct. In many cases, the ITLB is much smaller than the space needed to store the input array itself [23]. Throughout the paper, we assume the word-RAM model with word size .
We also consider range queries because of their close connections to the previous/next smaller/larger value queries. From a lower bound perspective, and induce the same equivalence classes of arrays, as do and , and hence have
All logarithms are base two. Throughout the paper, we omit ceilings and floors when they affect the bounds only by an additive constant.
Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach
the same ITLBs (see Proposition 5). For upper bounds, colored 2d-min heaps [9] provide a structure for supporting PSV, NSV, and RMin simultaneously; colored 2d-max heaps similarly support PLV, NLV, and RMax (see Section 2 for definitions).
Fischer and Heun [11] matched this bound with a -bit data structure supporting PSV and range minimum queries in constant time. Their representation is based on the 2d-min heap, an alternative representation of the Cartesian tree; see Section 2 for the definition. The same data structure also applies to general arrays with the same space bound; in the presence of equal minima, it returns the rightmost minimum in the query range, which corresponds to in our notation.
Fischer and Heun [10] also considered the problem of finding a minimum whose rank among all minima in the query range lies in the middle (range minima in the middle queries). For general arrays, they gave a data structure using additional bits that returns, in constant time, a minimum position whose rank among the minimum positions in the query range lies between and .
Fischer [9] studied the simultaneous support of PSV and NSV. For permutations, NSV can be supported in constant time using the data structure of Fischer and Heun [11]. For general arrays, Fischer introduced colored 2d-min heaps and gave a data structure using bits that supports PSV and NSV in constant time. He also proved a matching lower bound based on the little Schröder numbers. Later, Jo and Satti [17] observed that this representation can also support for arbitrary in constant time.
Gawrychowski and Nicholson [13] considered the simultaneous support of PSV and PLV on permutations. Their representation combines the 2d-min and 2d-max heaps and supports range minimum and maximum queries in constant time using bits. They also showed that this representation is succinct by proving that a Baxter permutation can be reconstructed from its answers to PSV and PLV queries, yielding an ITLB of bits. Jo and Satti [17] showed that the same bound can be achieved for arrays in . For general arrays, they also gave a representation using bits with query time.
For the full query family together with and , Jo and Satti [17] gave a data structure for using bits with constant query time. For , they gave two data structures: one using bits with linear query time and another using bits with constant query time. Tsur [26] improved the space to bits for and bits for , but with linear query time. More recently, Jo and Kim [16] obtained the same space bounds up to lower-order terms and improved the query time to for any constant , where and . They also established a lower bound of bits for supporting on arrays in .
OEIS identifiers refer to entries in the On-Line Encyclopedia of Integer Sequences [21]
The same lower bound is also implied by the bijection of Dulucq and Guibert [7], which shows that pairs of minimum and maximum Cartesian trees are counted by the Baxter numbers (A001181).
| Array type | Space (in bits) | Query time | Reference |
|---|---|---|---|
| Upper bounds | |||
| [9, 13, 17] | |||
| [17] | |||
| [26] | |||
| [16] | |||
| Theorem 2 | |||
| [17] | |||
| [17] | |||
| [26] | |||
| [16] | |||
| Theorem 2 | |||
| Lower bounds | |||
| [7, 13] | |||
| Theorem 1 | |||
| [16] | |||
| Theorem 1 | |||
Table 1 Summary of lower and upper bounds for the full query family. The stated lower bounds hold for both {PSV, PLV, NSV, NLV} and {RMin, RMax}. The upper bounds support PSV, PLV, NSV, NLV, RMin, and RMax simultaneously.
1.2 Our results
We obtain the first ITLBs and succinct data structures for the full query family on and , resolving the open problem of constructing succinct representations for this family [17, 16]. These results are stated in the following two theorems.
► Theorem 1 (Lower bounds). The ITLB for simultaneously supporting PSV, PLV, NSV, and NLV is
-
bits for arrays in ;
-
bits for arrays in .
The same ITLBs hold for simultaneously supporting RMin and RMax on the respective array classes.
► Theorem 2 (Upper bounds). There exist data structures that simultaneously support PSV, PLV, NSV, NLV, RMin, and RMax in constant time using
-
bits for arrays in ;
-
bits for arrays in .
Table 1 compares our lower and upper bounds with previous results for the full query family.
More generally, we give a unified approach to deriving ITLBs for every subfamily of {PSV, PLV, NSV, NLV} on the three array classes , , and . We also give succinct data structures for every such subfamily, matching the corresponding ITLBs up to lower-order terms while supporting queries in constant time. Table 2 (in Appendix A) summarizes the lower and upper bounds for these query subfamilies across the three array classes.
The rest of the paper is organized as follows. In the main text, we focus on the lower and upper bounds for the full query family. Section 2 introduces the heap structures used
Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach

Figure 1 Examples of Min(A), cMin(A), and cMax(A) for .
throughout the paper and their connections to the query families. Section 3 establishes the ITLBs for the full query family, and Section 4 presents matching succinct data structures supporting these queries in constant time. Due to space constraints, some proofs from Sections 3 and 4 are deferred to Appendices B and C, respectively. The lower bounds and data structures for the remaining query subfamilies are presented in Appendices D and E, respectively.
2 Preliminaries
2d-min heaps. Given an array , the 2d-min heap on , denoted by , is a rooted ordered tree with nodes labeled satisfying the following properties:
-
the root is labeled 0,
-
for every , the parent of the node labeled is the node labeled , and
-
the preorder traversal of visits the nodes in the order of their labels, . Similarly, the 2d-max heap on , denoted by , is defined by replacing with . Here, we simply denote a node labeled by a node .
Fischer and Heun [11] showed that supports and queries on . Thus, by maintaining both and , , , , and queries can be supported simultaneously.
Input: ; assume Output: 2d-min heap on 1 Initialize a stack and 2 Set node 0 as the root 3 for do 4 while do 5 | 6 end 7 8 Append node as the rightmost child of node 9 10 end
Algorithm 1 Construction of the 2d-min heap.
Algorithm 1 gives the stack-based construction of due to Fischer and Heun [11]. The construction of is analogous, with the comparison replaced by and assumed. We will use the following consequences of earlier work on the simultaneous construction of and .
► Proposition 3 ([13, 17, 26]). Consider the stacks maintained by the construction algorithms for and . For each step , the following holds:
-
If , pops occur in exactly one of the two stacks. Consequently, in the heap corresponding to the stack with no pop, node becomes a leftmost child and node becomes an internal node; in the other heap, node becomes a non-leftmost child and node becomes a leaf.
-
If , exactly one pop occurs in each of the two stacks. Consequently, in both heaps, node is appended as the right sibling of node and node becomes a leaf.
Colored 2d-min heaps. The colored 2d-min heap on , denoted by , is defined by coloring each node of as follows:
-
the root and leftmost children are colored blue, and
-
for each node with left sibling , node is colored blue if and red otherwise. Using the same coloring rule, the colored 2d-max heap on , denoted by , is defined analogously.
Fischer [9] showed that additionally supports NSV on . Jo and Satti [17] further showed that supports RMin queries. Thus, by maintaining both and , PSV, NSV, PLV, NLV, RMin, and RMax queries can be supported simultaneously. We will use the following consequences for the colored heaps.
► Proposition 4 ([26, 16]). For each , the following properties hold.
-
If , then, in either or , whenever node has a leaf as its left sibling, the color of node in that heap is forced to be red.
-
If , then the color of node is forced to be blue in both heaps.
Figure 1 gives examples of the heaps and colored heaps defined above.
3 Lower bounds
In this section, we establish ITLBs for supporting the full query family . By the following proposition, these bounds also imply the corresponding lower bounds for the query family .
► Proposition 5. The following properties hold:
-
The query families and can simulate each other.
-
The query families and can simulate each other.
Proof. We prove the first statement; the second is analogous.
We first simulate RMin using PSV and NSV. It is known that PSV can simulate , which returns the rightmost position among the minimum elements in a given interval [11]. Similarly, NSV can simulate , which returns the leftmost position among the minimum elements in a given interval.
Together, these two queries can simulate as follows. Let and . If , then there is a single minimum position in . Otherwise, starting from , repeatedly set until . This recovers all minimum positions in in increasing order, and therefore answers .
Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach
Conversely, we can also simulate PSV and NSV using RMin. First consider PSV(i). If , then PSV(i)=0. Otherwise, for , query RMin. The first position for which the answer is not is exactly PSV(i); if no such position exists, then PSV(i)=0. The simulation of NSV(i) is symmetric.
3.1 Main ideas
To compute desired ITLBs, we count the number of different inputs that give distinct answers to at least one query in {PSV, PLV, NSV, NLV}.
First, consider supporting only PSV. Two arrays have identical answers to all PSV queries if and only if they have the same 2d-min heap [11]. Every ordinal tree with nodes can arise as the 2d-min heap of a permutation of length , and hence of an array in any of the three classes. These heaps are therefore counted by the Catalan numbers (A000108), yielding an ITLB of bits.
By this equivalence and its max-heap analogue, two arrays have identical answers to all PSV and PLV queries if and only if they induce the same pair . Thus, it suffices to count the distinct realizable pairs in the array class under consideration. Here, not every pair of ordinal trees with nodes is realizable; i.e., some pairs cannot arise as for any array in the class under consideration. For permutations, the realizable pairs are counted by the Baxter numbers (A001181), yielding an ITLB of bits rather than bits [7, 13].
Two arrays have identical answers to all PSV and NSV queries if and only if they have the same colored 2d-min heap [9]. Together with the symmetric statement for PLV and NLV, this implies that two arrays have identical answers to all queries in {PSV, PLV, NSV, NLV} if and only if they induce the same pair . Hence, it suffices to count the realizable pairs of colored 2d-min and 2d-max heaps. As in the {PSV, PLV} case, counting these pairs requires accounting for dependencies between the heaps, now involving both their topologies and colors. However, the earlier arguments [7, 13] do not directly extend to this case; to our knowledge, no combinatorial class was previously known to capture the dependencies between the two colored heaps in the way that Baxter permutations do for the {PSV, PLV} case.
To capture these dependencies systematically, we use a generating tree. Let be a combinatorial class, where denotes the set of objects of size , and suppose that contains a unique object. A generating tree for is a rooted tree satisfying the following properties:
-
the root corresponds to the unique object in ;
-
the children of a node corresponding to an object in are determined by a prescribed rule that extends the object to objects in ; and
-
every object in appears exactly once in the tree.
The stack-based construction in Algorithm 1 scans the array from left to right, as does its max-heap counterpart. Thus, appending a new element to the array extends each colored heap by one node, with the extension determined by the stack operations and the color of the new node. We observe that Propositions 3 and 4 clearly capture the dependencies between the two heaps, specifying which stack operations can occur simultaneously and which colors are forced. Together, these constructions and properties allow us to characterize exactly how a realizable colored heap pair can be extended and thereby determine the children of each node in the generating tree. We first rederive the generating tree for Baxter permutations given in [4] and then extend it to colored heap pairs.
From the generating trees, we derive functional equations for the associated generating functions. For Baxter numbers, the obstinate kernel method yields an explicit formula [4], but this method does not appear to be applicable to our equations for and . Since asymptotic estimates suffice to derive the desired ITLBs, we follow the approach of Bouvel et al. [5], who relate the generating function for strong-Baxter permutations to that for quarter-plane excursions (defined in Section 3.5). Similarly, we relate our generating functions to those for quarter-plane excursions and apply known asymptotic results for these excursions. Taking logarithms of the resulting counts yields the desired ITLBs.
This generating-tree approach provides a unified framework for deriving ITLBs for every subfamily of on the three array classes , and . We apply this framework to the remaining query subfamilies in Appendix D.
The remainder of this section is organized as follows. Section 3.2 illustrates the generating-tree approach using a single 2d-min heap. Section 3.3 extends this approach to pairs of uncolored 2d-min and 2d-max heaps. Section 3.4 incorporates node colors to obtain generating trees for colored heap pairs. Finally, Section 3.5 derives the ITLBs for the full query family on all three array classes from the resulting generating trees.
3.2 Generating tree for 2d-min heaps
In this subsection, we derive a generating tree for 2d-min heaps induced by arrays in any of the three classes , and . Recall the construction algorithm for 2d-min heaps described in Algorithm 1. The algorithm scans the array from left to right. At each step, it pops indices from the stack until the top corresponds to a smaller value, attaches the new node as the rightmost child of that node, and pushes the new index onto the stack.
Consider the step of the construction algorithm that processes . Let be the indices in the stack immediately before this step, listed from bottom to top, where and . Since , the number of indices popped depends on where lies relative to these values. Every pop count can occur in each of the three array classes , and .
The nodes whose indices are stored in the stack form the rightmost spine of the current heap, namely the path from the root to node . We label each node of the generating tree by the stack size excluding index 0, equivalently the number of edges on this spine. The root of the generating tree corresponds to the unique heap induced by an array of length one, consisting of nodes 0 and 1, and has label 1. After pops and the push of index , the label becomes . Since each pop count determines a distinct extension of the heap, a node labeled has children, labeled , respectively. Figure 4 shows the first three levels of the generating tree for 2d-min heaps.
It is convenient to summarize a generating tree by its succession rule, which specifies the label of the root and, for each possible label, the labels of the children of a node with that label. Here, the generating tree for 2d-min heaps is described by
This succession rule coincides with the succession rule for the Catalan numbers (A000108).
3.3 Combining 2d-min and 2d-max heaps
In this subsection, we derive generating trees for pairs of 2d-min and 2d-max heaps induced by arrays in each of the three classes , and . Assume we run the construction algorithms
Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach
for the 2d-min and 2d-max heaps simultaneously, processing the same array element at each step. We label each node of the generating tree by a pair , where and are the stack sizes excluding index 0 for the min-heap and max-heap constructions, respectively.
Consider the step that processes . Let be the label immediately before this step. We first consider the case where . By the first part of Proposition 3, pops occur in exactly one of the two stacks. Suppose that no pop occurs in the stack of . Its label then increases from to . In the stack of , any pop count from 1 to can occur, so its label after pushing can be any value from 1 to . The other case is symmetric. Thus, for and , where consecutive elements are always distinct, we obtain the following succession rule:
This succession rule coincides with the succession rule for the Baxter numbers (A001181). Figure 5 shows the first three levels of the generating tree for uncolored heap pairs.
We next consider the case where . By the second part of Proposition 3, exactly one pop occurs in each stack. After pushing , both labels remain unchanged. Thus, for , we add a child node labeled to each node labeled in the generating tree defined by rule (2), obtaining
3.4 Combining colored 2d-min and 2d-max heaps
In this subsection, we derive generating trees for pairs of colored 2d-min and 2d-max heaps induced by arrays in each of the three classes , and . Counting these pairs yields the ITLB for supporting the full query family on each array class.
For , the colors are determined by the heap structure: the root and all leftmost children are blue, while all other nodes are red. Thus, the same succession rule (2) applies to colored heap pairs on permutations.
We now turn to and . Consider the step that processes , and let be the label immediately before this step. We first consider the case where . By Proposition 3, the new node is a leftmost child in one of the two heaps and is therefore blue in that heap. It remains to determine its color in the other heap. If its left sibling is a leaf, then its color is forced to be red by Proposition 4. This happens exactly when one index is popped from the corresponding stack, giving the label or . Otherwise, the left sibling is internal, and its value may be equal to or different from , so either color can occur. Thus, for , we obtain the following succession rule, where a superscript 2 denotes two children with the same label, one for each possible color:
Figure 2 shows the first three levels of this generating tree.
We next consider the case where . By Proposition 3, exactly one pop occurs in each stack, so both labels remain unchanged after pushing . Moreover, the

Figure 2 The first three levels of the generating tree for colored 2d-min and 2d-max heap pairs on arrays in . The two heaps are shown side by side in the first two levels and vertically in the third level, with the label below. Children with the same label can differ in their node colors.
new node is blue in both heaps by Proposition 4. Thus, for , we add a child node labeled to each node labeled in the generating tree defined by rule (4), obtaining
3.5 Lower bounds for supporting all queries
In this section, we give a proof of Theorem 1, restated below.
► Theorem 1 (Lower bounds). The ITLB for simultaneously supporting PSV, PLV, NSV, and NLV is
-
bits for arrays in ;
-
bits for arrays in .
The same ITLBs hold for simultaneously supporting RMin and RMax on the respective array classes.
As outlined above, we follow the approach of Bouvel et al. [5, Section 5.2]. We derive functional equations for the generating functions associated with the succession rules and relate these generating functions to those of quarter-plane walks. Known asymptotic results for such walks then yield the desired ITLBs.
Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach
Quarter-plane walks. Given a finite step multiset in , a quarter-plane walk with steps in is a lattice path that starts at , stays in , and takes each step from . The length of a walk is the number of its steps. We allow a step to have a multiplicity: two copies of the same step are counted as two different choices. Equivalently, multiple copies of the same step may be viewed as steps of different colors. For a step multiset , let
where is the number of walks of length that end at . A walk ending again at is called an excursion, so is the generating function for excursions. The characteristic polynomial of is defined by
We use the following consequence of [2, Theorem 3.11], specialized to the step multisets considered here. Its derivation is given in Appendix B.
► Lemma 6. Let be a finite step multiset in containing , , , , and . Then its characteristic polynomial has a unique critical point . Writing , the number of excursions of length satisfies
for some constant depending only on .
Proof of Theorem 1. We now derive the ITLBs for and by relating the number of realizable colored heap pairs to the number of quarter-plane excursions. This allows us to apply Lemma 6 to obtain the required asymptotic bounds.
Consider the step multiset , where denotes copies of the step . Let be obtained from by adding one further copy of .
► Proposition 7. For every , the numbers of distinct colored heap pairs () induced by arrays in and equal the numbers of excursions of length with steps in and , respectively.
Proof. We prove the statement for ; the case of is analogous. We begin by translating rule (4) into a generating function.
Let be the number of nodes at depth with label in the generating tree, and set
By rule (4), the root contributes , and the children of a node labeled contribute the following polynomial in and :
Multiplying this polynomial by , summing over , and adding the root contribution, we obtain the following equation after evaluating the geometric sums:
Define . Substituting and into (6) and rearranging terms, we obtain
(7)
where the characteristic polynomial of is
Write . Appending a step multiplies the generating function by , except that steps leaving the quarter plane must be excluded. The forbidden steps from the vertical boundary are the two copies each of and , and those from the horizontal boundary are the two copies each of and . Since no step decreases both coordinates, these two corrections do not overlap. Including the empty walk, we obtain
Define . Rewriting (8) in terms of yields (7), so . Setting , we obtain
This proves the statement for .
We now complete the proof of Theorem 1. Both and satisfy the assumptions of Lemma 6. Their characteristic polynomials have the same unique positive critical point , at which their values are and , respectively. Applying Lemma 6 and Proposition 7 and taking logarithms of the resulting counts gives the claimed ITLBs. The corresponding bounds for follow from Proposition 5.
4 Upper bounds
We first introduce some notations used throughout this section.
Word-access queries. Given a bit sequence and a position , the word-access query on , denoted by word_access(i), returns the length-w substring of starting at position , where is the word size.
Since we work in the word-RAM model with word size w, supporting this operation in constant time lets us access a represented bit sequence as if it were stored explicitly, with no asymptotic overhead in access time.
Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach
Rank and select queries. Given a bit array and a pattern , the query returns the number of occurrences of in , and returns the first position of the -th occurrence of in . We will use the following lemma.
► Lemma 8 ([24, 19]). Let be a bit array with ones. Then can be stored in bits while supporting word_access, and in constant time where .
Balanced parentheses. For an ordinal tree , the balanced-parentheses sequence of , denoted by , is obtained by a preorder traversal of : append an opening parenthesis when a node is first visited, and append a closing parenthesis when the traversal leaves the node. Thus, if has nodes, has length and can be stored explicitly using bits. With additional bits, supports various tree navigation queries on in constant time [20].
In this section, we mainly use balanced-parentheses sequences to represent 2d-min and 2d-max heaps. Consider for an array . We use the following observation of [16]: the number of closing parentheses immediately preceding the opening parenthesis of node in is exactly the number of stack pops performed just before node is appended in the construction algorithm.
Color sequences. Given an array , let be the color bit sequence of , storing the colors of the nodes in preorder, where 0 denotes blue and 1 denotes red. Define analogously for .
4.1 Main ideas and key lemmas
We first outline our approach and state the key lemmas used in our constructions. Their proofs are deferred to Appendix C.
Our approach builds on the combined representation of for arrays proposed by Jo and Kim [16]. They exploit the dependency between the two heaps described in Proposition 3 to reduce the space needed to represent their BP sequences. Specifically, their representation consists of two bit arrays and , defined in Section 4.2, of lengths and at most bits, respectively.
To represent the colored heap pair , they additionally store color information, recording only the colors that are not determined by the coloring rule and Proposition 4. Their encoding of , together with the color information, uses bits, yielding a total space usage of bits. With additional bits, their representation supports word_access on both and in constant time. Based on these word-access operations, they support the desired queries in time for any positive constant integer .
We improve both the space bound and the query time. To improve the space bound, we first show that is a contiguous substring of for some ordinal tree ; see Proposition 12. We further show that the stored color bits can be assigned to the non-leftmost children of , yielding a colored 2d-min heap realizable by an array in . By storing and applying Fischer's succinct representation [9] to this colored heap, we can achieve the desired space bound .
However, Fischer's representation uses tree covering [8] and does not directly provide constant-time word_access on . We need this operation to support word_access on and, in turn, on both and . We therefore develop an alternative succinct representation of colored 2d-min heaps that stores the tree topology and the color information separately. The following lemma summarizes this representation.
► Lemma 9. Given an array , there is a data structure using bits that supports word_access on both BP() and in constant time.
To improve the query time, we adapt the tree-covering representation of Hamada et al. [15], which uses balanced parentheses. Their representation allows the BP sequence of each micro tree to be obtained in constant time from the global BP sequence. Similarly, given constant-time word_access on BP() and , we obtain the BP and color sequences of each micro tree of cMin() in constant time. We use each pair of local sequences as an index into a precomputed table to support local operations in constant time. The following lemma formalizes this query support.
► Lemma 10. Given an array , suppose there is a data structure that supports word_access on both BP() and in constant time. Then, with additional bits, PSV, NSV, and RMin on can be supported in constant time.
Finally, we show that our representation supports constant-time word_access on BP(), BP(), , and . Applying Lemma 10 to both colored heaps then yields constant-time support for the desired queries. This proves Theorem 2 for arrays in . We then extend our construction to general arrays by applying the reduction of Jo and Kim [16].
As a further consequence, combining Lemmas 9 and 10 yields the following corollary.
► Corollary 11. Given an array , there is a succinct data structure using bits that supports PSV, NSV, and RMin on in constant time.
4.2 Encodings for supporting all queries
In this section, we give a proof of Theorem 2, restated below.
► Theorem 2 (Upper bounds). There exist data structures that simultaneously support PSV, PLV, NSV, NLV, RMin, and RMax in constant time using
-
bits for arrays in ;
-
bits for arrays in .
We first describe the representation of Jo and Kim [16] and then show how to improve it. For an array , their representation consists of the following components:
-
iff node is an internal node in cMin(). By Proposition 3, equivalently, iff node is an internal node in cMax().
-
: For , let be the heap in which node is a non-leftmost child of its parent. Let be the number of closing parentheses immediately preceding the opening parenthesis of node in BP(). Equivalently, is the number of stack pops performed just before node is appended in the construction of . We define . By Proposition 3, no pop occurs in the other construction, so encodes all close-runs for both Min() and Max().
-
(resp. ): the sequence of colors of nodes in cMin() (resp. cMax()) that are non-leftmost children and whose left sibling is not a leaf, listed in increasing order of their preorder numbers. By Propositions 3 and 4, all other colors are forced.
Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | ||
|---|---|---|---|---|---|---|---|---|---|---|
| 4 | 2 | 5 | 1 | 3 | 6 | 2 | 4 | |||
| BP(cMin(A)) | ( | ( | ( | ( | )) | ( | ( | )) | ( | )) |
| BP(cMax(A)) | ( | ( | ( | )) | ( | )) | ( | ( | )) | )) |
| 1 | 0 | 1 | 0 | 0 | 1 | 0 | ||||
| 0 | 10 | 10 | 0 | 10 | 10 | 0 | ||||
| 1 | 1 | |||||||||
| 1 |
Figure 3 An example of the sequences used in Section 4.2 for an array .
See Figure 3 for an example of the above sequences. We now describe our representation of . In what follows, interpret 0 as an open parenthesis and 1 as a close parenthesis in . For a bit array and a position , define as the number of zeros minus the number of ones in . We also define , with if is empty. We prove the following proposition.
Proposition 12. has nonnegative excess at every position
Proof. We construct by appending for , in parallel with the two stack constructions of and . Let denote the current prefix constructed so far, and let be the sum of the two stack sizes, excluding the bottom index 0 from each stack.
We first maintain the invariant after each string is appended. Initially, before is appended, is empty, and each stack contains only the indices 0 and 1; hence and . Now consider step . By Proposition 3, pops occur in exactly one of the two stacks. If indices are popped from that stack, then changes by , since one stack loses indices and both stacks push index . On the other hand, appending to changes by . Thus the invariant is preserved.
It remains to check that the excess does not become negative while each string is being appended. Before appending , the stack in which the pops occur contains the indices to be popped, and the other stack contains at least one non-bottom index. Thus , so by the invariant. During the first symbols of , the excess decreases by exactly , and therefore never becomes negative. The final symbol is an open parenthesis, so it only increases the excess. Hence has nonnegative excess at every position.
By the proposition, prepending an opening parenthesis to and appending closing parentheses yields for some ordinal tree with nodes. Let .
We now show that has non-leftmost child nodes. In the tree , the -th node in preorder is a non-leftmost child exactly when , since and hence the opening parenthesis corresponding to this node is preceded by a closing parenthesis exactly in this case. In terms of and , the condition means that, in the heap where node is inserted as a non-leftmost child, its left sibling is internal. By the definition of and , such indices are in one-to-one correspondence with the color bits stored in these two sequences. Hence has non-leftmost child nodes.
Let . We assign the bits of , in order, to the non-leftmost children of listed in preorder, and color the root and all leftmost children blue. The resulting colored tree is realizable as a colored 2d-min heap of an array in . We also store using bits to locate the boundary between and . Thus, by using Lemma 9, together with and all auxiliary structures, the total space usage is bits.
We now extend the construction to arrays in . Given an array , define a bit array by setting and, for , setting iff . Let be the subsequence of induced by the positions with , where is the number of zeros in . Our representation for consists of the above representation for , together with . See Figure 6 for an example of this representation.
By Lemma 8, can be stored using bits. For any constant and integers , we have [26]. Applying this inequality with , the total space usage is at most bits.
The query support follows from the following lemma (proof in Appendix C).
► Lemma 13. For arrays in either or , the corresponding representation constructed in this subsection can be augmented with bits to support constant-time word_access on , , , and .
Applying Lemma 10 to both colored heaps then yields constant-time support for the desired queries, completing the proof of Theorem 2.
5 Conclusions
We gave a unified approach to the encoding complexity of every subfamily of queries on permutations, arrays without consecutive equal elements, and general arrays. For all the possible subfamilies, we gave both lower bounds on encoding space and matching upper bounds while supporting constant time queries.
Although we settle the succinctness of all these query subfamilies, there are some interesting open problems to extend these results. First, our redundancy term bits) is not well-optimized. As in the encoding of range minimum queries [20], reducing the redundancies of the data structure would be interesting. Also, there exist redundancy-query time trade-offs for range minimum queries [18]. Extending the results for the queries considered in this paper would also be an interesting direction for future work.
AI Usage Disclosure The authors used OpenAI's ChatGPT (GPT-5 through GPT-5.5) as a research aid through extensive, iterative discussions over several months. GPT-5.6 Sol was additionally used to refine the language, clarity, and presentation of the manuscript.
The AI's main contribution concerned the lower-bound analysis. It first suggested applying the generating-tree framework of Bousquet-Mélou [4] to the incremental construction in Algorithm 1, leading to the generating trees and associated functional equations used in this paper. After extensive discussions about how to analyze these equations, the AI suggested relating them to quarter-plane walks based on the approaches of Bouvel et al. [5] and Bostan et al. [2]. This connection led to the asymptotic estimates needed to establish the lower bounds. All claims and proofs were independently verified by the authors.
Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach
16
References
1 Omer Berkman, Baruch Schieber, and Uzi Vishkin. Optimal doubly logarithmic parallel algorithms based on finding all nearest smaller values. J. Algorithms, 14(3):344–370, 1993. 2 Alin Bostan, Mireille Bousquet-Mélou, and Stephen Melczer. Counting walks with large steps in an orthant. Journal of the European Mathematical Society, 23(7):2221–2297, March 2021. doi:10.4171/jems/1053. 3 Alin Bostan, Kilian Raschel, and Bruno Salvy. Non-d-finite excursions in the quarter plane. Journal of Combinatorial Theory, Series A, 121:45–63, 2014. URL: https://www.sciencedirect.com/science/article/pii/S0097316513001441, doi:10.1016/j.jcta.2013.09.005. 4 Mireille Bousquet-Mélou. Four Classes of Pattern-Avoiding Permutations Under One Roof: Generating Trees with Two Labels. The Electronic Journal of Combinatorics, 9(2):R19, November 2003. doi:10.37236/1691. 5 Mathilde Bouvel, Veronica Guerrini, Andrew Rechnitzer, and Simone Rinaldi. Semi-baxter and strong-baxter: Two relatives of the Baxter sequence. SIAM Journal on Discrete Mathematics, 32(4):2795–2819, 2018. arXiv:https://doi.org/10.1137/17M1126734, doi:10.1137/17M1126734. 6 Denis Denisov and Vitali Wachtel. Random walks in cones. The Annals of Probability, 43(3):992–1044, 2015. URL: http://www.jstor.org/stable/24519214. 7 Serge Dulucq and Olivier Guibert. Stack words, standard tableaux and Baxter permutations. Discrete Mathematics, 157(1-3):91–106, 1996. 8 Arash Farzan and J. Ian Munro. A uniform approach towards succinct representation of trees. In Joachim Gudmundsson, editor, Algorithm Theory – SWAT 2008, pages 173–184, Berlin, Heidelberg, 2008. Springer Berlin Heidelberg. 9 Johannes Fischer. Combined data structure for previous- and next-smaller-values. Theoretical Computer Science, 412(22):2451–2456, May 2011. doi:10.1016/j.tcs.2011.01.036. 10 Johannes Fischer and Volker Heun. Finding Range Minima in the Middle: Approximations and Applications. Mathematics in Computer Science, 3(1):17–30, March 2010. doi:10.1007/s11786-009-0007-8. 11 Johannes Fischer and Volker Heun. Space-efficient preprocessing schemes for range minimum queries on static arrays. SIAM J. Comput., 40(2):465–492, April 2011. doi:10.1137/090779759. 12 Philippe Flajolet and Robert Sedgewick. Analytic Combinatorics. Cambridge University Press, 1 edition, January 2009. doi:10.1017/CB09780511801655. 13 Pawel Gawrychowski and Patrick K. Nicholson. Optimal Encodings for Range Top-k, Selection, and Min-Max. In Magnús M. Halldórsson, Kazuo Iwama, Naoki Kobayashi, and Bettina Speckmann, editors, Automata, Languages, and Programming, volume 9134, pages 593–604. Springer Berlin Heidelberg, Berlin, Heidelberg, 2015. doi:10.1007/978-3-662-47672-7_48. 14 Veronica Guerrini. On Enumeration Sequences Generalising Catalan and Baxter Numbers. Phd thesis, Università di Siena, 2017. URL: https://hdl.handle.net/20.500.14242/358781. 15 Kou Hamada, Sankardeep Chakraborty, Seungbum Jo, Takuto Koriyama, Kunihiko Sadakane, and Srinivasa Rao Satti. A Simple Representation of Tree Covering Utilizing Balanced Parentheses and Efficient Implementation of Average-Case Optimal RMQs. In Timothy Chan, Johannes Fischer, John Iacono, and Grzegorz Herman, editors, 32nd Annual European Symposium on Algorithms (ESA 2024), volume 308 of Leibniz International Proceedings in Informatics (LIPIcs), pages 64:1–64:18, Dagstuhl, Germany, 2024. Schloss Dagstuhl – Leibniz-Zentrum für Informatik. URL: https://drops.dagstuhl.de/entities/document/10.4230/LIPIcs.ESA.2024.64, doi:10.4230/LIPIcs.ESA.2024.64. 16 Seungbum Jo and Geunho Kim. Space-efficient Data Structure for Next/Previous Larger/Smaller Value Queries. Algorithmica, May 2025. arXiv:2209.00158, doi:10.1007/s00453-025-01325-9.
17 Seungbum Jo and Srinivasa Rao Satti. Simultaneous encodings for range and next/previous larger/smaller value queries. Theoretical Computer Science, 654:80–91, November 2016. doi:10.1016/j.tcs.2016.01.043.
18 Mingmou Liu and Huacheng Yu. Lower bound for succinct range minimum query. In Proceedings of the 52nd Annual ACM SIGACT Symposium on Theory of Computing, pages 1402–1415, 2020.
19 J.Ian Munro, Venkatesh Raman, and S.Srinivasa Rao. Space efficient suffix trees. Journal of Algorithms, 39(2):205–222, 2001. URL: https://www.sciencedirect.com/science/article/pii/S0196677400911519, doi:10.1006/jagm.2000.1151.
20 Gonzalo Navarro and Kunihiko Sadakane. Fully functional static and dynamic succinct trees. ACM Trans. Algorithms, 10(3), May 2014. doi:10.1145/2601073.
21 OEIS Foundation Inc. The on-line encyclopedia of integer sequences. https://oeis.org, 2026.
22 Enno Ohlebusch, Johannes Fischer, and Simon Gog. CST++. In SPIRE 2010. Proceedings, volume 6393 of Lecture Notes in Computer Science, pages 322–333, 2010.
23 Rajeev Raman. Encoding data structures. In WALCOM 2015. Proceedings, volume 8973 of Lecture Notes in Computer Science, pages 1–7, 2015.
24 Rajeev Raman, Venkatesh Raman, and Srinivasa Rao Satti. Succinct indexable dictionaries with applications to encoding k-ary trees, prefix sums and multisets. ACM Trans. Algorithms, 3(4):43–es, November 2007. doi:10.1145/1290672.1290680.
25 John Riordan. Enumeration of plane trees by branches and endpoints. Journal of Combinatorial Theory, Series A, 19(2):214–222, 1975. URL: https://www.sciencedirect.com/science/article/pii/S0097316575800100, doi:10.1016/S0097-3165(75)80010-0.
26 Dekel Tsur. The effective entropy of next/previous larger/smaller value queries. Information Processing Letters, 145:39–43, 2019. doi:10.1016/j.ipl.2019.01.011.
Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach
A Omitted Figures and Tables
| Data structure | Lower bound | Upper bound | |||||
|---|---|---|---|---|---|---|---|
| Queries | Array type | Space (in bits) | Sequence | Reference | Space (in bits) | Query time | Reference |
| PSV, RMin(·, ·, n) | Catalan A000108 | [11] | [11] | ||||
| PSV, NSV RMin | Catalan A000108 | [11] | [11, 9] | ||||
| A071356 | Theorem 19 | Theorem 24 | |||||
| Little Schröder A001003 | [9] | [9, 17], Corollary 11 | |||||
| PSV, PLV RMin(·, ·, n), RMax(·, ·, n) | Baxter A001181 | [7, 13] | [13, 17] | ||||
| A216947 | Theorem 18 | Theorem 23 | |||||
| PSV, NLV RMin(·, ·, n), RMax(·, ·, 1) | Baxter A001181 | Theorem 21 | Theorem 25 | ||||
| PSV, PLV, NSV, RMin, RMax(·, ·, n) | Baxter A001181 | [7, 13] | [9, 13, 17] | ||||
| Not indexed by the OEIS | Theorem 22 | Theorem 26 | |||||
| Not indexed by the OEIS | Theorem 22 | Theorem 26 | |||||
| PSV, PLV, NSV, NLV, RMin, RMax | Baxter A001181 | [7, 13] | [9, 13, 17] | ||||
| Not indexed by the OEIS | Theorem 1 | Theorem 2 | |||||
| Not indexed by the OEIS | Theorem 1 | Theorem 2 | |||||
Table 2 Summary of lower and upper bounds. In each entry, the previous/next smaller/larger value queries and the range queries define two query families with the same ITLB. The upper bounds are for supporting all listed queries simultaneously.

Figure 4 The first three levels of the generating tree for 2d-min heaps. Each heap is shown together with its label.

Figure 5 The first three levels of the generating tree for uncolored 2d-min and 2d-max heap pairs on and . The two heaps are shown side by side in the first two levels and vertically in the third level, with the label below.

cMin(A)
cMax(A)
| i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| A[i] | 4 | 2 | 5 | 5 | 1 | 3 | 3 | 6 | 2 | 4 | ||
| A' | 4 | 2 | 5 | 1 | 3 | 6 | 2 | 4 | ||||
| BP(cMin(A')) | ( | ( | ( | ( | ) | ( | ( | ( | ) | ( | )) | |
| BP(cMax(A')) | ( | ( | ( | ) | ) | ( | ) | ) | ) | ( | ) | )) |
| U | 1 | 0 | 1 | 0 | 0 | 1 | 0 | |||||
| S = s_2 ... s_n | 0 | 10 | 10 | 0 | 10 | 10 | 0 | |||||
| c_min | 1 | 1 | 1 | |||||||||
| c_max | 1 | 1 | ||||||||||
| C | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
Figure 6 An example of the sequences used in Section 4.2 for an array .
Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach
B Omitted Proofs in Section 3
B.1 Proof of Lemma 6
▶ Lemma 6. Let be a finite step multiset in containing , , , , and . Then its characteristic polynomial has a unique critical point . Writing , the number of excursions of length satisfies
for some constant c > 0 depending only on S.
The asymptotic behavior of quarter-plane walks is known from the work of Denisov et al. [6] and Bostan et al. [3]. Bouvel et al. [5] used these results to derive the asymptotic behavior of strong-Baxter permutations. For our purposes, we use the formulation of these asymptotic results for step multisets given by Bostan et al. [2]. To state the lemma, we first introduce two necessary notations.
Given a step multiset , let be the lattice spanned by . We say that is strongly aperiodic if, for every lattice point , the translated step multiset also generates .
Note that every step multiset in Table 3 is strongly aperiodic, since it contains , , and . Indeed, since contains and , it generates . For every , the translated step set contains , , and . Hence it generates their differences
and therefore generates . Thus, is strongly aperiodic.
Given a step multiset , we say that a point is reachable from infinity for if there exists a quarter-plane walk with steps in that starts at a point with and , and ends at .
Note that, for every step multiset in Table 3, every is reachable from infinity, since contains and .
▶ Lemma 14 (Theorem 3.11 in [2]). Let be a finite step multiset in , with characteristic polynomial . Suppose that is not contained in a half-plane, contains a step in , and is strongly aperiodic. Then has a unique critical point .
Moreover, let denote the number of quarter-plane walks with steps in of length that end at . If is reachable from infinity for , then, setting , we have
for some constant c > 0.
For any step multiset satisfying the assumptions of Lemma 6, the presence of , , , and ensures that is not contained in a half-plane. The presence of , , and guarantees strong aperiodicity, and is a step in . Moreover, is reachable from infinity using the steps and . Thus, applying the preceding lemma with and observing that proves Lemma 6.
C Omitted Proofs in Section 4
C.1 Proof of Lemma 9
▶ Lemma 9. Given an array , there is a data structure using bits that supports word_access on both and in constant time.
Let denote the number of ordinal trees with nodes and leaves. These numbers are the Narayana numbers [25] and are given by
▶ Lemma 15. Let be an ordinal tree with nodes and leaves. Then can be represented using bits while supporting word_access on in constant time.
Proof. Remove the first and last parentheses of , corresponding to the root. Let the remaining balanced-parentheses sequence be P, of length 2n. Then, we can write
where .
We first store a bit array where iff the i-th opening parenthesis of P is followed by a closing parenthesis. Equivalently, iff the -th node in preorder is a leaf. Hence, L has k ones.
We also store a bit array where iff the i-th closing parenthesis of P is followed by an opening parenthesis. In this case, the node corresponding to this opening parenthesis is a non-leftmost child. Since an ordinal tree with k leaves has k-1 non-leftmost child nodes, R has k-1 ones.
The two bit arrays are stored using
bits by Lemma 8.
It remains to support word_access on BP(T). Partition P into superblocks of length and microblocks of length . For each superblock, store the numbers of opening and closing parentheses preceding it. For each microblock, store the numbers of opening and closing parentheses preceding it within its superblock, together with the first parenthesis of the microblock. The total space is
To answer word_access on BP(T), it suffices to reconstruct the constant number of microblocks intersecting the requested word. To decode a microblock, suppose that o opening and c closing parentheses precede it. We decode the next b bits of L starting from position and the next b bits of R starting from position . This is supported in constant time since representations of L and R allow word_access on them by Lemma 8. We use a precomputed table indexed by these two length-b strings and the first parenthesis of the microblock; the table returns the whole microblock of P. This table is well defined
Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach
because the next parenthesis is determined during the scan: after an opening parenthesis, the following parenthesis is closing exactly when the corresponding bit of is 1, and after a closing parenthesis, the following parenthesis is opening exactly when the corresponding bit of is 1. Since the table has size , any consecutive bits of , and hence of , can be accessed in constant time.
Suppose has leaves. Then it has non-leftmost child nodes, and it is enough to store the colors of those nodes. Let be the array of their color bits, listed in preorder. We store explicitly and encode the underlying tree using Lemma 15. Thus, the total space is
bits. Let . Since
where is the binary entropy function, we have
The function is maximized at , and its maximum value is . Therefore the space is at most bits.
By Lemma 15, word_access on can be supported in constant time. Thus, by Lemma 10, the required query support follows once word_access on is supported in constant time.
Since the nodes of are labeled in preorder, with node 0 as the root, the bit array stored in Lemma 15 satisfies iff node is a leaf. Hence, for , node is a non-leftmost child iff .
The first two bits of are always 0, since they correspond to nodes 0 and 1, which are always blue. We therefore partition the suffix of after these two bits into blocks of length . Consider a block whose first entry stores the color of node . We decode the next bits of starting from position . Let , which is the number of non-leftmost child nodes before node . We then read the next bits of starting from position . A precomputed table indexed by these two length- strings returns the corresponding block of : positions whose corresponding bit of is 0 are filled with 0, and the remaining positions are filled, in order, with the bits read from . The table has size bits, so each block of can be decoded in constant time. Consequently, word_access on is supported in constant time.
C.2 Proof of Lemma 10
▶ Lemma 10. Given an array , suppose there is a data structure that supports word_access on both and in constant time. Then, with additional bits, PSV, NSV, and RMin on can be supported in constant time.
First, define the following color-related operations on :
color(i): return the color of node i.
PRS(i): return the rightmost red sibling to the left of node i.
NRS(i): return the leftmost red sibling to the right of node i.
By [16, Lemma 5], NSV and RMin on A can be answered in constant time provided that the standard tree navigation operations (previous sibling, next sibling, subtree size, degree, level ancestor, child rank, child select, and parent) and the three color-related operations above are supported in constant time.
Moreover, PSV corresponds to the parent operation in Min(A). The standard tree navigation operations are supported on BP(Min(A)) in constant time using additional bits [20], and color is supported by in constant time. Thus, it remains only to support PRS and NRS.
To support these queries, we use the modified tree cover introduced by Hamada et al. [15]. Let be an ordinal tree with nodes, and let be a block-size parameter. The modified tree cover is defined on a tree obtained from by inserting dummy nodes. Each dummy node is inserted as a child of a node of , and a consecutive subsequence of the children of in is moved under , preserving their order. The nodes of are partitioned into disjoint micro trees, where each micro tree is a connected subgraph of .
Contracting each micro tree into a single node gives the top-tier tree . For two micro trees and , we say that is a child micro tree of if is a child of in .
The modified tree cover satisfies the following properties:
-
The number of micro trees is .
-
Each micro tree is of one of the following two types:
a. A singleton micro tree, which consists of a single non-dummy node and may have an arbitrary number of child micro trees.
b. A non-singleton micro tree, which consists of fewer than 2B nodes and has at most one child micro tree. Its root d is a dummy node, and all remaining nodes are non-dummy nodes.
They showed that, for each micro tree , can be obtained from at most two substrings of by concatenating them and, if is non-singleton, enclosing the resulting string with the parentheses corresponding to the dummy root.
The same observation applies to the color sequence. Let denote the subsequence of consisting of the colors of the non-dummy nodes of in preorder. If the non-dummy nodes of correspond to one substring of , then their preorder numbers form a consecutive interval, and hence is obtained from the corresponding substring of . If they correspond to two substrings of , then their preorder numbers form at most two consecutive intervals, and is obtained by concatenating the two corresponding substrings of .
We set . Using the approach of Hamada et al. [15], can be obtained in constant time from word_access on for any micro tree . Similarly, can be obtained in constant time from word_access on . Thus, the pair consisting of and can be used as an index to a universal precomputed table with at most entries.
With this table, the remaining support for PRS and NRS is obtained by adapting the tree-cover-based support for NRS in Fischer's representation of colored 2d-min heaps [9]. The main difference is that the top-tier tree of the modified tree cover replaces the mini-tree layer used in Fischer's construction.
See Table 1 of [20] for their definitions.
Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach
C.3 Proof of Lemma 13
► Lemma 13. For arrays in either or , the corresponding representation constructed in this subsection can be augmented with bits to support constant-time word_access on BP(), BP(), , and .
We use the following formulations of the reconstruction results of Jo and Kim [16]. Their Theorem 2 shows how to support word_access on both BP() and BP() using , an alternative representation of , and bits of auxiliary structures. Specifically, their formulation uses two sequences, denoted and in their paper, to represent in compressed form. The same reconstruction framework applies when constant-time word_access on is available directly, yielding the first lemma below. The second lemma formulates their Theorem 3 in terms of the bit array and the induced subsequence .
► Lemma 16 ([16, Theorem 2]). Given an array , suppose there is a data structure that supports word_access on both and in constant time. Then, with bits of auxiliary structures, word_access on both BP() and BP() can be supported in constant time.
► Lemma 17 ([16, Theorem 3]). Given an array , consider the bit array and the induced subsequence . Suppose there is a data structure that supports word_access on both BP() and BP() in constant time. Then, together with and additional bits, word_access on both BP() and BP() can be supported in constant time.
Query support for . By Lemmas 10 and 16, it remains only to support word_access on both and .
In the present representation, is exactly the leaf bit array for , while the complement of is the leaf bit array for by Proposition 3. Thus, for , node is a non-leftmost child in iff , and it is a non-leftmost child in iff .
Recall that we regard as part of the balanced-parentheses sequence BP() of an ordinal tree , and store using Lemma 15. The representation of Lemma 15 uses the bit array . Since iff the -st vertex of in preorder is a leaf, the discussion above implies that, for , if node is a non-leftmost child in , then its left sibling is internal iff .
Hence, for , the color of node is determined as follows. If or , then it is always blue. If , then it is blue when and red when . For , the color is stored in precisely when and . If and , then the color is forced to be red; if , then node is a leftmost child in , so its color is blue. The colors in are determined symmetrically.
We handle the colors of nodes 0, 1, and 2 separately and partition the remaining suffix of into blocks of length . We group these blocks into superblocks of length . For each superblock, we store the number of entries of corresponding to nodes preceding it, and for each block, we store the corresponding count within its superblock. These counts use bits in total and determine the starting position in for each block in constant time.
Consider a block whose first entry stores the color of node . We read the next bits of starting from position , the next bits of starting from position , and the next bits of starting from the position determined by the stored counts. A precomputed table indexed by these three strings reconstructs the block according to the color rules above. The precomputed table has size bits. Since any requested word intersects only a constant number of blocks, both color bit sequences support word_access in constant time using additional bits.
Query support for . For query support, by Lemma 17, it suffices to support word_access on both and in constant time. Consider the color bit sequences and for . For each , let . If , then node of corresponds to node of , and its colors in and are given by the corresponding entries of and , respectively. If , then by Proposition 4, node is forced to be blue in both colored heaps.
The first bit of is always 0, since it corresponds to the root. We therefore partition the suffix of after this bit into blocks of length . Consider a block whose first entry stores the color of node . Let . We decode the next bits of starting from position , and the next bits of starting from the entry for node . A precomputed table indexed by these two length- strings returns the corresponding block of : positions whose corresponding bit of is 1 are filled with 0, and the remaining positions are filled, in order, with the bits read from . The table has size bits, so each block of can be decoded in constant time. The same construction supports word_access on . Consequently, by Lemma 10, all desired queries are supported in constant time.
D Lower bounds for query subfamilies
| Generating tree | Step multiset | Positive critical point | Growth constant | Data structure | ||
|---|---|---|---|---|---|---|
| Root | Rule | Queries | Array type | |||
| (1) | - | - | 4 | PSV | ||
| - | - | PSV, NSV | ||||
| - | - | PSV, NSV | ||||
| (1,1) | 8 | PSV, PLV | ||||
| PSV, NLV | ||||||
| PSV, NSV, PLV | ||||||
| PSV, NSV, PLV, NLV | ||||||
| (1,1) | 9 | PSV, PLV | ||||
| PSV, NSV, PLV | ||||||
| PSV, NSV, PLV | ||||||
| PSV, NSV, PLV, NLV | ||||||
| PSV, NSV, PLV, NLV | ||||||
Table 3 Generating trees and their corresponding quarter-plane step multisets, where applicable.
In this section, we derive ITLBs for each nonempty proper subfamily of on the three array classes , and , using the framework developed in Section 3. For subfamilies containing three queries, symmetry allows us to restrict our analysis to . Table 3 summarizes the succession rules, the corresponding step multisets
Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach
and growth constants where applicable, and the query families and array classes to which they apply.
D.1 The {PSV, PLV} case
We follow the framework of Section 3, counting realizable pairs of uncolored 2d-min and 2d-max heaps. Rule (2) is known to generate the Baxter numbers, yielding an ITLB of bits for and , matching the lower bound obtained by Gawrychowski and Nicholson [13]. Thus, it remains to analyze the case of . Applying the same framework as in Section 3.5 to rule (3), we find that the number of realizable heap pairs of size equals the number of quarter-plane excursions of length with step multiset
Its characteristic polynomial is
The unique positive critical point is , at which . Applying Lemma 6 and taking logarithms of the resulting counts yields the following lower bound.
▶ Theorem 18. For arrays in , the ITLB for the simultaneous support of PSV and PLV is bits.
D.2 The {PSV, NSV} case
For , the colors of a colored 2d-min heap are completely determined by its topology, so counting colored heaps is equivalent to counting uncolored heaps, yielding an ITLB of bits. For , Fischer [9] already established an ITLB of bits. Thus, it remains to analyze the case of .
We count distinct colored 2d-min heaps induced by arrays . We use the same labeling from Section 3.2. As in Section 3.2, the underlying 2d-min heap is determined by the number of indices popped from the stack at each insertion. Thus, as in rule (1), each node labeled has one child with each label in before accounting for colors. It remains to account for the color of the newly inserted node.
Consider the step of the construction algorithm that processes . If no pop occurs, then the new node is appended as the leftmost child of node m. Hence its color is forced to be blue. This case gives a child node labeled .
If exactly one pop occurs, node becomes a left leaf sibling of node . Since , the color of the new node is forced to be red, as noted in Proposition 4. This case gives a child node labeled .
If more than one pop occurs, then the new node has a left non-leaf sibling. In this case, by choosing to be equal to or different from the value of that sibling, either blue or red can be realized for the new node. Thus, each node labeled k has two children with each label in , one for each possible color, and only one child with each of the labels k and .
Thus, the generating tree for colored 2d-min heaps on arrays with no consecutive equal elements is described by
We next estimate the number of objects generated by rule (9), which yields the desired lower bound.
Theorem 19. Let be the number of objects generated by rule (9). Then,
Hence, the ITLB for the simultaneous support of PSV and NSV on arrays with no consecutive equal elements is bits.
Proof. Let be the number of nodes of size n with label k, and set
The root contributes xy. For a node with label k, the rule (9) contributes
Therefore,
Multiplying by 1 - y and rearranging gives
Here, our goal is to determine the generating function
We therefore choose so that the left-hand side of (10) becomes zero and leaves an equation for . Hence must satisfy
Solving this equation for Y gives the two roots
Since Y will be substituted into the generating function, we need Y to be a power series in x. Thus we choose
Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach
Substituting into (10) gives
and hence
This generating function, and the corresponding sequence , are listed in [21] as A071356. The desired asymptotic follows by standard singularity analysis on the generating function [12, Chapter VI]; it is also noted in [21, A071356].
D.3 The {PSV, NLV} case
Let denote the reversal of , defined by for . For an index in , we write for the corresponding index in . Under this correspondence, PSV and PLV on correspond to NSV and NLV on , respectively. Similarly, and on correspond to and on , respectively. We now relate the constructions of and .
► Proposition 20. For every , no pop occurs at step in the construction of if and only if no pop occurs at step in the construction of . Consequently, node is a leftmost child in if and only if node is a leftmost child in .
Proof. Immediately before step in the construction of , the top of the stack is . Therefore, no pop occurs at this step if and only if . In the construction of , immediately before step , the top of the stack is . Thus, no pop occurs at this step if and only if . By the definition of , this condition is equivalent to . Hence, the two conditions are equivalent.
Here, we first restrict the class of arrays to permutations. Thus, it suffices to count the realizable pairs induced by arrays . We use a pair of labels . The first label is defined as in the PSV case for the construction of . The second label is the number of children of the root in .
Consider the step of the construction algorithm that processes a new element , viewed as appending as the -st element of . Since is a permutation, represents a new rank inserted among the existing values; in , this corresponds to prepending the same new element.
Let be the subtrees rooted at the children of the root of , from left to right, and let be the values at their roots. After is prepended to , it is the first element processed by the max-heap construction and has no preceding larger element, so its node is attached as the leftmost child of the root. It has as its child subtrees for some . Moreover, every value of is realizable: for , choose ; for , choose so that , where . If , the new root degree becomes . If , then the new root degree becomes . Thus, any value in can be the second label.
By Proposition 20, the case occurs if and only if no pop occurs in the construction of while processing . Moreover, the choices of required by the two labels are compatible. The case is exactly the range , where a pop occurs in and the first label can be chosen from as in the PSV case. The cases all lie in the range , where no pop occurs and the first label becomes . Therefore we obtain the same succession rule as rule (2), which is the Baxter succession rule. Thus, we obtain the following lower bound.
For arrays in and , allowing repeated values introduces no additional children beyond those prescribed by the Baxter succession rule. It can only make the sequence nondecreasing rather than strictly increasing, and hence may remove some choices of for a particular array. Nevertheless, the second label of each child is still one of the values described above, with the first label determined as before. Since permutations already realize all pairs counted by the Baxter succession rule, the number of realizable pairs is the Baxter number in all three array classes. Thus, we obtain the following lower bound.
► Theorem 21. For permutations, arrays with no consecutive equal elements and general arrays, the ITLB for the simultaneous support of PSV and NLV is bits.
D.4 The {PSV, PLV, NSV} case
► Theorem 22. For arrays in , the ITLB for the simultaneous support of PSV, PLV, and NSV is bits. For arrays in , it is bits.
Proof. For arrays in , it is enough to count realizable pairs consisting of a colored 2d-min heap and an uncolored 2d-max heap. The same labeling as in Section 3.4 gives the following succession rule:
Root: (1,1)
This rule gives the step multiset
whose characteristic polynomial is
A direct calculation shows that the positive critical point is . At this point,
For arrays in , adding one copy of the step changes the characteristic polynomial from to . The positive critical point remains , and at this point the new value is .
E Upper bounds for query subfamilies
E.1 The {PSV, PLV} case
For and , storing and and applying Lemma 16 supports the desired queries in constant time using bits, matching the upper bounds of Gawrychowski and Nicholson [13].
Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach
For , we use the same bit array and induced subsequence as in Section 4.2. We store the above representation for , together with encoded using Lemma 8. The total space is
bits. By Lemma 17, this supports word_access on both and in constant time. The desired queries are then supported through the corresponding tree navigation operations, yielding the following theorem.
► Theorem 23. Given an array , there is a data structure using bits that supports PSV, PLV, , and on in constant time.
E.2 The {PSV, NSV} case
For , the colors are determined by the heap topology, so an uncolored 2d-min heap representation suffices to support the desired queries in constant time using bits [11, 9]. For , Corollary 11 supports the desired queries in constant time using bits, matching Fischer's upper bound [9]. Thus, we describe only the case of .
For arrays in , Proposition 4 implies that the colors of non-leftmost child nodes whose left sibling is a leaf are forced. We exploit this property to improve the space bound.
► Theorem 24. Given an array , there is a data structure using bits that supports PSV, NSV, and RMin on , as well as word_access on both and , in constant time.
Proof. Similar to the proof of Lemma 9 (Appendix C.1), we encode and the colors separately. Let be the number of leaves of , and let be the number of non-leftmost child nodes whose left sibling is an internal node. We describe below an encoding of using
bits that supports word_access on in constant time.
By Proposition 4, the only colors that are not forced are those of the non-leftmost child nodes whose left sibling is internal. Let be the array of their color bits, listed in preorder. We store explicitly, and hence the total space is
bits.
Let and . Then, the total space is at most
bits. The expression in parentheses is maximized at and and its maximum value is . Therefore, the total space is at most
bits.
By Lemma 10, the query support follows once we support word_access on both BP(Min(A)) and in constant time. We first describe a representation of BP(Min(A)) that supports word_access in constant time; then we show how to support word_access on .
Supporting word_access on BP(Min(A)). Following the proof of Lemma 15, remove the first and last parentheses of BP(Min(A)), corresponding to the root, and let be the remaining sequence. Use the same bit array , where iff the -th opening parenthesis of is followed by a closing parenthesis. Then has ones and can be stored in bits.
We encode the boundaries of the maximal runs of closing parentheses in using two bit arrays and as follows. Excluding the final closing run of , the number of maximal closing runs of length at least two is exactly .
We construct a bit array over the leaves, except for the last one, in postorder: iff, for the corresponding node , the closing parenthesis of is followed by a closing parenthesis. Since the closing parenthesis of a leaf is preceded by an opening parenthesis, marks the starting points of the closing runs of length at least two. Hence, has ones and can be stored in bits.
In addition, we construct a bit array over the non-root internal nodes in postorder: iff, for the corresponding node , the closing parenthesis of is followed by an opening parenthesis. Since the closing parenthesis of an internal node is preceded by a closing parenthesis, marks the ending points of the maximal closing runs of length at least two, excluding the final run. Hence, has ones and can be stored in bits.
Thus the total space is (11). It remains to support word_access on BP(Min(A)). We use the same two-level blocking scheme for as in the proof of Lemma 15, and decode each microblock using a precomputed table. For each superblock, we store the following three quantities: (a) number of opening parentheses, (b) number of closing parentheses of leaves preceding it, and (c) number of closing parentheses of internal nodes preceding it. For each microblock, we store the same three quantities preceding it within its superblock, together with its first parenthesis and one additional bit indicating whether the preceding parenthesis is opening.
To decode a microblock, suppose that opening parentheses, closing parentheses of leaves, and closing parentheses of internal nodes precede it. We decode the next bits of starting from position , the next bits of starting from position , and the next bits of starting from position .
Given these three length- strings, the first parenthesis of the microblock, and the additional bit, a precomputed table reconstructs the whole microblock of . After an opening parenthesis, the next parenthesis is closing exactly when the corresponding bit of is 1. After a closing parenthesis preceded by an opening parenthesis, the next parenthesis is closing exactly when the corresponding bit of is 1. After a closing parenthesis preceded by another closing parenthesis, the next parenthesis is opening exactly when the corresponding bit of is 1. If the microblock starts with a closing parenthesis, the additional bit determines which of the latter two rules applies. Since the table has size , any consecutive bits of , and hence of BP(Min(A)), can be accessed in constant time.
Supporting word_access on . Since the nodes of are labeled in preorder, with node 0 as the root, the leaf bit array satisfies iff node is a leaf. Hence, for , node is a non-leftmost child iff . Among these nodes,
Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach

| i | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 4 | 2 | 5 | 5 | 1 | 3 | 3 | 6 | 2 | 4 | |||
| 4 | 2 | 6 | 3 | 3 | 1 | 5 | 5 | 2 | 4 | |||
| U | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 1 | 0 | ||
| BP(Min(A)) | (( )(( )(( ) | ) | ) | ) | ( ) | ( ) | ) | ( ) | ) | ) | ) | ) |
| 1 | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | ||
| BP(Max( )) | (( ) | ) | ( ) | ( ) | ) | ( ) | ( ) | ( ) | ) | ) | ) | ) |
| 0 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 0 |
Figure 7 An example of the sequences used in Appendix E.3 for an array . For readability, each node in is labeled with its corresponding index in rather than with the index in . For the rows and , the entry in column contains the -th closing parenthesis followed by the subsequent run of opening parentheses. Thus and are 1 exactly when the corresponding entry contains an opening parenthesis after its first symbol.
the ones whose left sibling is internal are determined by .
We use the same block decoding scheme as in the proof of Lemma 13 (Appendix C.3), with , , and determining the colors here. The first two bits of are always 0, since they correspond to nodes 0 and 1, which are always blue. We partition the remaining suffix into blocks of length . Consider a block whose first entry stores the color of node . Let , which is the number of non-leftmost child nodes before node , and let , which is the number of such nodes whose left sibling is internal. We decode the next bits of starting from position , the next bits of starting from position , and the next bits of starting from position . A precomputed table indexed by these three length- strings returns the corresponding block of : positions whose corresponding bit of is 0 are filled with 0, positions whose corresponding bit of is 1 and whose corresponding bit of is 0 are filled with 1, and the remaining positions are filled, in order, with the bits read from . The table has size bits, so each block of can be decoded in constant time. Consequently, word_access on is supported in constant time.
E.3 The {PSV, NLV} case
In this subsection, we give a representation for the pair and . Recall that is the index in corresponding to index in . This pair supports PSV and from , and also supports NLV and from through this correspondence. The remaining symmetric case uses the pair and , and is handled in the same way.
▶ Theorem 25. Given an array (or ), there is a data structure using bits that supports PSV, NLV, , and on in constant time.
Proof. The desired queries are obtained from standard navigation on and . The queries PSV and NLV are parent queries in and , respectively, under the correspondence . Moreover, and are answered using a constant number of standard navigation operations on and , respectively [11]; these operations are supported from balanced-parentheses sequences with additional bits [20]. Thus, it remains to show that word_access on both and can be supported in constant time.
Store a bit array marking the non-leftmost child nodes of . By Proposition 20, for every , node is a non-leftmost child in iff . Thus, the non-leftmost child nodes for are also determined by . Also store the and , which are arrays defined in the proof of Lemma 15 for and , respectively. These three arrays use bits. See Figure 7 for an example.
In the proof of Lemma 15, word access to the balanced-parentheses sequence is supported using the leaf array together with the array . Since, for every , node is a non-leftmost child iff node is a leaf, the array also determines the leaf arrays for both and . Together with the explicitly stored arrays and , the decoding method of Lemma 15 therefore supports word_access on both and in constant time with additional bits.
E.4 The {PSV, PLV, NSV} case
In this subsection, we show how to combine a representation of one colored heap with an uncolored representation of the other heap. We present the construction for and , which gives the query family ; the remaining cases follow by exchanging min and max, and by applying the construction to the reversed array . We first handle arrays in , and then extend the construction to arrays in .
▶ Theorem 26. Given an array , there is a data structure using bits that supports PSV, PLV, NSV, RMin, and RMax( , , n) on in constant time. For arrays , the same queries can be supported in constant time using bits.
Proof. Let . Our data structure stores the representation of from Theorem 24. Hence, the queries PSV, NSV, and RMin are supported in constant time. It remains to support the information needed from . For this, we store the bit array for , defined as in Lemma 15, using bits. The representation of already contains the bit array , where iff node is a leaf in . By Proposition 3, also determines the leaf information for : for , node is a leaf iff , and node is always a leaf. Thus, applying the decoding method from the proof of Lemma 15 using this information and the stored array supports word_access on in constant time.
The query PLV is given by the parent operation in , and can be answered using a constant number of standard navigation operations on [11]. Since these operations are supported from with additional bits [20], the required queries involving are supported in constant time. The space usage for is
bits.
For an array , we use the same reduction as in Section 4.2. Define the bit array and the induced subsequence as in that subsection. We store the above representation for , together with encoded using Lemma 8. By the same space calculation as in Section 4.2, this uses
Optimal Encodings for Next/Previous Larger/Smaller Value Queries: A Unified Approach
bits. By Lemma 17, this supports word_access on both BP(Min(A)) and BP(Max(A)) in constant time. Moreover, we support word_access on in constant time using the block decoding method for general arrays described in the proof of Lemma 13 (Appendix C.3). Consequently, Lemma 10 and the standard tree navigation operations support all desired queries in constant time.