<s>
In	O
data	B-General_Concept
structures	I-General_Concept
,	O
a	O
range	B-Data_Structure
query	I-Data_Structure
consists	O
of	O
preprocessing	O
some	O
input	O
data	O
into	O
a	O
data	B-General_Concept
structure	I-General_Concept
to	O
efficiently	O
answer	O
any	O
number	O
of	O
queries	O
on	O
any	O
subset	O
of	O
the	O
input	O
.	O
</s>
<s>
Particularly	O
,	O
there	O
is	O
a	O
group	O
of	O
problems	O
that	O
have	O
been	O
extensively	O
studied	O
where	O
the	O
input	O
is	O
an	O
array	B-Data_Structure
of	O
unsorted	O
numbers	O
and	O
a	O
query	O
consists	O
of	O
computing	O
some	O
function	O
,	O
such	O
as	O
the	O
minimum	O
,	O
on	O
a	O
specific	O
range	O
of	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
A	O
range	B-Data_Structure
query	I-Data_Structure
on	O
an	O
array	B-Data_Structure
of	O
n	O
elements	O
of	O
some	O
set	O
,	O
denoted	O
,	O
takes	O
two	O
indices	O
,	O
a	O
function	O
defined	O
over	O
arrays	O
of	O
elements	O
of	O
and	O
outputs	O
.	O
</s>
<s>
For	O
example	O
,	O
for	O
and	O
an	O
array	B-Data_Structure
of	O
numbers	O
,	O
the	O
range	B-Data_Structure
query	I-Data_Structure
computes	O
,	O
for	O
any	O
.	O
</s>
<s>
These	O
queries	O
may	O
be	O
answered	O
in	O
constant	O
time	O
and	O
using	O
extra	O
space	O
by	O
calculating	O
the	O
sums	O
of	O
the	O
first	O
elements	O
of	O
and	O
storing	O
them	O
into	O
an	O
auxiliary	O
array	B-Data_Structure
,	O
such	O
that	O
contains	O
the	O
sum	O
of	O
the	O
first	O
elements	O
of	O
for	O
every	O
.	O
</s>
<s>
When	O
the	O
function	O
of	O
interest	O
in	O
a	O
range	B-Data_Structure
query	I-Data_Structure
is	O
a	O
semigroup	O
operator	O
,	O
the	O
notion	O
of	O
is	O
not	O
always	O
defined	O
,	O
so	O
the	O
strategy	O
in	O
the	O
previous	O
section	O
does	O
not	O
work	O
.	O
</s>
<s>
Andrew	O
Yao	O
showed	O
that	O
there	O
exists	O
an	O
efficient	O
solution	O
for	O
range	B-Data_Structure
queries	I-Data_Structure
that	O
involve	O
semigroup	O
operators	O
.	O
</s>
<s>
He	O
proved	O
that	O
for	O
any	O
constant	O
,	O
a	O
preprocessing	O
of	O
time	O
and	O
space	O
allows	O
to	O
answer	O
range	B-Data_Structure
queries	I-Data_Structure
on	O
lists	O
where	O
is	O
a	O
semigroup	O
operator	O
in	O
time	O
,	O
where	O
is	O
a	O
certain	O
functional	O
inverse	O
of	O
the	O
Ackermann	O
function	O
.	O
</s>
<s>
Then	O
denotes	O
the	O
corresponding	O
minimum	O
range	B-Data_Structure
query	I-Data_Structure
.	O
</s>
<s>
There	O
are	O
several	O
data	B-General_Concept
structures	I-General_Concept
that	O
allow	O
to	O
answer	O
a	O
range	B-Algorithm
minimum	I-Algorithm
query	I-Algorithm
in	O
time	O
using	O
a	O
preprocessing	O
of	O
time	O
and	O
space	O
.	O
</s>
<s>
The	O
Cartesian	B-Algorithm
tree	I-Algorithm
of	O
an	O
array	B-Data_Structure
has	O
as	O
root	O
and	O
as	O
left	O
and	O
right	O
subtrees	B-Application
the	O
Cartesian	B-Algorithm
tree	I-Algorithm
of	O
and	O
the	O
Cartesian	B-Algorithm
tree	I-Algorithm
of	O
respectively	O
.	O
</s>
<s>
A	O
range	B-Algorithm
minimum	I-Algorithm
query	I-Algorithm
is	O
the	O
lowest	O
common	O
ancestor	O
in	O
of	O
and	O
.	O
</s>
<s>
Because	O
the	O
lowest	O
common	O
ancestor	O
can	O
be	O
solved	O
in	O
constant	O
time	O
using	O
a	O
preprocessing	O
of	O
time	O
and	O
space	O
,	O
range	B-Algorithm
minimum	I-Algorithm
query	I-Algorithm
can	O
as	O
well	O
.	O
</s>
<s>
Cartesian	B-Algorithm
trees	I-Algorithm
can	O
be	O
constructed	O
in	O
linear	O
time	O
.	O
</s>
<s>
The	O
mode	O
of	O
an	O
array	B-Data_Structure
A	O
is	O
the	O
element	O
that	O
appears	O
the	O
most	O
in	O
A	O
.	O
</s>
<s>
Several	O
data	B-General_Concept
structures	I-General_Concept
have	O
been	O
devised	O
to	O
solve	O
this	O
problem	O
,	O
we	O
summarize	O
some	O
of	O
the	O
results	O
in	O
the	O
following	O
table	O
.	O
</s>
<s>
proved	O
a	O
lower	O
bound	O
on	O
the	O
cell-probe	B-Application
model	I-Application
of	O
for	O
any	O
data	B-General_Concept
structure	I-General_Concept
that	O
uses	O
cells	O
.	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
the	O
median	O
problem	O
,	O
a	O
special	O
case	O
of	O
the	O
selection	B-Algorithm
problem	I-Algorithm
,	O
is	O
solvable	O
in	O
O(n )	O
,	O
using	O
the	O
median	B-Algorithm
of	I-Algorithm
medians	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
There	O
have	O
been	O
studied	O
two	O
variants	O
of	O
this	O
problem	O
,	O
the	O
offline	B-Algorithm
version	O
,	O
where	O
all	O
the	O
k	O
queries	O
of	O
interest	O
are	O
given	O
in	O
a	O
batch	O
,	O
and	O
a	O
version	O
where	O
all	O
the	O
preprocessing	O
is	O
done	O
up	O
front	O
.	O
</s>
<s>
The	O
offline	B-Algorithm
version	O
can	O
be	O
solved	O
with	O
time	O
and	O
space	O
.	O
</s>
<s>
The	O
following	O
pseudocode	O
of	O
the	O
quickselect	B-Algorithm
algorithm	I-Algorithm
shows	O
how	O
to	O
find	O
the	O
element	O
of	O
rank	O
in	O
an	O
unsorted	O
array	B-Data_Structure
of	O
distinct	O
elements	O
,	O
to	O
find	O
the	O
range	O
medians	O
we	O
set	O
.	O
</s>
<s>
The	O
total	O
cost	O
for	O
any	O
query	O
,	O
without	O
considering	O
the	O
partitioning	O
part	O
,	O
is	O
since	O
at	O
most	O
recursion	O
calls	O
are	O
done	O
and	O
only	O
a	O
constant	O
number	O
of	O
operations	O
are	O
performed	O
in	O
each	O
of	O
them	O
(	O
to	O
get	O
the	O
value	O
of	O
fractional	B-Data_Structure
cascading	I-Data_Structure
should	O
be	O
used	O
)	O
.	O
</s>
<s>
The	O
algorithm	O
can	O
also	O
be	O
modified	O
to	O
solve	O
the	O
online	B-Algorithm
version	O
of	O
the	O
problem	O
.	O
</s>
<s>
One	O
of	O
the	O
most	O
famous	O
algorithms	O
for	O
finding	O
the	O
majority	O
of	O
an	O
array	B-Data_Structure
was	O
proposed	O
by	O
Boyer	O
and	O
Moore	O
which	O
is	O
also	O
known	O
as	O
the	O
Boyer	O
–	O
Moore	O
majority	O
vote	O
algorithm	O
.	O
</s>
<s>
In	O
the	O
context	O
of	O
Boyer	O
and	O
Moore	O
’s	O
work	O
and	O
generally	O
speaking	O
,	O
a	O
majority	O
element	O
in	O
a	O
set	O
of	O
items	O
(	O
for	O
example	O
string	O
or	O
an	O
array	B-Data_Structure
)	O
is	O
one	O
whose	O
number	O
of	O
instances	O
is	O
more	O
than	O
half	O
of	O
the	O
size	O
of	O
that	O
set	O
.	O
</s>
<s>
Few	O
years	O
later	O
,	O
Misra	O
and	O
Gries	O
proposed	O
a	O
more	O
general	O
version	O
of	O
Boyer	O
and	O
Moore	O
's	O
algorithm	O
using	O
comparisons	O
to	O
find	O
all	O
items	O
in	O
an	O
array	B-Data_Structure
whose	O
relative	O
frequencies	O
are	O
greater	O
than	O
some	O
threshold	O
.	O
</s>
<s>
A	O
range	O
-majority	O
query	O
is	O
one	O
that	O
,	O
given	O
a	O
subrange	O
of	O
a	O
data	B-General_Concept
structure	I-General_Concept
(	O
for	O
example	O
an	O
array	B-Data_Structure
)	O
of	O
size	O
,	O
returns	O
the	O
set	O
of	O
all	O
distinct	O
items	O
that	O
appear	O
more	O
than	O
(	O
or	O
in	O
some	O
publications	O
equal	O
to	O
)	O
times	O
in	O
that	O
given	O
range	O
.	O
</s>
<s>
Due	O
to	O
this	O
decomposability	O
,	O
some	O
data	B-General_Concept
structures	I-General_Concept
answer	O
-majority	O
queries	O
on	O
one-dimensional	O
arrays	O
by	O
finding	O
the	O
Lowest	O
common	O
ancestor	O
(	O
LCA	O
)	O
of	O
the	O
endpoints	O
of	O
the	O
query	O
range	O
in	O
a	O
Range	B-Data_Structure
tree	I-Data_Structure
and	O
validating	O
two	O
sets	O
of	O
candidates	O
(	O
of	O
size	O
)	O
from	O
each	O
endpoint	O
to	O
the	O
lowest	O
common	O
ancestor	O
in	O
constant	O
time	O
resulting	O
in	O
query	O
time	O
.	O
</s>
<s>
proposed	O
a	O
data	B-General_Concept
structure	I-General_Concept
that	O
supports	O
range	O
-majority	O
queries	O
on	O
an	O
array	B-Data_Structure
.	O
</s>
<s>
For	O
each	O
query	O
in	O
this	O
data	B-General_Concept
structure	I-General_Concept
a	O
threshold	O
and	O
a	O
rectangular	O
range	O
are	O
specified	O
,	O
and	O
the	O
set	O
of	O
all	O
elements	O
that	O
have	O
relative	O
frequencies	O
(	O
inside	O
that	O
rectangular	O
range	O
)	O
greater	O
than	O
or	O
equal	O
to	O
are	O
returned	O
as	O
the	O
output	O
.	O
</s>
<s>
This	O
data	B-General_Concept
structure	I-General_Concept
supports	O
dynamic	O
thresholds	O
(	O
specified	O
at	O
query	O
time	O
)	O
and	O
a	O
preprocessing	O
threshold	O
based	O
on	O
which	O
it	O
is	O
constructed	O
.	O
</s>
<s>
During	O
the	O
preprocessing	O
,	O
a	O
set	O
of	O
vertical	O
and	O
horizontal	O
intervals	O
are	O
built	O
on	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
Many	O
other	O
data	B-General_Concept
structures	I-General_Concept
(	O
as	O
discussed	O
below	O
)	O
have	O
proposed	O
methods	O
for	O
verifying	O
each	O
candidate	O
in	O
constant	O
time	O
and	O
thus	O
maintaining	O
the	O
query	O
time	O
while	O
returning	O
no	O
false	O
positives	O
.	O
</s>
<s>
The	O
cases	O
in	O
which	O
the	O
query	O
block	O
is	O
smaller	O
than	O
are	O
handled	O
by	O
storing	O
different	O
instances	O
of	O
this	O
data	B-General_Concept
structure	I-General_Concept
of	O
the	O
following	O
form	O
:	O
</s>
<s>
As	O
mentioned	O
above	O
,	O
this	O
data	B-General_Concept
structure	I-General_Concept
has	O
query	O
time	O
and	O
requires	O
bits	O
of	O
space	O
by	O
storing	O
a	O
Huffman-encoded	O
copy	O
of	O
it	O
(	O
note	O
the	O
factor	O
and	O
also	O
see	O
Huffman	B-General_Concept
coding	I-General_Concept
)	O
.	O
</s>
<s>
proposed	O
a	O
data	B-General_Concept
structure	I-General_Concept
that	O
given	O
a	O
one-dimensional	O
array	B-Data_Structure
,	O
a	O
subrange	O
of	O
(	O
specified	O
at	O
query	O
time	O
)	O
and	O
a	O
threshold	O
(	O
specified	O
at	O
query	O
time	O
)	O
,	O
is	O
able	O
to	O
return	O
the	O
list	O
of	O
all	O
-majorities	O
in	O
time	O
requiring	O
words	O
of	O
space	O
.	O
</s>
<s>
begin	O
by	O
noting	O
that	O
there	O
exists	O
a	O
data	B-General_Concept
structure	I-General_Concept
capable	O
of	O
returning	O
the	O
top-k	O
most	O
frequent	O
items	O
in	O
a	O
range	O
in	O
time	O
requiring	O
words	O
of	O
space	O
.	O
</s>
<s>
For	O
a	O
one-dimensional	O
array	B-Data_Structure
,	O
let	O
a	O
one-sided	O
top-k	O
range	B-Data_Structure
query	I-Data_Structure
to	O
be	O
of	O
form	O
.	O
</s>
<s>
first	O
construct	O
a	O
range	B-Data_Structure
tree	I-Data_Structure
in	O
which	O
each	O
branching	O
node	O
stores	O
one	O
copy	O
of	O
the	O
data	B-General_Concept
structure	I-General_Concept
described	O
above	O
for	O
one-sided	O
range	O
top-k	O
queries	O
and	O
each	O
leaf	O
represents	O
an	O
element	O
from	O
.	O
</s>
<s>
The	O
top-k	O
data	B-General_Concept
structure	I-General_Concept
at	O
each	O
node	O
is	O
constructed	O
based	O
on	O
the	O
values	O
existing	O
in	O
the	O
subtrees	B-Application
of	O
that	O
node	O
and	O
is	O
meant	O
to	O
answer	O
one-sided	O
range	O
top-k	O
queries	O
.	O
</s>
<s>
Please	O
note	O
that	O
for	O
a	O
one-dimensional	O
array	B-Data_Structure
,	O
a	O
range	B-Data_Structure
tree	I-Data_Structure
can	O
be	O
constructed	O
by	O
dividing	O
into	O
two	O
halves	O
and	O
recursing	O
on	O
both	O
halves	O
;	O
therefore	O
,	O
each	O
node	O
of	O
the	O
resulting	O
range	B-Data_Structure
tree	I-Data_Structure
represents	O
a	O
range	O
.	O
</s>
<s>
It	O
can	O
also	O
be	O
seen	O
that	O
this	O
range	B-Data_Structure
tree	I-Data_Structure
requires	O
words	O
of	O
space	O
,	O
because	O
there	O
are	O
levels	O
and	O
each	O
level	O
has	O
nodes	O
.	O
</s>
<s>
Moreover	O
,	O
since	O
at	O
each	O
level	O
of	O
a	O
range	B-Data_Structure
tree	I-Data_Structure
all	O
nodes	O
have	O
a	O
total	O
of	O
elements	O
of	O
at	O
their	O
subtrees	B-Application
and	O
since	O
there	O
are	O
levels	O
,	O
the	O
space	O
complexity	O
of	O
this	O
range	B-Data_Structure
tree	I-Data_Structure
is	O
.	O
</s>
<s>
First	O
,	O
the	O
lowest	O
common	O
ancestor	O
(	O
LCA	O
)	O
of	O
leaf	B-Application
nodes	I-Application
and	O
is	O
found	O
in	O
constant	O
time	O
.	O
</s>
<s>
Note	O
that	O
there	O
exists	O
a	O
data	B-General_Concept
structure	I-General_Concept
requiring	O
bits	O
of	O
space	O
that	O
is	O
capable	O
of	O
answering	O
the	O
LCA	O
queries	O
in	O
time	O
.	O
</s>
<s>
Let	O
denote	O
the	O
LCA	O
of	O
and	O
,	O
using	O
and	O
according	O
to	O
the	O
decomposability	O
of	O
range	O
-majority	O
queries	O
(	O
as	O
described	O
above	O
and	O
in	O
)	O
,	O
the	O
two-sided	O
range	B-Data_Structure
query	I-Data_Structure
can	O
be	O
converted	O
into	O
two	O
one-sided	O
range	O
top-k	O
queries	O
(	O
from	O
to	O
and	O
)	O
.	O
</s>
<s>
Each	O
candidate	O
is	O
then	O
assessed	O
in	O
constant	O
time	O
using	O
a	O
linear-space	O
data	B-General_Concept
structure	I-General_Concept
(	O
as	O
described	O
in	O
Lemma	O
3	O
in	O
)	O
that	O
is	O
able	O
to	O
determine	O
in	O
time	O
whether	O
or	O
not	O
a	O
given	O
subrange	O
of	O
an	O
array	B-Data_Structure
contains	O
at	O
least	O
instances	O
of	O
a	O
particular	O
element	O
.	O
</s>
<s>
proposed	O
a	O
data	B-General_Concept
structure	I-General_Concept
which	O
supports	O
queries	O
such	O
that	O
,	O
given	O
two	O
nodes	O
and	O
in	O
a	O
tree	O
,	O
are	O
able	O
to	O
report	O
the	O
list	O
of	O
elements	O
that	O
have	O
a	O
greater	O
relative	O
frequency	O
than	O
on	O
the	O
path	O
from	O
to	O
.	O
</s>
<s>
To	O
construct	O
this	O
data	B-General_Concept
structure	I-General_Concept
,	O
first	O
nodes	O
are	O
marked	O
.	O
</s>
<s>
It	O
is	O
easy	O
to	O
see	O
that	O
this	O
data	B-General_Concept
structure	I-General_Concept
require	O
words	O
of	O
space	O
,	O
because	O
as	O
mentioned	O
above	O
in	O
the	O
construction	O
phase	O
nodes	O
are	O
marked	O
and	O
for	O
each	O
marked	O
node	O
some	O
candidate	O
sets	O
are	O
stored	O
.	O
</s>
<s>
Therefore	O
,	O
this	O
data	B-General_Concept
structure	I-General_Concept
requires	O
words	O
of	O
space	O
.	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
range	B-Data_Structure
queries	I-Data_Structure
might	O
be	O
extended	O
to	O
other	O
data	B-General_Concept
structures	I-General_Concept
like	O
trees	B-Application
,	O
such	O
as	O
the	O
level	O
ancestor	O
problem	O
.	O
</s>
<s>
A	O
similar	O
family	O
of	O
problems	O
are	O
orthogonal	B-Data_Structure
range	I-Data_Structure
queries	O
,	O
also	O
known	O
as	O
counting	O
queries	O
.	O
</s>
