-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructures.tex
3386 lines (2810 loc) · 164 KB
/
structures.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\chapter{Advanced Data Structures}
In the previous chapter, we showed how three different bit string encodings of ordinal trees can be turned into data structures which support wide varieties of operations, provided that they are equipped with the \rank{} and \select{} (and later \match{} and \enclose{}) indices.
Here we present data structures which are fundamentally different in either the primitive operations they use, or that they require more advanced encoding instead of being based on a single bit string.
\begin{description}
\item[Fully-Functional (FF)]
A data structure built on top of the BP representation which replaces the set of traditional indices by new more general index.
This results in a structure which supports not only all operations which BP did -- the old primitive operations are special cases of the new ones, but also allows to implement those operations which either required a specialized index, or they simply were not possible.
\item[Tree Covering (TC)]
Tree covering is a data structure based on a recursive decomposition of the tree into mini-trees and micro-trees which are then encoded.
Each level of the decomposition is stored and processed in a different way.
Since it is tree structure rather than bit string oriented, it is easier to augment it with small pieces of information which are required by various operations.
This is a big difference from
\begin{enuminline}
\item discovering, more than inventing, the inner rules of the representations, which we did with ranking and selecting;
\item or developing general indices which cover the bit string in various blocks on multiple levels.
\end{enuminline}
\item[Universal Succinct Representation (USR)]
An attempt to develop a universal data structure which diminishes the differences between several representations.
It provides a view to the BP and DFUDS bit string representation as well as decomposition into micro-trees.
Its main advantage is that it automatically benefits from any indices which are proposed for either BP, DFUDS, or TC representations.
\end{description}
%\todo{Mention transformation into a binary tree}
\section{Fully Functional Representation}
The BP representation which we described in the previous chapter was based on using \rank{} and \select{}, and \match{} and \enclose{} operations as the main tool.
There were several operations which required an additional index (\childAny{} and \levelAncestor{}), and several operations which were not supported at all (\levelAny{}).
All the operations which we have just listed have one thing in common: they can all be expressed using more general operations.
We did not show the indices for \levelAncestor{} in the BP and DFUDS representations for a reason: they were unnecessarily complicated, whereas here in the Fully Functional data structure, it is going to be one of the easiest operations.
The data structure was developed by \cite{sadakane2010fully}.
The name Fully Functional refers to the set of operations, that the structure offers full range of functionality.
We define a new set of primitive operations for which we then design a single index, incorporating even the range operation.
The index will be general -- any of the representations (LOUDS, BP, DFUDS) can use it, however only BP will benefit from it.
\subsection{New Operations}
We follow on the section \ref{ss:rmq-def} and define more operations on the bit string $S$ parametrized by the function $g$ which is to be applied.
\begin{description}
\item[The sum operation]
Since \rank{} will no longer be a primitive operation, we cannot use it to compute $G[i]$.
It is therefore replaced by an operation \summ{} which is equivalent to $G[j] - G[i - 1]$, which we used before in \ref{ss:rmq-def}.
It is defined as:
$$ \summ(S, g, i, j) = \sum_{k=i}^j g(S[k]). $$
\item[Linear search operations]
We replace \select{} in all forms by linear searching operations.
\begin{description}
\item[\fwdSearch{}]
Returns the first position $j$ after a given position $i$ such that the values sum up to $d$:
$$ \fwdSearch(S, g, i, d) = \min_{j > i} \{j : \summ(S, g, i, j) = d\}. $$
\item[\bwdSearch{}]
Works the same way as \fwdSearch{} except that it searches towards the beginning of the bit string:
$$ \bwdSearch(S, g, i, d) = \max_{j < i} \{j : \summ(S, g, j, i) = d\}. $$
\end{description}
Note that despite having used the same names in section \ref{s:match-enclose}, their definition here is different.
They refer to operations defined for all $i \in [0, N)$, rather than only to look-up tables.
\item[Range operations]
We generalize the range operations for any function $g$.
We also extend the original operation \rmqi{} into \rmqSelect{}, so that it does not only return the first occurrence of the minimum, but any occurrence in general.
\begin{description}
\item[\rmq{}]
Returns the value of the minimum in the given range.
$$\rmq(S, g, i, j) = \summ(S, g, 0, i - 1) + \min_{i \le k \le j} \summ(S, g, i, k)$$
\item[\rmqSize{}]
Returns how many occurrences of minimum there are in the given range:
$$\rmqSize(S, g, i, j) = | \{i \le k \le j : \summ(S, g, 0, k) = \rmq(S, g, i, j) \} |. $$
\item[\rmqRank{}]
Returns how many occurrences of minimum there are in the given range up to position $r$.
This can be directly implemented using \rmq{} and \rmqSize{}:
\begin{algorithm}
\begin{algorithmic}
\Function{\rmqRank}{$S, g, i, j, r$}
\If{$\rmq(S, g, i, j) \ne \rmq(S, g, i, r)$}
\State \Return{$0$}
\Else
\State \Return{$\rmqSize(S, g, i, r)$}
\EndIf
\EndFunction
\end{algorithmic}
\end{algorithm}
\item[\rmqSelect{}]
Returns the position $p$ of the $r$-th minimum in the range $i, j$:
$$\rmqSelect(S, g, i, j, r) = \min_{p \ge i} \{p : \rmqRank(S, g, i, j, p) \ge r\}.$$
\item[\RMQ{}, \RMQSize{}, \RMQRank{}, \RMQSelect{}]
The maximum variants of the range operations are defined similarly.
\end{description}
\end{description}
\bigbreak
We show how it is possible to realize all operations which we used as primitives in the previous succinct data structures only by sums, searches and range operations on calculated arrays.
We immediately get a data structure equivalent to those in the previous chapter in terms of the operations which they support.
Later we will show that even more operations become feasible for the FF representation.
We recall the definitions of the functions $\phi, \psi, \pi$ from the section \ref{ss:rmq-def}:
\begin{align*}
\phi(b) &= b, \\
\psi(b) &= 1 - b, \\
\pi(b) &= 2 b - 1.
\end{align*}
\begin{description}
\item[Operations on general bit strings]~\vspace{-1\baselineskip}
\begin{align*}
\rank_1(i) &= \summ(S, \phi, 0, i) \\
\select_1(n) &= \fwdSearch(S, \phi, 0, n) \\
\rank_0(i) &= \summ(S, \psi, 0, i) \\
\select_0(n) &= \fwdSearch(S, \psi, 0, n)
\end{align*}
\item[Operations on balanced bit strings]~\vspace{-1\baselineskip}
\begin{align*}
\findClose(i) &= \fwdSearch(S, \pi, i, 0) \\
\findOpen(i) &= \bwdSearch(S, \pi, i, 0) \\
\enclose(i) &= \bwdSearch(S, \pi, i, 2) \\
\enclose(i_1, i_2)&\ \textrm{stays the same} \\
\rmqi(E, i, j) &= \rmqSelect(S, \pi, i, j, 1) \\
\RMQi(E, i, j) &= \RMQSelect(S, \pi, i, j, 1)
\end{align*}
\end{description}
Therefore, while designing the index, we can focus only on the new operations.
\subsection{The Succinct Index}
As we are describing a universal index which is independent of its specific usage for representing trees, we again use $N$ to denote the size of the bit string.
We also parametrize all operations by the function $g$ which will be fixed for the rest of this section.
The structure of the index follows the general idea of dividing into blocks and them into small blocks.
Here the blocks will be of roughly polylogarithmic size and small blocks of size $b = \frac{\log N}{2}$ in order to be processed by using look-up tables.
If the query spans multiple small blocks within a single block, the queries are handled by min-max trees.
The queries spanning multiple blocks are answered by a macro structure.
\bigbreak
The small blocks are small enough to by used as indices of precomputed look-up tables, which answer all queries in constant time.
We use the following look-up tables which directly implement the primitive operations on small blocks:
\begin{description}
\item[\summ{}]
For a given range $i, j$, it returns the sum $v$.
\item[\fwdSearch{}, \bwdSearch{}]
For a given position $i$ and value $d$, they return the first next and previous position $p$ where the difference is $d$.
If the answer is not present, a special value is returned.
\item[\rmq{}, \RMQ{}]
For a given range $i, j$, they return the local value $v$ of minimum and maximum.
\item[\rmqSize{}, \RMQSize{}]
For a given range $i, j$, they return the number $r$ of occurrences of them minimum and maximum.
\item[\rmqSelect{}, \RMQSelect{}]
For a given range $i, j$ and $r$, they return the position $p$ of $r$-th minimum and maximum in the small block.
\end{description}
The values of $i, j, r$ are in range $[0, b - 1]$; the values $d, v$ are in range $[-b, b]$; the value of $p$ is in range $[0, b - 1]$ plus the special value indicating that such position does not exist in the small block.
Together we have 9 precomputed tables; each of them requires $O(\sqrt{N} \log N \log \log N)$ bits of memory.
The tables are used to answer all range operations whenever $i$ and $j$ are in the same small block.
In case of the searches, they also determine that the answer is not present in the small block.
We also use the look-up tables to handle the prefix and suffix of a range query, or at the beginning or the end of a search query.
\subsection{Min-Max Tree}
Each block contains $k^c$ small blocks for $k = \frac{\log N}{\log \log N}$ and an arbitrary integer constant $c \ge 1$.
The value of the constant $c$ will be discussed at the end.
The purpose of the min-max tree is to lift the operations from $b$ bits to $b k^c$ while retaining their constant running time.
The $l$-th block covers an interval of $B = b k^c$ bits spanning from the position $l B$ to $(l + 1) B - 1$.
To simplify the description we isolate the block by shifting the offsets so that it covers the range $[0, B - 1]$ by setting $l = 0$.
We also assume that the values in the array $G$ are in range $[-B, B]$; the global values can be computed simply by adding the value $G[l B - 1]$.
This does not have any impact on the correctness of the operations on the block level.
We extend the bit string $S$ to the nearest multiple of $B$ in order to ensure that each block is full.
The extra space caused by the extension is negligible.
Only the \fwdSearch{} operation could return an answer from the extended part, however it can happen only if the answer was not found earlier -- this case is easy to handle in the end.
\bigbreak
We build a perfect $k$-ary tree on top of the sequence of the small blocks (belonging to the block); we call it the \emph{min-max tree}.
The tree has a constant height equal to $c$.
Its leaves represent the information from the small blocks provided by the look-up tables; the inner nodes aggregate the information from their children.
We store the following values in each node which spans over the range $i, j$:
\begin{description}
\item[$e = \summ(S, g, 0, j)$]
The value at the end of the range.
\item[$m = \rmq(S, g, i, j), \ms = \rmqSize(S, g, i, j)$]
The minimum value and how many times it occurs.
\item[$M = \RMQ(S, g, i, j), \Ms = \RMQSize(S, g, i, j)$]
The maximum value and how many times it occurs.
\end{description}
The aggregation in the inner node is the obvious one:
\begin{iteminline}
\item the last value for $e$;
\item minimum for $m$, maximum for $M$;
\item sum for $\ms$ and $\Ms$.
\end{iteminline}
All values within a node require only $O(\log b)$ bits each.
As it is a perfect $k$-ary tree, we store the values of the nodes in a heap-like fashion in arrays $e[\cdot], m[\cdot], \ms[\cdot], M[\cdot], \Ms[\cdot]$.
We can navigate in this tree from an node with number $n$ to the parent ($\lfloor n / k \rfloor$), first child ($n \cdot k$), last child ($n \cdot k + k - 1$), previous sibling $n - 1$, next sibling $n + 1$.
All children of a node are stored together in consecutive $O(k \log \log N) = O(\log N) = c' b$ bits of memory, for a constant $c'$.
There are $\frac{k^{c + 1}}{k - 1} = O(k^c)$ nodes, each of them requires $O(\log b)$ bits.
The density of the tree structure built on top of the sequence of the small blocks is $O\left(\frac{k^c \log b}{k^c b}\right) = O\left(\frac{\log b}{b}\right) = o(1)$.
\bigskip
We show how to support the operations on the block level using traversal of the min-max tree and possibly delegation to small blocks.
By $b(i)$ we denote the index of the small block which contains the position $i$.
Contrary to the defined numbering of nodes, in order to simplify the notation, we assume in the algorithms that the node numbers of leaves are the same as indices of the small blocks with which they are associated.
We will use subscripts to refer to bits of $S$ represented by a small block $n$: $S_n = S[n b : (n + 1) b]$
\begin{algorithm}
\begin{algorithmic}
\Function{\sumBlock}{$S, g, i, j$}
\State $x \gets e[b(i - 1) - 1] + \summ[S_{b(i - 1)}, g, 0, (i - 1) \% b]$
\State $y \gets e[b(j) - 1] + \summ[S_{b(j)}, g, 0, j \% b]$
\State \Return{$y - x$}
\EndFunction
\end{algorithmic}
\end{algorithm}
\subsubsection{Search Operations}
We say that the small block $j$ \emph{covers} a value $v$ if $m[j] \le v \le M[j]$.
\begin{lemma}\label{l:search}
The answer to a search query starting at $i$ and looking for a difference $d$ (such that it is not answered in the small block $b(i)$) is in the first small block $j$ such that covers $v = \summ(S, g, 0, i) + d$.
No small block in the subtree of a min-max tree node not covering the value $v$ contains the answer.
\end{lemma}
\begin{proof}
Let us recall an important property of the $\pm 1$ functions $g$:
$$| G[i] - G[i-1] | \le 1.$$
From that it follows that each small block contains all values between its minimum and maximum.
The first small block which covers the desired value $v$ has to contain it.
Moreover, if $v < \rmq[S_{b(i)}, (i + 1) \% b, b - 1]$ (less than minimum of the rest of the small block), then it is sufficient to find the first small block $j$ such that $m[j] \ge v$ because $M[j] \ge m[j-1]$.
A similar statement holds for $v$ being greater than the maximum.
The extremes of a leaf are the same as of the small block with which it is associated.
If a small block $j$ contains the answer, then the value $v$ is covered by the leaf $j$.
Because of the aggregation method of inner vertices, all ancestors of $j$ cover $v$ too.
An inner vertex covers $v$ only if a leaf in its subtree covers $v$.
\end{proof}
When we process the search operations, we first check the small block containing $i$ for an answer.
If the answer is present there, we return the position, and stop.
Otherwise, we compute the desired value $v$ and traverse the tree up until we find the answer or until we reach the root.
If we get to root, we signal that this block does not contain the answer by returning a special value.
When we are in a node $a$, we check the extremes of its siblings in the direction in which we are searching.
This check can be performed in time $c'$ using a precomputed look-up table as all siblings are stored in consecutive $c' b$ bits.
Only one-sided checks are necessary as we noted in the lemma \ref{l:search}.
When we find a sibling covering $v$, we move to it and start descending to its first child covering the value $v$.
Finding such child again requires time $c'$ and another set of precomputed tables.
Once we descend to a leaf $j$, we compute the value $d' = v - e[j - 1]$ which we are going to look for in the associated small block using a look-up table.
As the height of the tree is $c$, the whole operation takes at most $O(2 c c') = O(1)$ steps.
We show the pseudocode for the operation \fwdSearch{}.
By an asterisk superscript we denote a repeated application of a look-up table on the list of consecutive nodes in the tree.
\begin{algorithm}
\begin{algorithmic}
\Function{\fwdSearchBlock}{$S, g, i, d$}
\State $p \gets \fwdSearch[S_{b(i)}, g, i \% b, d]$
\If{$p \ne -1$}
\State \Return{$p + b(i) b$}
\Else
\State $v \gets e[b(i) - 1] + \summ[S_{b(i)}, g, 0, i \% b] + d$
\State $n \gets b(i)$ \Comment{The current node, initialized to be a leaf}
\While{$(j \gets \nodeSearch^*[\rightSiblings(n), v]) = -1$} \Comment{Sibling search}
\State $n \gets \parent(n)$
\If{$\isRoot(n)$}
\State \Return{$-1$}
\EndIf
\EndWhile
\While{$\boolnot \isLeaf(j)$}
\State{$j \gets \nodeSearch^*[\children(j), v]$} \Comment{The first child covering $v$}
\EndWhile
\State $d' \gets v - e[j - 1]$ \Comment{The remaining difference}
\State \Return{$j b + \fwdSearch'(S_j, g, 0, d')$} \Comment{$d'$ in the small block $j$}
\EndIf
\EndFunction
\end{algorithmic}
\end{algorithm}
Note that the final \fwdSearch' is the standard \fwdSearch{} altered to allow the answer $0$, which is prohibited by the original definition.
This can be handled with a simple check for $\summ[S_j, g, 0, 0] = d'$.
\subsubsection{Range Operations}
The range operations work similarly: they traverse the tree up gathering information on the way, and in case of range select operations, they descend down.
If the query is fully contained within a small block, the answer is found in a look-up table.
In order to solve the other queries, we use two helper functions.
By the same argument as for the search operations, the running time is $O(2 c c') = O(1)$.
The helper function \rmqInfo{} which returns aggregated information for all siblings of a given node.
The function returns a tuple containing: the number of the first node, the number of the last node, the minimum, the maximum, the number of occurrences of the minimum, the number of occurrences of the maximum.
It iterates over $\frac{k}{c'}$ nodes in each step using look-up tables, which results in the running time $O(c')$.
Another helper function \rmqList{} gathers all aggregated info for subranges between $i$ and $j$.
It starts with the leaves $u = b(i)$ and $v = b(j)$, and keeps two lists of partial answers, one for each starting point.
We first ask the small blocks for their answers and remember them in their respective lists.
In a loop until $u$ and $v$ merge, we get the answers from the right siblings of $u$ and the left siblings of $v$, we remember them in the lists and move to their parents.
In the last iteration the left siblings and the right siblings overlap; therefore we store the answers in only one of the lists.
Each list contains at most $c$ values as the table look-ups aggregate the answers.
We combine the lists into a single list and find the answer to the queries in it.
\begin{algorithm}
\begin{algorithmic}
\Function{\rmqList}{$S, g, i, j$} \Comment{$b(i) \ne b(j)$}
\State $L \gets [\,]$%
\Instr $R \gets [\,]$ \Comment{Initialization of the empty lists}
\State $i_m \gets \rmq[S_{b(i)}, g, i \% b, b - 1] + e[b(i) - 1]$ \Comment{Similarly $j_m, i_M, j_M$}
\State $i_{\ms} \gets \rmqSize[S_{b(i)}, g, i \% b, b - 1]$ \Comment{Similarly $j_{\ms}, i_{\Ms}, j_{\Ms}$}
\State $\append(L, \{\struct{f}{b(i)}, \struct{l}{b(i)}, \struct{m}{i_m}, \struct{\ms}{i_{\ms}}, \struct{M}{i_M}, \struct{\Ms}{i_{\Ms}} \})$ \Comment{Info for $b(i)$}
\State $\prepend(R, \{\struct{f}{b(j)}, \struct{l}{b(j)}, \struct{m}{j_m}, \struct{\ms}{j_{\ms}}, \struct{M}{j_M}, \struct{\Ms}{j_{\Ms}} \})$ \Comment{Info for $b(j)$}
\While{$\parent(u) \ne \parent(v)$} \Comment{$u, v$ in different subtrees}
\State $\append(L, \rmqInfo(S, g, u, \str{r}))$
\State $\prepend(R, \rmqInfo(S, g, v, \str{l}))$
\EndWhile
\State $\append(L, \rmqInfo(S, g, u, v))$ \Comment{Between $u$ and $v$}
\State $\concatenate(L, R)$
\State \Return{$L$}
\EndFunction
\end{algorithmic}
\end{algorithm}
\bigbreak
For \rmq{} we return the minimum of the list obtained from \rmqList{}.
\begin{algorithm}
\begin{algorithmic}
\Function{\rmqBlock}{$S, g, i, j$}
\If{$b(i) = b(j)$}
\State \Return{$\rmq[S_{b(i)}, g, i \% b, j \% b] + e[b(i) - 1]$}
\Else
\State $L \gets \rmqList(S, g, i, j)$
\State $m \gets \infty$
\ForAll{$I \gets L$}
\State $m \gets \min(m, I.m)$
\EndFor
\State \Return{$m$}
\EndIf
\EndFunction
\end{algorithmic}
\end{algorithm}
For \rmqSizeBlock{} we sum $\ms$ for those info-nodes whose minimum is equal to the global one calculated by \rmq{}.
\begin{algorithm}
\begin{algorithmic}
\Function{\rmqSizeBlock}{$S, g, i, j$}
\If{$b(i) = b(j)$}
\State \Return{$\rmqSize[S_{b(i)}, g, i \% b, j \% b]$}
\Else
\State $m \gets \rmqBlock(G, i, j)$
\State $L \gets \rmqList(G, i, j)$
\State $ms \gets 0$
\ForAll{$I \gets L$}
\If{$I.m = m$}
\State $\ms \gets \ms + I.\ms$
\EndIf
\EndFor
\State \Return{$\ms$}
\EndIf
\EndFunction
\end{algorithmic}
\end{algorithm}
In case of \rmqSelect{}, we find the first node $n$ in the list such that the prefix sum of $\ms$ is greater than or equal to $r$.
We descend in such node into the first child whose left siblings including itself sum up to $r$, and repeat until we get to a leaf.
Once we are in a leaf, we use a look-up table to solve the select there.
\begin{algorithm}
\begin{algorithmic}
\Function{\rmqSelectBlock}{$S, g, i, j, r$} \Comment{Assuming $r$-th min exists}
\If{$b(i) = b(j)$}
\State $p \gets \rmqSelect[S_{b(i)}, g, i \% b, j \% b, r]$
\State \Return{$b(i) b + p$}
\Else
\State $m \gets \rmqBlock(G, i, j)$
\State $L \gets \rmqList(G, i, j)$
\State $ms \gets 0$
\ForAll{$I \gets L$}
\If{$I.m = m$}
\If{$\ms + I.\ms \ge r$}
\State \Break
\EndIf
\State $\ms \gets \ms + I.\ms$
\EndIf
\EndFor
\algstore{rmqselect}
\end{algorithmic}
\end{algorithm}
When the loop is broken, $I$ contains the information about list of siblings node with one of them containing the desired minimum.
Using look-up tables which are similar to \nodeSearch{}, we descend into the right child.
The table also returns the new remaining number of occurrences by subtracting those in nodes before $p$.
We first search the restricted range of nodes provided by $I$, keeping track of the number of the occurrences which we are interested in.
\begin{algorithm}
\begin{algorithmic}
\algrestore{rmqselect}
\State $(p, r) \gets \minSearch^*[\nodeRange(I.f, I.l), r - I.ms]$
\While{$\boolnot \isLeaf(p)$}
\State $(p, r) \gets \minSearch^*[\children(p), r]$
\EndWhile
\State $x \gets \max(i, p b) \% b$%
\Instr $y \gets \min(j, (p + 1) b - 1) \% b$
\State \Return{$b(p)b + \rmqSelect[S_{p}, g, x, y]$}
\EndIf
\EndFunction
\end{algorithmic}
\end{algorithm}
There are seven look-up tables necessary for traversal and processing the tree.
The tables have an additional (not mentioned) parameter which restricts the size of the block to be processed.
Whenever an index of a node is returned, it is turned into a node number by adding the number of the leftmost node involved in the query.
\begin{description}
\item[\fwdSearch]
The table is looking for the node which contains the first occurrence of $v$ in the block of consecutive nodes.
\bwdSearch{} requires another table returning the position of the last occurrence $v$.
\item[\rmqInfo]
Four tables aggregating information for consecutive nodes are necessary; the tables compute minimum, maximum, and sum conditioned by the value of minimum or maximum of the other nodes in the given range.
\item[\rmqSelect]
One table which is looking for the $r$-th occurrence of the minimum.
It returns the index of the node and the number of occurrences of the minimum in its preceding siblings.
\RMQSelect{} requires a similar table for occurrences of the maximum.
\end{description}
All tables are defined for blocks of $b$ bits with $O(1)$ parameters of size $O(\log k^c) = O(\log \log N)$.
\subsection{Macro Structure}
The macro structure starts with five arrays which summarize the results from the underlying blocks (the root nodes of their min-max trees).
\begin{description}
\item[$e[i\char93$]
The value at the end of the block $i$; in comparison to the block level, here we store the absolute values (the same for minima and maxima).
Each element has size of $O(\log N)$ bits.
\item[$m[i\char93$, $M[i\char93$]
The minimum and maximum values of the block $i$.
\item[$ms[i\char93$, $Ms[i\char93$]
The number of their occurrences in the block $i$.
Each element is of size $O(\log \log N)$ bits.
\end{description}
\bigbreak
We denote $B(i)$ to the index of the block containing the position $i$.
The algorithm for \summ is very similar to \sumBlock.
\begin{algorithm}
\begin{algorithmic}
\Function{\summ}{$S, g, i, j$}
\State $x \gets e[B(i - 1) - 1] + \sumBlock(S_{B(i - 1)}, g, 0, (i - 1) \% B)$
\State $y \gets e[B(j) - 1] + \sumBlock(S_{B(j)}, g, 0, j \% B)$
\State \Return{$y - x$}
\EndFunction
\end{algorithmic}
\end{algorithm}
\subsubsection{Search Operations}\label{ff-search}
For each block $i$ we define an array of \emph{left-to-right minima} $\lrm_i$ such that
\begin{align*}
\lrm_i[0] &= i \\
\lrm_i[j + 1] &= \min \{k : k > \lrm_i[j] \booland m[k] > m[\lrm_i[k]]\}
\end{align*}
Similarly we define left-to-right maxima arrays $\LRM_i$, and for backward searching we define the arrays $\rlm_i$ and $\RLM_i$.
We focus only on left-to-right direction.
The arrays $\lrm$ and $\LRM$ can be used to implement the global operation $\fwdSearch(S, g, i, d)$.
Let's assume that the result is not in block $B(i)$, if it was, we can solve it on the block level.
We are looking for a block $j \ge B(i) + 1 = n$ such that $j$ covers the value $v = \summ(S, g, 0, i) + d$.
There are three possibilities of how to find the block $j$:
\begin{enumerate}
\item If $n$ covers $v$, then the first block after $B(i)$ contains the answer.
\item If $v < m[n]$, we search for the block in the array $\lrm$.
\item Otherwise, we search in the array $\LRM$.
\end{enumerate}
It can happen that the value $v$ is not covered by any block $j$, in which case we report a failure.
\begin{algorithm}
\begin{algorithmic}
\Function{\fwdSearch}{$S, g, i, d$}
\State $p \gets \fwdSearchBlock(S_{B(i)}, g, i \% B, d)$ \Comment{Block operation offset}
\If{$p \ne -1$}
\State \Return{$B(i) B + p$}
\Else
\State $v \gets \summ(S, g, 0, i) + d$
\State $n \gets B(i) + 1$
\If{$v < m[n]$}
\State $j \gets \lrmSearch(n, v)$
\ElsIf{$v > M[j]$}
\State $j \gets \LRMSearch(n, v)$
\Else
\State $j \gets n$
\EndIf
\If{$j = -1$} \Comment{A search reported that $v$ is not covered.}
\State \Return{$-1$}
\Else
\State $d' \gets v - \summ(S, g, 0, B(j) B - 1)$
\State \Return{$B(j) B + \fwdSearchBlock'(S_{B(j)}, g, 0, d')$}
\EndIf
\EndIf
\EndFunction
\end{algorithmic}
\end{algorithm}
Note that the final call of \fwdSearchBlock{}' is the standard block operation \fwdSearchBlock{} altered to allow the answer $0$.
The same was necessary for the search on the block level.
\bigbreak
It remains to show a constant time algorithm for the operation \lrmSearch{} (\LRMSearch{} is similar).
The \naive{} approach would be to store each $\lrm_i$ as an array $A$ turned into a compressed array using operation \succ{}.
The array $A$ is defined partially for all $j$ in $\lrm_i$: $ A[M[i] - m[j]] = j$.
The problem with this approach is that, the compressed arrays for all $i$ can contain $O\left(\left(\frac{N}{B}\right)^2\right)$ runs in total, which is too much.
\bigbreak
We observe that the arrays $\lrm_i$ are alike.
\begin{lemma}
Let a block $a$ be in two different arrays $\lrm_i$ and $\lrm_j$:
$$ \lrm_i[x_i] = a = \lrm_j[x_j], $$
then all following values in the arrays are the same:
$$ \lrm_i[x_i + k] = \lrm_j[x_j + k] \ \forall k \ge 0. $$
\end{lemma}
\begin{proof}
It follows from the definition of the array $\lrm$
\end{proof}
\label{Tlrm} We define a tree $T_{\lrm}$ which is built as a trie for reversed $\lrm$ arrays.
We add an artificial root, in order to make it a tree; it will not be used in algorithms.
\begin{example}
Let $m = [6, 4, 9, 7, 4, 4, 1, 8, 5]$, then the arrays $\lrm_i$ are:
\begin{align*}
\lrm_0 &= [0, 1, 6] &
\lrm_1 &= [1, 6] &
\lrm_2 &= [2, 3, 4, 6] \\
\lrm_3 &= [3, 4, 6] &
\lrm_4 &= [4, 6] &
\lrm_5 &= [5, 6] \\
\lrm_6 &= [6] &
\lrm_7 &= [7, 8] &
\lrm_8 &= [8]
\end{align*}
The tree $T_{\lrm}$ built from the arrays is shown in the figure \ref{fig:ex:tlrm}.
The values of $m$ are in parentheses.
\begin{figure}
\centering
\begin{tikzpicture}
[
every node/.style = {
shape=rectangle, rounded corners,
draw, align=center
},
baseline,
align=center,
level distance=2\baselineskip,
text depth=.1em,
text height=.8em,
level 1/.style={sibling distance=10em},
level 2/.style={sibling distance=5em},
level 3/.style={sibling distance=2em},
level 4/.style={sibling distance=1em}
]
\node {root}
child { node {6 (1)}
child { node {1 (4)}
child { node {0 (6)} }
}
child { node {4 (4)}
child { node {3 (7)}
child { node {2 (9)} }
}
}
child { node {5 (4)} }
}
child { node {8 (5)}
child { node {7 (8)} }
};
\end{tikzpicture}
\caption{An example of tree $T_{\lrm}$ (\cite{sadakane2010fully}).}
\label{fig:ex:tlrm}
\end{figure}
\end{example}
The properties of the tree are:
\begin{enumerate}
\item $m[\parent(i)] < m[i] \le m[\parent(i)] + B$;
\item $i < \parent(i)$;
\item the tree has $\frac{N}{B} + 1$ nodes.
\end{enumerate}
The search for the value $v$ in $\lrm_i$ is transformed to a search for an ancestor $j$ of $i$ in the tree $T_{\lrm}$ such that $m[j] \le v$.
We split the search into two parts:
\begin{enumerate}
\item a search in $\lrm_i$ restricted to powers of two;
\item a search in $\lrm_i$ with a bounded distance.
\end{enumerate}
\bigbreak
We use the $\langle O(n \log n); O(1) \rangle$ algorithm for querying level ancestor introduced in \cite{bender2004level}.
Instead of searching through the whole array $\lrm_i$, we limit the number of elements to $\log N$:
$$ \lrm'_i[j] = \lrm_i[2^j]. $$
This array is turned into a tiny compressed array of jumps $J_i$ requiring only $O(\log^2 N)$ bits of space, which is $O\left(\frac{N}{B} \log^2 N\right) = o(N)$ for $c \ge 2$.
By using the tiny compressed array, we move from node $i$ to a node $j'$ of $T_{\lrm}$ for which we bounds on depth and height, and the number of ancestors which we have to search through.
\begin{align*}
\dep(j') - \dep(j) &< \dep(i) - \dep(j') \\
\hei(j') &\ge \dep(i) - \dep(j') \\
\dep(j') - \dep(j) &< \hei(j')
\end{align*}
Note that if we tried to be more clever and represent nodes $j$ which cover values $m[i] - m[j]$ instead of those at distance $2^j$, then the bounds would not hold.
\bigbreak
The tree gets decomposed iteratively to paths; in each step the longest path $p$ from a node $\start(p)$ to a leaf $\en(p)$ is removed.
If the path contains $l = \length(p)$ nodes, we prepend it with $l$ more ancestors (or less if the root is reached) and call it a \emph{ladder}.
There are as many ladders as leaves of the tree $T_{\lrm}$, and all ladders together contain $\le 2 \frac{N}{B}$ nodes.
Note that the root of the tree $T_{\lrm}$ is not represented in the ladders.
The previous bound guaranties that the final answer $j$ is in the ladder which was extended from a path containing the node $j'$, to which we got by using the tiny compressed dictionary $J_i$.
We represent each ladder by an array of block indices (which are nodes of $T_{\lrm}$) covering the value $M[\start(p)] - v$ (using maximum makes sure that the whole block $\start(p)$ is represented).
We combine all ladders together in a single compressed array $L$ which leads to $2 \frac{N}{B}$ runs in total; as the difference of $m[i] - m[\parent(i)] \le B$ is bounded, the total number of elements is $O(N)$.
The space complexity of the compressed array $L$ is $O\left(\frac{N}{B} \log N\right) + o(N) = o(N)$
For each block $i$, we also store the index $l$ of the ladder, which is also the index of the part in the compressed dictionary $L$, in an array $\ladder$.
The size of the array is $O\left(\frac{N}{B} \log N\right) = o(N)$ bits.
\begin{algorithm}
\begin{algorithmic}
\Function{\lrmSearch}{$i, v$}
\State $j' \gets J_i[M[i] - v]$ \Comment{Jump by power of two}
\State $l \gets \ladder[j']$ \Comment{The ladder containing $j'$}
\State $\en \gets L[l, 0]$
\State $j \gets L[l, M[\en] - v]$ \Comment{If the index is out of the range, $-1$ is returned}
\State \Return{$j$}
\EndFunction
\end{algorithmic}
\end{algorithm}
\subsubsection{Range Operations}
We split the queried range into a prefix, suffix, and span.
The solution for the prefix and suffix is solved on the block level; we focus on supporting the operations for spans of the queries.
We use a helper function \rmqiSpan{} which was defined in lemma \ref{lemma:rmq2} with two minor differences:
\begin{enumerate}
\item it is defined directly for the array $m[\cdot]$ instead of $E$ and $P$;
\item it returns indices of the first and the last block containing the minimum;
\item contrary to \rmqi{} shown in \ref{sss:rmq-index}, it is sufficient to use only one level provided that we set $c \ge 2$.
\end{enumerate}
\begin{algorithm}
\begin{algorithmic}
\Function{\rmq}{$S, g, i, j$}
\State $m_1 \gets e[B(i) - 1] + \rmqBlock(S_{B(i)}, g, \prefix(i, j))$
\State $m_2 \gets e[B(j) - 1] + \rmqBlock(S_{B(j)}, g, \suffix(i, j))$
\State $(f, l) \gets \rmqiSpan(m, \spann(i, j))$
\State $m_3 \gets m[f]$
\State \Return{$\min(m_1, m_2, m_3)$}
\EndFunction
\end{algorithmic}
\end{algorithm}
The structure for \rmqiSpan{} cannot be extended for querying the number of occurrences of the minimum nor selecting the $r$-th one.
If we augmented each precomputed interval of size $2^k$ with the number of occurrences, we would still fit in the same space.
The problem stems for the inability of an easily combination of information from the two intervals, like the $\min$ function does.
In this case, the inclusion-exclusion principle would have to be used: adding the numbers of occurrences in both intervals and subtracting the number of occurrences in the overlapping part.
Since the size of the overlapping part is not a power of two in general, and so its number of occurrences is not precomputed, it leads to recursion and running time $O(\log N)$.
\subsubsection{Structures for \rmqSize{} and \rmqSelect{}}
We store the several indexable and fully indexable dictionaries which use the properties of the minima of the blocks.
The idea is to reorder the blocks, which are represented by elements in $m[\cdot]$, so that all blocks containing the same minimum are stored consecutively.
There are at most $\frac{N}{B}$ distinct minima in range $[-N, N]$.
We store the minima in an indexable dictionary $\mr{}$ offsetted by $N$ to become a range $[0, 2 N]$
The indexable dictionary uses $O\left(\frac{N}{B} \log B\right)$ bits and it supports \rank{} on the minima in constant time.
This is used for referring to $k$-th smallest minimum in the following structures.
We omit the correction of an off-by-one error caused by \rank{} as the smallest minimum has $k = 1$ instead of desired $k = 0$.
For each block minimum $m$ (which is the $k$-th smallest), we define a set containing all blocks which have minimum $m$: $\{ i : m[i] = m\}$.
We represent the set as an indexable dictionary $\mi_k$.
Because of the inequality derived from the generalized Chu-Vandermonde's identity \cite{belbachircombinatorial}, the space complexity of all $\mi_m$ together can be bounded by:
\begin{align*}
\sum_m S(\mi_k) &= \sum_m \left( \log {\frac{N}{B} \choose |\mi_k|} + o(|\mi_k|) + O(\log \log N) \right) \\
&\le \log {\left(\frac{N}{B}\right)^2 \choose \frac{N}{B}} + o\left(\frac{N}{B}\right) + O\left(\frac{N}{B} \log \log N\right) \\
&= O\left(\frac{N}{B} \log N\right) = o(N).
\end{align*}
Finally we define an array $\mpp_k$ for each block minimum $m$ (which is again the $k$-th smallest):
$$\mpp_k\left[\sum_{\substack{j \in \mi_k \\ j \le i}} \ms[j] \right] = i \ \forall i \in \mi_k.$$
We turn all these arrays into a single compressed array $\mpp$ using the operation \succ{}.
The property of this array, from which it was also derived, connects it to a block $t$ which contains the $r$-th occurrence of the $k$-th smallest minimum:
$$ t = \select_1(\mi_k, \mpp[k, r]). $$
All the structures require only $o(N)$ bits of memory.
\bigbreak
As we restricted our queries to their spans, and from \rmqiSpan{} we know the indices of the first and last block containing the minimum, it is easy to find how many occurrences there were before the any block.
\begin{algorithm}
\begin{algorithmic}
\Function{\rmqSize}{$S, g, i, j$}
\State $\ms_1 \gets 0$%
\Instr $\ms_2 \gets 0$%
\Instr $\ms_3 \gets 0$
\State $m \gets \rmq(S, g, i, j)$
\If{$e[B(i) - 1] + \rmqBlock(S_{B(i)}, g, \prefix(i, j)) = m$} \Comment{In prefix?}
\State $\ms_1 \gets \rmqSizeBlock(S_{B(i)}, g, \prefix(i, j))$
\EndIf
\If{$e[B(j) - 1] + \rmqBlock(S_{B(j)}, g, \suffix(i, j)) = m$} \Comment{In suffix?}
\State $\ms_2 \gets \rmqSizeBlock(S_{B(j)}, g, \suffix(i, j))$
\EndIf
\State $(f, l) \gets \rmqiSpan(m, \spann(i, j))$
\If{$m[f] = m$} \Comment{In span?}
\State $k \gets \rank_1(\mr, m + N)$ \Comment{$k$-th smallest minimum}
\State $x \gets \runFirst(\mpp, k, \rank_1(\mi_k, f))$ \Comment{Before the run of $f$}
\State $y \gets \runFirst(\mpp, k, \rank_1(\mi_k, l))$ \Comment{The block $l$ is not accounted}
\State $\ms_3 \gets y - x + \ms[l]$
\EndIf
\State \Return{$m_1 + m_2 + m_3$}
\EndFunction
\end{algorithmic}
\end{algorithm}
The operation \rmqSelect{} first looks for the minimum in the prefix, then in the span, and finally in the suffix.
The idea of the search in span is to use the property of $\mpp$.
\begin{algorithm}
\begin{algorithmic}
\Function{\rmqSelect}{$S, g, i, j, r$}
\State $m \gets \rmq(S, g, i, j)$
\If{$e[B(i) - 1] + \rmqBlock(S_{B(i)}, g, \prefix(i, j)) = m$} \Comment{In prefix?}
\State $\ms_1 \gets \rmqSizeBlock(S_{B(i)}, g, \prefix(i, j))$ \Comment{Prefix size}
\If{$\ms_1 \ge r$}
\State \Return{$B(i) B + \rmqSelectBlock(S_{B(i)}, g, \prefix(i, j), r)$}
\Else
\State $r \gets r - \ms_1$
\EndIf
\EndIf
\State $(f, l) \gets \rmqiSpan(m, \spann(i, j))$
\If{$m[f] = m$} \Comment{In span?}
\State $\ms_3 \gets \rmqSize(S, g, \spann(i, j))$
\If{$\ms_3 \ge r$}
\State $k \gets \rank_1(\mr, m + N)$ \Comment{$k$-th smallest minimum}
\State $x \gets \runFirst(\mpp, k, \rank_1(\mi_k, f))$ \Comment{Before the run of $f$}
\State $t' \gets \mpp[k, r + x]$
\State $t \gets \select_1(\mi_k, t')$ \Comment{Block containing the desired occurrence}
\State $y \gets \runFirst(\mpp, k, t')$ \Comment{Elements before the run of block $t$}
\State \Return{$t B + \rmqSelectBlock(S_t, g, 0, B - 1, r - y)$}
\Else
\State $r \gets r - \ms_3$
\EndIf
\EndIf
\If{$e[B(j) - 1] + \rmqBlock(S_{B(j)}, g, \suffix(i, j)) = m$} \Comment{In suffix?}
\State $\ms_2 \gets \rmqSizeBlock(S_{B(j)}, g, \suffix(i, j))$ \Comment{Prefix size}
\If{$\ms_2 \ge r$}
\State \Return{$B(j) B + \rmqSelectBlock(S_{B(j)}, g, \suffix(i, j), r)$}
\Else
\State \Comment{This cannot happen as the bounds of $r$ has been checked}
\EndIf
\EndIf
\EndFunction
\end{algorithmic}
\end{algorithm}
\subsection{$\pm 1$ Functions Revisited}
We follow in discussion of $\pm 1$ functions which we began in section \ref{ss:rmq-def}.
Although there are 9 functions $g$ mapping $\{0, 1\} \to \{-1, 0, 1\}$, we only use 3 of them in our index: $\pi, \phi, \psi$.
Answers to all operations except for searches with parameters $\phi$ and $\psi$ can be answered using the structure for $\pi$.
For the operation $\summ$, the following identities hold:
\begin{gather*}
\summ(S, \pi, i, j) = \summ(S, \phi, i, j) - \summ(S, \psi, i, j), \\
\summ(S, \phi, i, j) + \summ(S, \psi, i, j) = j - i + 1.
\end{gather*}
The explicit formula for \summ{}s follow from the previous equations:
\begin{align*}
\summ(S, \phi, i, j) &= \frac{(j - i + 1) + \summ(S, \pi, i, j)}{2}, \\
\summ(S, \psi, i, j) &= \frac{(j - i + 1) - \summ(S, \pi, i, j)}{2}.
\end{align*}
Because the sums of $g \in \{\phi, \psi \}$ are monotonic, we can reduce all range operations to sums and searches.
In the range $i, j$, the minimum occurs for the first time at $i$ and maximum occurs for the last time at $j$.
All occurrences are continuous.
\begin{align*}
\rmq(S, g, i, j) &= \summ(S, g, 0, i) \\
\RMQ(S, g, i, j) &= \summ(S, g, 0, j) \\
\rmqSize(S, g, i, j) &= \min(j + 1, \fwdSearch(S, g, i, 1)) - i \\
\RMQSize(S, g, i, j) &= j - \max(i - 1, \bwdSearch(S, g, j, 1)) \\
\rmqSelect(S, g, i, j, r) &= i + r - 1 \\
\RMQSelect(S, g, i, j, r) &= j - \RMQSize(S, g, i, j) + r
\end{align*}
\subsubsection{Search Operations}
The operation \bwdSearch{} can be the reduced to \fwdSearch{} and \summ{}.
\begin{algorithm}
\begin{algorithmic}
\Function{\bwdSearch}{$S, g, i, d$}
\If{$d > 0$}
\State $v \gets \summ(S, g, 0, i) - d + 1$
\If{$v \ge 2 \boolor (v = 1 \booland g(S[0]) \ne 1)$}
\State \Return{$\fwdSearch(S, g, 0, v)$}
\ElsIf{$v = 1$}
\State \Return{$0$}
\Else
\State \Return{$-1$}
\EndIf
\ElsIf{$d = 0 \booland g(S[i]) = 0 \booland g(S[i-1]) = 0$}
\State \Return{$i - 1$}
\Else
\State \Return{$-1$}
\EndIf
\EndFunction
\end{algorithmic}
\end{algorithm}
The only non-trivial operation is \fwdSearch{}.
We review the structures which are used in its index and note places which can be simplified.
No simplification is possible for small block (the look-up tables) and block (min-max tree) levels.
In the macro structure, only the tree $T_{\LRM}$ is necessary ($T_{\lrm}$ is not because of the monotonicity of $g$).
The tree is also degenerated into a path.
The tiny compressed array of jumps are not necessary as the whole path forms a ladder; the search in the ladder stays the same.
The operation \fwdSearch{} can be used to implement \select{}, which makes the index for \fwdSearch{} an alternative to the index which we showed in the section \ref{ss:select}.
\begin{algorithm}
\begin{algorithmic}
\Function{\select$_1$}{$S, i$}
\If{$i \ge 2 \boolor (i = 1 \booland S[0] \ne 1)$} \Comment{In case of \select$_0$: $\ne 0$}
\State \Return{$\fwdSearch(S, \phi, 0, i)$} \Comment{In case of \select$_0$: $\psi$}
\ElsIf{$i = 1$}
\State \Return{$0$}
\Else
\State \Return{$-1$}
\EndIf
\EndFunction
\end{algorithmic}
\end{algorithm}
\subsection{Extension of BP}
The index which we developed in the previous section can be used for any representation, however only the BP representation benefits from it as many operations can be immediately supported.
Child operations, mainly \degree{}, \childRank{}, and \childSelect{}, which are important for basic navigation in the tree, were only available with a specialized index.
They were also the reason for developing the DFUDS representation, which supports them natively.
Using the generalized \rmq{} operations, we present an alternative implementation to the one described by \cite{sadakane2010fully}.
The key observation is that in a representation of a subtree of a vertex $i$ with omitted boundary parentheses, the occurrences of minimum correspond with the terminal parentheses of the children of $i$.
\begin{algorithm}
\begin{algorithmic}
\Function{\degree}{$i$}
\State \Return{$\rmqSize(S, \pi, i + 1, \findClose(S, i) - 1)$}
\EndFunction
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\begin{algorithmic}
\Function{\childRank}{$i$}
\State $p \gets \parent(i)$
\State \Return{$\rmqRank(S, \pi, p + 1, \findClose(S, p) - 1, \findClose(S, i))$}
\EndFunction
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\begin{algorithmic}
\Function{\childSelect}{$i, k$}
\State \Return{$\findOpen(S, \rmqSelect(S, \pi, i + 1, \findClose(i) - 1, k))$}
\EndFunction
\end{algorithmic}
\end{algorithm}
The operation \levelAncestor{} is now a straightforward generalization of \enclose{}.
\begin{algorithm}
\begin{algorithmic}
\Function{\levelAncestor}{$i, d$}
\If{$d = 0$}
\State \Return{$i$}
\Else
\State \Return{$\bwdSearch(S, \pi, i, d + 1)$}
\EndIf
\EndFunction
\end{algorithmic}
\end{algorithm}
It is also possible to realize some of the \levelAny{} operations with the restriction to a subtree of a vertex $a$.
The most general ones: \levelSize{}, \levelRank{}, and \levelSelect{} are however not supported.
\begin{algorithm}
\begin{algorithmic}
\Function{\levelFirst}{$a, l$}
\If{$\dep(S, a) = l$}
\State \Return{$a$}
\Else
\State \Return{$\fwdSearch(S, \pi, a, l - \dep(S, a))$}
\EndIf
\EndFunction
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\begin{algorithmic}
\Function{\levelLast}{$a, l$}
\If{$\dep(S, a) = l$}
\State \Return{$a$}
\Else
\State \Return{$\findOpen(S, \bwdSearch(S, \pi, \findClose(S, a), \dep(S, a) - l)$}
\EndIf
\EndFunction
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\begin{algorithmic}
\Function{\levelNext}{$a, i$}
\State \Return{$\fwdSearch(S, \pi, \findClose(S, i), 0)$}
\EndFunction
\end{algorithmic}
\end{algorithm}
\begin{algorithm}
\begin{algorithmic}
\Function{\levelPrev}{$a, i$}
\State \Return{$\findOpen(S, \bwdSearch(S, \pi, i, 0))$}
\EndFunction
\end{algorithmic}
\end{algorithm}
\subsection{Final Thoughts}
The constant $c$ needs to be set to $c \ge 2$ because of the level-ancestor and range minimum query macro structure.
\bigbreak
The single index which we presented is more general than all the indices which used the BP representation.
As a consequence more operations are supported, in fact more than any of the previous representations.
The only unsupported operations by this index are
\begin{enuminline}
\item the \rank{} and \select{} in respect to lo-order and dfuds-order numberings of vertices;
\item arbitrary access for level queries through \levelSize{}, \levelRank{}, and \levelSelect{} operations.
\end{enuminline}
All of the listed operations would benefit from a better handling of the level structure of the tree, an operation like \fwdSearch{} which is parametrized by the number of the occurrence.
If these operations are crucial, the only data structure which supports them is the LOUDS.
\section{Tree Covering}\label{s:TC}