<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
the	O
treap	B-Application
and	O
the	O
randomized	B-Application
binary	I-Application
search	I-Application
tree	I-Application
are	O
two	O
closely	O
related	O
forms	O
of	O
binary	B-Language
search	I-Language
tree	I-Language
data	B-General_Concept
structures	I-General_Concept
that	O
maintain	O
a	O
dynamic	O
set	O
of	O
ordered	O
keys	O
and	O
allow	O
binary	O
searches	O
among	O
the	O
keys	O
.	O
</s>
<s>
After	O
any	O
sequence	O
of	O
insertions	O
and	O
deletions	O
of	O
keys	O
,	O
the	O
shape	O
of	O
the	O
tree	B-Application
is	O
a	O
random	O
variable	O
with	O
the	O
same	O
probability	O
distribution	O
as	O
a	O
random	O
binary	O
tree	B-Application
;	O
in	O
particular	O
,	O
with	O
high	O
probability	O
its	O
height	O
is	O
proportional	O
to	O
the	O
logarithm	O
of	O
the	O
number	O
of	O
keys	O
,	O
so	O
that	O
each	O
search	O
,	O
insertion	O
,	O
or	O
deletion	O
operation	O
takes	O
logarithmic	O
time	O
to	O
perform	O
.	O
</s>
<s>
The	O
treap	B-Application
was	O
first	O
described	O
by	O
Raimund	O
Seidel	O
and	O
Cecilia	O
R	O
.	O
Aragon	O
in	O
1989	O
;	O
its	O
name	O
is	O
a	O
portmanteau	O
of	O
tree	B-Application
and	O
heap	B-Application
.	O
</s>
<s>
It	O
is	O
a	O
Cartesian	B-Algorithm
tree	I-Algorithm
in	O
which	O
each	O
key	O
is	O
given	O
a	O
(	O
randomly	O
chosen	O
)	O
numeric	O
priority	O
.	O
</s>
<s>
As	O
with	O
any	O
binary	B-Language
search	I-Language
tree	I-Language
,	O
the	O
inorder	O
traversal	O
order	O
of	O
the	O
nodes	O
is	O
the	O
same	O
as	O
the	O
sorted	O
order	O
of	O
the	O
keys	O
.	O
</s>
<s>
The	O
structure	O
of	O
the	O
tree	B-Application
is	O
determined	O
by	O
the	O
requirement	O
that	O
it	O
be	O
heap-ordered	O
:	O
that	O
is	O
,	O
the	O
priority	O
number	O
for	O
any	O
non-leaf	O
node	O
must	O
be	O
greater	O
than	O
or	O
equal	O
to	O
the	O
priority	O
of	O
its	O
children	O
.	O
</s>
<s>
Thus	O
,	O
as	O
with	O
Cartesian	B-Algorithm
trees	I-Algorithm
more	O
generally	O
,	O
the	O
root	B-Application
node	I-Application
is	O
the	O
maximum-priority	O
node	O
,	O
and	O
its	O
left	O
and	O
right	O
subtrees	B-Application
are	O
formed	O
in	O
the	O
same	O
manner	O
from	O
the	O
subsequences	O
of	O
the	O
sorted	O
order	O
to	O
the	O
left	O
and	O
right	O
of	O
that	O
node	O
.	O
</s>
<s>
An	O
equivalent	O
way	O
of	O
describing	O
the	O
treap	B-Application
is	O
that	O
it	O
could	O
be	O
formed	O
by	O
inserting	O
the	O
nodes	O
highest	O
priority-first	O
into	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
without	O
doing	O
any	O
rebalancing	O
.	O
</s>
<s>
Therefore	O
,	O
if	O
the	O
priorities	O
are	O
independent	O
random	O
numbers	O
(	O
from	O
a	O
distribution	O
over	O
a	O
large	O
enough	O
space	O
of	O
possible	O
priorities	O
to	O
ensure	O
that	O
two	O
nodes	O
are	O
very	O
unlikely	O
to	O
have	O
the	O
same	O
priority	O
)	O
then	O
the	O
shape	O
of	O
a	O
treap	B-Application
has	O
the	O
same	O
probability	O
distribution	O
as	O
the	O
shape	O
of	O
a	O
random	O
binary	B-Language
search	I-Language
tree	I-Language
,	O
a	O
search	O
tree	B-Application
formed	O
by	O
inserting	O
the	O
nodes	O
without	O
rebalancing	O
in	O
a	O
randomly	O
chosen	O
insertion	O
order	O
.	O
</s>
<s>
Because	O
random	O
binary	B-Language
search	I-Language
trees	I-Language
are	O
known	O
to	O
have	O
logarithmic	O
height	O
with	O
high	O
probability	O
,	O
the	O
same	O
is	O
true	O
for	O
treaps	B-Application
.	O
</s>
<s>
This	O
mirrors	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
argument	O
that	O
quicksort	B-Algorithm
runs	O
in	O
expected	O
time	O
.	O
</s>
<s>
If	O
binary	B-Language
search	I-Language
trees	I-Language
are	O
solutions	O
to	O
the	O
dynamic	B-General_Concept
problem	I-General_Concept
version	O
of	O
sorting	O
,	O
then	O
Treaps	B-Application
correspond	O
specifically	O
to	O
dynamic	O
quicksort	B-Algorithm
where	O
priorities	O
guide	O
pivot	O
choices	O
.	O
</s>
<s>
This	O
modification	O
would	O
cause	O
the	O
tree	B-Application
to	O
lose	O
its	O
random	O
shape	O
;	O
instead	O
,	O
frequently	O
accessed	O
nodes	O
would	O
be	O
more	O
likely	O
to	O
be	O
near	O
the	O
root	O
of	O
the	O
tree	B-Application
,	O
causing	O
searches	O
for	O
them	O
to	O
be	O
faster	O
.	O
</s>
<s>
Naor	O
and	O
Nissim	O
describe	O
an	O
application	O
in	O
maintaining	O
authorization	O
certificates	O
in	O
public-key	B-Application
cryptosystems	I-Application
.	O
</s>
<s>
Treaps	B-Application
support	O
the	O
following	O
basic	O
operations	O
:	O
</s>
<s>
To	O
search	O
for	O
a	O
given	O
key	O
value	O
,	O
apply	O
a	O
standard	O
binary	O
search	O
algorithm	O
in	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
,	O
ignoring	O
the	O
priorities	O
.	O
</s>
<s>
To	O
insert	O
a	O
new	O
key	O
x	O
into	O
the	O
treap	B-Application
,	O
generate	O
a	O
random	O
priority	O
y	O
for	O
x	O
.	O
Binary	O
search	O
for	O
x	O
in	O
the	O
tree	B-Application
,	O
and	O
create	O
a	O
new	O
node	O
at	O
the	O
leaf	O
position	O
where	O
the	O
binary	O
search	O
determines	O
a	O
node	O
for	O
x	O
should	O
exist	O
.	O
</s>
<s>
Then	O
,	O
as	O
long	O
as	O
x	O
is	O
not	O
the	O
root	O
of	O
the	O
tree	B-Application
and	O
has	O
a	O
larger	O
priority	O
number	O
than	O
its	O
parent	O
z	O
,	O
perform	O
a	O
tree	B-Data_Structure
rotation	I-Data_Structure
that	O
reverses	O
the	O
parent-child	O
relation	O
between	O
x	O
and	O
z	O
.	O
</s>
<s>
To	O
delete	O
a	O
node	O
x	O
from	O
the	O
treap	B-Application
,	O
if	O
x	O
is	O
a	O
leaf	O
of	O
the	O
tree	B-Application
,	O
simply	O
remove	O
it	O
.	O
</s>
<s>
If	O
x	O
has	O
a	O
single	O
child	O
z	O
,	O
remove	O
x	O
from	O
the	O
tree	B-Application
and	O
make	O
z	O
be	O
the	O
child	O
of	O
the	O
parent	O
of	O
x	O
(	O
or	O
make	O
z	O
the	O
root	O
of	O
the	O
tree	B-Application
if	O
x	O
had	O
no	O
parent	O
)	O
.	O
</s>
<s>
Finally	O
,	O
if	O
x	O
has	O
two	O
children	O
,	O
swap	O
its	O
position	O
in	O
the	O
tree	B-Application
with	O
the	O
position	O
of	O
its	O
immediate	O
successor	O
z	O
in	O
the	O
sorted	O
order	O
,	O
resulting	O
in	O
one	O
of	O
the	O
previous	O
cases	O
.	O
</s>
<s>
In	O
this	O
final	O
case	O
,	O
the	O
swap	O
may	O
violate	O
the	O
heap-ordering	O
property	O
for	O
z	O
,	O
so	O
additional	O
rotations	O
may	O
need	O
to	O
be	O
performed	O
to	O
restore	O
this	O
property	O
.	O
</s>
<s>
To	O
build	O
a	O
treap	B-Application
we	O
can	O
simply	O
insert	O
n	O
values	O
in	O
the	O
treap	B-Application
where	O
each	O
takes	O
time	O
.	O
</s>
<s>
Therefore	O
a	O
treap	B-Application
can	O
be	O
built	O
in	O
time	O
from	O
a	O
list	O
values	O
.	O
</s>
<s>
In	O
addition	O
to	O
the	O
single-element	O
insert	O
,	O
delete	O
and	O
lookup	O
operations	O
,	O
several	O
fast	O
"	O
bulk	O
"	O
operations	O
have	O
been	O
defined	O
on	O
treaps	B-Application
:	O
union	O
,	O
intersection	O
and	O
set	O
difference	O
.	O
</s>
<s>
To	O
split	O
a	O
treap	B-Application
into	O
two	O
smaller	O
treaps	B-Application
,	O
those	O
smaller	O
than	O
key	O
x	O
,	O
and	O
those	O
larger	O
than	O
key	O
x	O
,	O
insert	O
x	O
into	O
the	O
treap	B-Application
with	O
maximum	O
priority	O
—	O
larger	O
than	O
the	O
priority	O
of	O
any	O
node	O
in	O
the	O
treap	B-Application
.	O
</s>
<s>
After	O
this	O
insertion	O
,	O
x	O
will	O
be	O
the	O
root	B-Application
node	I-Application
of	O
the	O
treap	B-Application
,	O
all	O
values	O
less	O
than	O
x	O
will	O
be	O
found	O
in	O
the	O
left	O
subtreap	O
,	O
and	O
all	O
values	O
greater	O
than	O
x	O
will	O
be	O
found	O
in	O
the	O
right	O
subtreap	O
.	O
</s>
<s>
This	O
costs	O
as	O
much	O
as	O
a	O
single	O
insertion	O
into	O
the	O
treap	B-Application
.	O
</s>
<s>
Joining	O
two	O
treaps	B-Application
that	O
are	O
the	O
product	O
of	O
a	O
former	O
split	O
,	O
one	O
can	O
safely	O
assume	O
that	O
the	O
greatest	O
value	O
in	O
the	O
first	O
treap	B-Application
is	O
less	O
than	O
the	O
smallest	O
value	O
in	O
the	O
second	O
treap	B-Application
.	O
</s>
<s>
Create	O
a	O
new	O
node	O
with	O
value	O
x	O
,	O
such	O
that	O
x	O
is	O
larger	O
than	O
this	O
max-value	O
in	O
the	O
first	O
treap	B-Application
and	O
smaller	O
than	O
the	O
min-value	O
in	O
the	O
second	O
treap	B-Application
,	O
assign	O
it	O
the	O
minimum	O
priority	O
,	O
then	O
set	O
its	O
left	O
child	O
to	O
the	O
first	O
heap	B-Application
and	O
its	O
right	O
child	O
to	O
the	O
second	O
heap	B-Application
.	O
</s>
<s>
Rotate	O
as	O
necessary	O
to	O
fix	O
the	O
heap	B-Application
order	O
.	O
</s>
<s>
The	O
result	O
is	O
one	O
treap	B-Application
merged	O
from	O
the	O
two	O
original	O
treaps	B-Application
.	O
</s>
<s>
More	O
generally	O
,	O
the	O
join	O
operation	O
can	O
work	O
on	O
two	O
treaps	B-Application
and	O
a	O
key	O
with	O
arbitrary	O
priority	O
(	O
i.e.	O
,	O
not	O
necessary	O
to	O
be	O
the	O
highest	O
)	O
.	O
</s>
<s>
The	O
union	O
of	O
two	O
treaps	B-Application
and	O
,	O
representing	O
sets	O
and	O
is	O
a	O
treap	B-Application
that	O
represents	O
.	O
</s>
<s>
(	O
The	O
algorithm	O
is	O
non-destructive	B-Application
,	O
but	O
an	O
in-place	O
destructive	O
version	O
exists	O
as	O
well	O
.	O
)	O
</s>
<s>
The	O
complexity	O
of	O
each	O
of	O
union	O
,	O
intersection	O
and	O
difference	O
is	O
for	O
treaps	B-Application
of	O
sizes	O
and	O
,	O
with	O
.	O
</s>
<s>
Moreover	O
,	O
since	O
the	O
recursive	O
calls	O
to	O
union	O
are	O
independent	O
of	O
each	O
other	O
,	O
they	O
can	O
be	O
executed	O
in	B-Operating_System
parallel	I-Operating_System
.	O
</s>
<s>
Split	O
and	O
Union	O
call	O
Join	O
but	O
do	O
not	O
deal	O
with	O
the	O
balancing	O
criteria	O
of	O
treaps	B-Application
directly	O
,	O
such	O
an	O
implementation	O
is	O
usually	O
called	O
the	O
"	O
join-based	O
"	O
implementation	O
.	O
</s>
<s>
Provided	O
that	O
there	O
can	O
only	O
be	O
one	O
simultaneous	O
root	B-Application
node	I-Application
representing	O
a	O
given	O
set	O
of	O
keys	O
,	O
two	O
sets	O
can	O
be	O
tested	O
for	O
equality	O
by	O
pointer	O
comparison	O
,	O
which	O
is	O
constant	O
in	O
time	O
.	O
</s>
<s>
The	O
randomized	B-Application
binary	I-Application
search	I-Application
tree	I-Application
,	O
introduced	O
by	O
Martínez	O
and	O
Roura	O
subsequently	O
to	O
the	O
work	O
of	O
Aragon	O
and	O
Seidel	O
on	O
treaps	B-Application
,	O
stores	O
the	O
same	O
nodes	O
with	O
the	O
same	O
random	O
distribution	O
of	O
tree	B-Application
shape	O
,	O
but	O
maintains	O
different	O
information	O
within	O
the	O
nodes	O
of	O
the	O
tree	B-Application
in	O
order	O
to	O
maintain	O
its	O
randomized	O
structure	O
.	O
</s>
<s>
Rather	O
than	O
storing	O
random	O
priorities	O
on	O
each	O
node	O
,	O
the	O
randomized	B-Application
binary	I-Application
search	I-Application
tree	I-Application
stores	O
a	O
small	O
integer	O
at	O
each	O
node	O
,	O
the	O
number	O
of	O
its	O
descendants	O
(	O
counting	O
itself	O
as	O
one	O
)	O
;	O
these	O
numbers	O
may	O
be	O
maintained	O
during	O
tree	B-Data_Structure
rotation	I-Data_Structure
operations	O
at	O
only	O
a	O
constant	O
additional	O
amount	O
of	O
time	O
per	O
rotation	O
.	O
</s>
<s>
When	O
a	O
key	O
x	O
is	O
to	O
be	O
inserted	O
into	O
a	O
tree	B-Application
that	O
already	O
has	O
n	O
nodes	O
,	O
the	O
insertion	O
algorithm	O
chooses	O
with	O
probability	O
1/	O
( n+1	O
)	O
to	O
place	O
x	O
as	O
the	O
new	O
root	O
of	O
the	O
tree	B-Application
,	O
and	O
otherwise	O
,	O
it	O
calls	O
the	O
insertion	O
procedure	O
recursively	O
to	O
insert	O
x	O
within	O
the	O
left	O
or	O
right	O
subtree	B-Application
(	O
depending	O
on	O
whether	O
its	O
key	O
is	O
less	O
than	O
or	O
greater	O
than	O
the	O
root	O
)	O
.	O
</s>
<s>
Placing	O
x	O
at	O
the	O
root	O
of	O
a	O
subtree	B-Application
may	O
be	O
performed	O
either	O
as	O
in	O
the	O
treap	B-Application
by	O
inserting	O
it	O
at	O
a	O
leaf	O
and	O
then	O
rotating	O
it	O
upwards	O
,	O
or	O
by	O
an	O
alternative	O
algorithm	O
described	O
by	O
Martínez	O
and	O
Roura	O
that	O
splits	O
the	O
subtree	B-Application
into	O
two	O
pieces	O
to	O
be	O
used	O
as	O
the	O
left	O
and	O
right	O
children	O
of	O
the	O
new	O
node	O
.	O
</s>
<s>
The	O
deletion	O
procedure	O
for	O
a	O
randomized	B-Application
binary	I-Application
search	I-Application
tree	I-Application
uses	O
the	O
same	O
information	O
per	O
node	O
as	O
the	O
insertion	O
procedure	O
,	O
but	O
unlike	O
the	O
insertion	O
procedure	O
,	O
it	O
only	O
needs	O
on	O
average	O
O(1 )	O
random	O
decisions	O
to	O
join	O
the	O
two	O
subtrees	B-Application
descending	O
from	O
the	O
left	O
and	O
right	O
children	O
of	O
the	O
deleted	O
node	O
into	O
a	O
single	O
tree	B-Application
.	O
</s>
<s>
That	O
is	O
because	O
the	O
subtrees	B-Application
to	O
be	O
joined	O
are	O
on	O
average	O
at	O
depth	O
Θ(log n )	O
;	O
joining	O
two	O
trees	O
of	O
size	O
n	O
and	O
m	O
needs	O
Θ( log( n+m	O
)	O
)	O
random	O
choices	O
on	O
average	O
.	O
</s>
<s>
If	O
the	O
left	O
or	O
right	O
subtree	B-Application
of	O
the	O
node	O
to	O
be	O
deleted	O
is	O
empty	O
,	O
the	O
join	O
operation	O
is	O
trivial	O
;	O
otherwise	O
,	O
the	O
left	O
or	O
right	O
child	O
of	O
the	O
deleted	O
node	O
is	O
selected	O
as	O
the	O
new	O
subtree	B-Application
root	O
with	O
probability	O
proportional	O
to	O
its	O
number	O
of	O
descendants	O
,	O
and	O
the	O
join	O
proceeds	O
recursively	O
.	O
</s>
<s>
The	O
information	O
stored	O
per	O
node	O
in	O
the	O
randomized	O
binary	O
tree	B-Application
is	O
simpler	O
than	O
in	O
a	O
treap	B-Application
(	O
a	O
small	O
integer	O
rather	O
than	O
a	O
high-precision	O
random	O
number	O
)	O
,	O
but	O
it	O
makes	O
a	O
greater	O
number	O
of	O
calls	O
to	O
the	O
random	O
number	O
generator	O
(O(logn )	O
calls	O
per	O
insertion	O
or	O
deletion	O
rather	O
than	O
one	O
call	O
per	O
insertion	O
)	O
and	O
the	O
insertion	O
procedure	O
is	O
slightly	O
more	O
complicated	O
due	O
to	O
the	O
need	O
to	O
update	O
the	O
numbers	O
of	O
descendants	O
per	O
node	O
.	O
</s>
<s>
A	O
minor	O
technical	O
difference	O
is	O
that	O
,	O
in	O
a	O
treap	B-Application
,	O
there	O
is	O
a	O
small	O
probability	O
of	O
a	O
collision	O
(	O
two	O
keys	O
getting	O
the	O
same	O
priority	O
)	O
,	O
and	O
in	O
both	O
cases	O
,	O
there	O
will	O
be	O
statistical	O
differences	O
between	O
a	O
true	O
random	O
number	O
generator	O
and	O
the	O
pseudo-random	B-Algorithm
number	I-Algorithm
generator	I-Algorithm
typically	O
used	O
on	O
digital	O
computers	O
.	O
</s>
<s>
Although	O
the	O
treap	B-Application
and	O
the	O
randomized	B-Application
binary	I-Application
search	I-Application
tree	I-Application
both	O
have	O
the	O
same	O
random	O
distribution	O
of	O
tree	B-Application
shapes	O
after	O
each	O
update	O
,	O
the	O
history	O
of	O
modifications	O
to	O
the	O
trees	O
performed	O
by	O
these	O
two	O
data	B-General_Concept
structures	I-General_Concept
over	O
a	O
sequence	O
of	O
insertion	O
and	O
deletion	O
operations	O
may	O
be	O
different	O
.	O
</s>
<s>
For	O
instance	O
,	O
in	O
a	O
treap	B-Application
,	O
if	O
the	O
three	O
numbers	O
1	O
,	O
2	O
,	O
and	O
3	O
are	O
inserted	O
in	O
the	O
order	O
1	O
,	O
3	O
,	O
2	O
,	O
and	O
then	O
the	O
number	O
2	O
is	O
deleted	O
,	O
the	O
remaining	O
two	O
nodes	O
will	O
have	O
the	O
same	O
parent-child	O
relationship	O
that	O
they	O
did	O
prior	O
to	O
the	O
insertion	O
of	O
the	O
middle	O
number	O
.	O
</s>
<s>
In	O
a	O
randomized	B-Application
binary	I-Application
search	I-Application
tree	I-Application
,	O
the	O
tree	B-Application
after	O
the	O
deletion	O
is	O
equally	O
likely	O
to	O
be	O
either	O
of	O
the	O
two	O
possible	O
trees	O
on	O
its	O
two	O
nodes	O
,	O
independently	O
of	O
what	O
the	O
tree	B-Application
looked	O
like	O
prior	O
to	O
the	O
insertion	O
of	O
the	O
middle	O
number	O
.	O
</s>
<s>
An	O
implicit	O
treap	B-Application
is	O
a	O
simple	O
variation	O
of	O
an	O
ordinary	O
treap	B-Application
which	O
can	O
be	O
viewed	O
as	O
a	O
dynamic	O
array	O
that	O
supports	O
the	O
following	O
operations	O
in	O
:	O
</s>
<s>
The	O
idea	O
behind	O
an	O
implicit	O
treap	B-Application
is	O
to	O
use	O
the	O
array	O
index	O
as	O
a	O
key	O
,	O
but	O
to	O
not	O
store	O
it	O
explicitly	O
.	O
</s>
<s>
Otherwise	O
,	O
an	O
update	O
(	O
insertion/deletion	O
)	O
would	O
result	O
in	O
changes	O
of	O
the	O
keys	O
in	O
nodes	O
of	O
the	O
tree	B-Application
.	O
</s>
<s>
Note	O
that	O
such	O
nodes	O
can	O
be	O
present	O
not	O
only	O
in	O
its	O
left	O
subtree	B-Application
but	O
also	O
in	O
left	O
subtrees	B-Application
of	O
its	O
ancestors	O
P	O
,	O
if	O
T	O
is	O
in	O
the	O
right	O
subtree	B-Application
of	O
P	O
.	O
</s>
<s>
Therefore	O
we	O
can	O
quickly	O
calculate	O
the	O
implicit	O
key	O
of	O
the	O
current	O
node	O
as	O
we	O
perform	O
an	O
operation	O
by	O
accumulating	O
the	O
sum	O
of	O
all	O
nodes	O
as	O
we	O
descend	O
the	O
tree	B-Application
.	O
</s>
<s>
Note	O
that	O
this	O
sum	O
does	O
not	O
change	O
when	O
we	O
visit	O
the	O
left	O
subtree	B-Application
but	O
it	O
will	O
increase	O
by	O
when	O
we	O
visit	O
the	O
right	O
subtree	B-Application
.	O
</s>
<s>
The	O
join	O
algorithm	O
for	O
an	O
implicit	O
treap	B-Application
is	O
as	O
follows	O
:	O
</s>
<s>
The	O
split	O
algorithm	O
for	O
an	O
implicit	O
treap	B-Application
is	O
as	O
follows	O
:	O
</s>
<s>
We	O
find	O
the	O
element	O
to	O
be	O
deleted	O
and	O
perform	O
a	O
join	O
on	O
its	O
children	O
L	O
and	O
R	O
.	O
We	O
then	O
replace	O
the	O
element	O
to	O
be	O
deleted	O
with	O
the	O
tree	B-Application
that	O
resulted	O
from	O
the	O
join	O
operation	O
.	O
</s>
<s>
We	O
will	O
call	O
this	O
target	O
function	O
at	O
the	O
end	O
of	O
all	O
functions	O
that	O
modify	O
the	O
tree	B-Application
,	O
i.e.	O
,	O
split	O
and	O
join	O
.	O
</s>
<s>
Second	O
we	O
need	O
to	O
process	O
a	O
query	O
for	O
a	O
given	O
range	O
A	O
..	O
B	O
We	O
will	O
call	O
the	O
split	O
function	O
twice	O
and	O
split	O
the	O
treap	B-Application
into	O
which	O
contains	O
,	O
which	O
contains	O
,	O
and	O
which	O
contains	O
.	O
</s>
<s>
After	O
the	O
query	O
is	O
answered	O
we	O
will	O
call	O
the	O
join	O
function	O
twice	O
to	O
restore	O
the	O
original	O
treap	B-Application
.	O
</s>
<s>
We	O
will	O
create	O
an	O
extra	O
field	O
D	O
which	O
will	O
contain	O
the	O
added	O
value	O
for	O
the	O
subtree	B-Application
.	O
</s>
<s>
We	O
will	O
call	O
this	O
function	O
at	O
the	O
beginning	O
of	O
all	O
functions	O
which	O
modify	O
the	O
tree	B-Application
,	O
i.e.	O
,	O
split	O
and	O
join	O
so	O
that	O
after	O
any	O
changes	O
made	O
to	O
the	O
tree	B-Application
the	O
information	O
will	O
not	O
be	O
lost	O
.	O
</s>
<s>
To	O
show	O
that	O
the	O
subtree	B-Application
of	O
a	O
given	O
node	O
needs	O
to	O
be	O
reversed	O
for	O
each	O
node	O
we	O
will	O
create	O
an	O
extra	O
boolean	O
field	O
R	O
and	O
set	O
its	O
value	O
to	O
true	O
.	O
</s>
