Part Two
31
41
59
26
53
58
97
93
Some Notation
Blocking Revisited
31
31
41
59
26
26
53
58
97
23
93
23
84
62
62
64
33
27
83
27
Blocking Revisited
This
Thisisisjust
justRMQ
RMQon
on
the
theblock
blockminimums!
minimums!
31
31
41
59
26
26
53
58
97
23
93
23
84
62
62
64
33
27
83
27
Blocking Revisited
31
31
41
59
26
26
53
58
97
23
93
23
84
This
Thisisisjust
justRMQ
RMQ
inside
insidethe
theblocks!
blocks!
62
62
64
33
27
83
27
The Framework
31
31
41
59
26
26
53
58
97
23
93
23
84
62
62
64
33
27
83
27
A Useful Observation
An Observation
A Key Difference
An Observation
10
30
20
40
166
361
261
464
An Observation
10
30
20
40
166
361
261
464
An Observation
10
30
20
40
166
361
261
464
An Observation
10
30
20
40
166
361
261
464
An Observation
10
30
20
40
166
361
261
464
An Observation
10
30
20
40
166
361
261
464
An Observation
10
30
20
40
166
361
261
464
An Observation
10
30
20
40
166
361
261
464
An Observation
10
30
20
40
166
361
261
464
An Observation
10
30
20
40
166
361
261
464
Claim:
Claim:The
Theindices
indicesof
ofthe
the
answers
answersto
toany
anyrange
range
minimum
minimumqueries
querieson
onthese
these
two
twoarrays
arraysare
arethe
thesame.
same.
Modifying RMQ
30
20
40
166
361
261
464
Some Notation
An Observation
31
41
59
26
53
58
97
23
93
84
62
64
33
83
27
An Observation
31
41
59
26
53
58
97
23
93
84
62
64
33
83
27
An Observation
31
41
59
26
53
58
97
23
93
84
62
64
33
83
27
An Observation
RMQ
Structure
31
41
59
26
53
58
97
23
93
84
62
64
33
83
27
An Observation
RMQ
Structure
31
41
59
26
53
58
97
23
93
84
62
64
33
83
27
An Observation
RMQ
Structure
31
41
59
26
RMQ
Structure
53
58
97
23
93
84
62
64
33
83
27
An Observation
RMQ
Structure
31
41
59
26
RMQ
Structure
53
58
97
23
93
84
RMQ
Structure
62
64
33
83
27
An Initial Idea
41
59
16
18
27
18
28
66
73
84
12
66
26
60
22
14
72
99
27
An Initial Idea
41
2
59
3
16
2
18
3
3
1
27
2
18
1
28
3
66
1
73
2
84
3
12
3
2
1
5
2
66
3
26
2
6
1
60
3
22
1
14
2
72
2
99
3
27
1
An Initial Idea
41
2
59
3
16
2
18
3
3
1
27
2
18
1
28
3
66
1
73
2
84
3
12
3
2
1
5
2
66
3
26
2
6
1
60
3
22
1
14
2
72
2
99
3
27
1
Some Problems
Some Problems
261 268 161 167 166 167 261 161 268 166 166 268 161 261 167
4
5
1
3
2
3
4
1
5
2
2
5
1
4
3
Some Problems
261 268 161 167 166 167 261 161 268 166 166 268 161 261 167
4
5
1
3
2
3
4
1
5
2
2
5
1
4
3
Some Problems
261 268 161 167 166 167 261 161 268 166 166 268 161 261 167
4
5
1
3
2
3
4
1
5
2
2
5
1
4
3
An Observation
An Observation
An Observation
75
35
80
85
83
14
22
11
43
35
An Observation
75
35
80
85
83
14
22
11
43
35
An Observation
75
35
80
85
83
14
22
11
43
35
An Observation
75
35
80
85
83
14
22
11
43
35
An Observation
14
22
11
43
35
An Observation
14
22
11
43
35
An Observation
14
22
11
43
35
An Observation
14
22
11
43
35
An Observation
14
22
11
43
35
An Observation
14
22
11
43
35
An Observation
22
11
43
35
An Observation
22
11
43
35
An Observation
22
11
43
35
Cartesian Trees
0
3
14
55
22
43
11
31
41
59
26
53
58
97
23
93
84
33
64
62
83
27
7
3
0
14
4
10
5
1
2
31
41
59
9
6
26
53
58
97
12
8
23
93
11
84
33
64
13
62
83
27
7
3
0
14
4
10
5
1
2
31
41
59
9
6
26
53
58
97
12
8
23
93
11
84
33
64
13
62
83
27
7
3
0
14
4
10
5
1
2
31
41
59
9
6
26
53
58
97
12
8
23
93
11
84
33
64
13
62
83
27
7
3
0
14
4
10
5
1
2
31
41
59
9
6
26
53
58
97
12
8
23
93
11
84
33
64
13
62
83
27
7
3
0
14
4
10
5
1
2
31
41
59
9
6
26
53
58
97
12
8
23
93
11
84
33
64
13
62
83
27
7
3
0
14
4
10
5
1
2
31
41
59
9
6
26
53
58
97
12
8
23
93
11
84
33
64
13
62
83
27
7
3
0
14
4
10
5
1
2
31
41
59
9
6
26
53
58
97
12
8
23
93
11
84
33
64
13
62
83
27
Office Hours
Your Questions
A
B
C
G
F
CS166
DataStructures
A B C B A D E D F D A G A
D
E
G
F
A B C B A D E D F D A G A
D
E
G
F
A B C B A D E D F D A G A
This
Thisisiscalled
calledan
anEuler
Euler
tour
of
the
tree.
We'll
tour of the tree. We'lltalk
talk
about
aboutthis
thismore
morein
inaafew
few
weeks.
weeks.
G
F
A B C B A D E D F D A G A
0
G
F
A B C B A D E D F D A G A
0
Back to CS166!
A Better Approach
A Better Approach
93
84
33
64
62
83
63
A Better Approach
0
93
3
84
33
64
5
62
83
63
A Better Approach
0
93
3
84
33
64
5
62
83
63
A Better Approach
0
93
3
84
33
64
62
83
63
A Better Approach
0
93
3
84
33
64
62
83
63
Observation
Observation1:
1:This
Thisnew
new
node
nodecannot
cannotend
endup
upas
asthe
the
left
leftchild
childof
ofany
anynode
nodein
inthe
the
tree.
tree.
A Better Approach
0
93
3
84
Observation
Observation2:
2:This
Thisnew
new
node
nodewill
willend
endup
upon
onthe
theright
right
spine
spineof
ofthe
thetree.
tree.
33
64
62
83
63
A Better Approach
0
93
3
84
33
64
62
83
63
Observation
Observation3:
3:Cartesian
Cartesian
trees
treesare
aremin-heaps
min-heapswith
with
respect
respectto
tothe
theelements
elementsin
in
the
theoriginal
originalarray.
array.
A Better Approach
0
93
3
84
33
64
62
83
63
A Better Approach
5
6
93
84
33
64
62
83
63
A Better Approach
5
6
93
84
33
64
62
83
63
A Better Approach
5
6
93
84
33
64
62
83
63
A Better Approach
93
84
33
64
62
83
63
A Better Approach
3
5
93
84
33
64
62
83
63
A Better Approach
3
5
93
84
33
64
62
83
63
A Better Approach
3
5
93
84
33
64
62
83
63
55
A Better Approach
3
5
93
84
33
64
62
83
63
55
A Better Approach
3
5
93
84
33
64
62
83
7
63
55
A Better Approach
3
5
93
84
33
64
62
83
63
55
A Better Approach
1
4
3
5
93
84
33
64
62
83
63
55
A Better Approach
1
4
3
5
93
84
33
64
62
83
63
55
A Stack-Based Algorithm
32
45
16
18
33
A Stack-Based Algorithm
32
45
16
18
33
A Stack-Based Algorithm
32
32
45
16
18
33
A Stack-Based Algorithm
32
32
45
16
18
33
A Stack-Based Algorithm
32
32
32
45
16
18
33
A Stack-Based Algorithm
32
32
32
45
16
18
33
A Stack-Based Algorithm
32
32
45
32
45
16
18
33
A Stack-Based Algorithm
32
32
45
32
45
16
18
33
A Stack-Based Algorithm
32
45
32
45
32
45
16
18
33
A Stack-Based Algorithm
32
45
32
45
32
45
16
18
33
A Stack-Based Algorithm
32
45
32
45
16
32
45
16
18
33
A Stack-Based Algorithm
32
32
45
16
32
45
16
18
33
A Stack-Based Algorithm
32
45
16
32
45
16
18
33
A Stack-Based Algorithm
16
32
45
32
45
16
18
33
A Stack-Based Algorithm
16
16
32
45
32
45
16
18
33
A Stack-Based Algorithm
16
16
32
45
32
45
16
18
33
A Stack-Based Algorithm
16
16
32
45
18
32
45
16
18
33
A Stack-Based Algorithm
16
16
32
18
45
32
45
16
18
33
A Stack-Based Algorithm
16
18
16
32
18
45
32
45
16
18
33
A Stack-Based Algorithm
16
18
16
32
18
45
32
45
16
18
33
A Stack-Based Algorithm
16
18
16
32
18
45
9
32
45
16
18
33
A Stack-Based Algorithm
16
16
32
18
45
9
32
45
16
18
33
A Stack-Based Algorithm
16
32
18
45
9
32
45
16
18
33
A Stack-Based Algorithm
16
32
18
45
32
45
16
18
33
A Stack-Based Algorithm
16
32
18
45
32
45
16
18
33
A Stack-Based Algorithm
16
32
18
45
32
45
16
18
33
A Stack-Based Algorithm
16
32
18
45
33
32
45
16
18
33
A Stack-Based Algorithm
16
32
33
18
45
32
45
16
18
33
A Stack-Based Algorithm
33
16
32
33
18
45
32
45
16
18
33
A Stack-Based Algorithm
33
16
32
33
18
45
32
45
16
18
33
which is roughly
( )
which is roughly
( )
which is roughly
4b
b3/2
Proof Approach
The Insight
32
45
16
18
33
32
45
16
18
33
32
32
45
16
18
33
32
32
45
16
18
33
32
32
45
16
18
33
32
32
45
16
18
33
32
45
32
45
16
18
33
32
45
32
45
16
18
33
45
32
45
32
45
16
18
33
1 1
45
32
45
32
45
16
18
33
1 1
45
32
45
16
32
45
16
18
33
1 1
32
45
16
32
45
16
18
33
1 1 0
32
45
16
32
45
16
18
33
1 1 0 0
16
32
45
32
45
16
18
33
1 1 0 0
16
32
45
32
45
16
18
33
1 1 0 0 1
16
32
45
32
45
16
18
33
1 1 0 0 1
16
32
45
18
32
45
16
18
33
1 1 0 0 1
16
32
18
45
32
45
16
18
33
1 1 0 0 1
18
16
32
18
45
32
45
16
18
33
1 1 0 0 1 1
18
16
32
18
45
32
45
16
18
33
1 1 0 0 1 1
18
16
32
18
45
9
32
45
16
18
33
1 1 0 0 1 1
16
32
18
45
9
32
45
16
18
33
1 1 0 0 1 1 0
16
32
18
45
9
32
45
16
18
33
1 1 0 0 1 1 0 0
9
16
32
18
45
32
45
16
18
33
1 1 0 0 1 1 0 0
18
45
32
45
16
18
33
1 1 0 0 1 1 0 0 1
18
45
32
45
16
18
33
1 1 0 0 1 1 0 0 1
18
45
33
32
45
16
18
33
1 1 0 0 1 1 0 0 1
33
18
45
32
45
16
18
33
1 1 0 0 1 1 0 0 1
33
9
16
32
33
18
45
32
45
16
18
33
1 1 0 0 1 1 0 0 1 1
33
9
16
32
33
18
45
32
45
16
18
33
1 1 0 0 1 1 0 0 1 1
33
18
45
32
45
16
18
33
1 1 0 0 1 1 0 0 1 1 0
9
16
32
33
18
45
32
45
16
18
33
1 1 0 0 1 1 0 0 1 1 0 0
9
16
32
33
18
45
32
45
16
18
33
1 1 0 0 1 1 0 0 1 1 0 0
18
28
18
28
45
90
45
23
53
60
28
74
71
35
18
28
18
28
45
90
45
23
53
60
28
74
71
35
18
28
27
18
28
45
90
45
23
53
60
28
74
71
35
18
28
27
18
28
45
90
45
23
53
60
28
74
71
35
1 0
18
28
18
28
45
90
45
23
53
60
28
74
71
35
18
28
18
1 0 1
18
28
45
90
45
23
53
60
28
74
71
35
18
28
18
1 0 1
18
28
45
90
45
23
53
60
28
74
71
35
18
28
18
1 0 1 1
18
28
28
45
90
45
23
53
60
28
74
71
35
18
28
18
1 0 1 1
18
28
28
45
90
45
23
53
60
28
74
71
35
18
28
18
1 0 1 1 0
18
28
45
90
45
23
53
60
28
74
71
35
18
28
18
1 0 1 1 0 1
18
18
28
45
90
45
23
53
60
28
74
71
35
18
28
18
1 0 1 1 0 1
18
18
28
45
90
45
23
53
60
28
74
71
35
18
28
18
18
1 0 1 1 0 1 1
18
28
45
28
90
45
23
53
60
28
74
71
35
18
28
18
18
1 0 1 1 0 1 1
18
28
45
28
90
45
23
53
60
28
74
71
35
18
28
18
18
18
1 0 1 1 0 1 1 1
28
45
28
90
45
45
23
53
60
28
74
71
35
18
28
18
18
18
1 0 1 1 0 1 1 1
28
45
28
90
45
45
23
53
60
28
74
71
35
18
28
18
18
28
18
1 0 1 1 0 1 1 1 1
45
28
90
45
45
23
90
53
60
28
74
71
35
18
28
18
18
28
18
1 0 1 1 0 1 1 1 1
45
28
90
45
45
23
90
53
60
28
74
71
35
18
28
18
18
28
18
1 0 1 1 0 1 1 1 1 0
45
28
90
45
45
23
53
60
28
74
71
35
18
28
18
18
18
28
45
28
1 0 1 1 0 1 1 1 1 0 1
90
45
45
23
45
53
60
28
74
71
35
18
28
18
18
18
28
45
28
1 0 1 1 0 1 1 1 1 0 1
90
45
45
23
45
53
60
28
74
71
35
18
28
18
18
18
28
45
28
1 0 1 1 0 1 1 1 1 0 1 0
90
45
45
23
53
60
28
74
71
35
18
28
18
18
18
28
45
90
28
1 0 1 1 0 1 1 1 1 0 1 0 0
45
23
53
60
28
74
71
35
18
28
18
18
28
45
90
18
1 0 1 1 0 1 1 1 1 0 1 0 0 0
45
23
53
60
28
74
71
35
18
28
18
18
18
28
45
90
45
23
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1
23
53
60
28
74
71
35
18
28
18
18
18
28
45
90
45
23
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1
23
53
60
28
74
71
35
18
28
18
18
18
28
45
23
90
45
53
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1
23
53
60
28
74
71
35
18
28
18
18
18
28
45
23
90
45
53
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1
23
53
60
28
74
71
35
18
28
18
18
18
28
45
23
90
53
45
23
60
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1
53
60
28
74
71
35
18
28
18
18
18
28
45
23
90
53
45
23
60
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1
53
60
28
74
71
35
18
28
18
18
18
28
45
23
90
45
23
53
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0
53
60
28
74
71
35
18
28
18
18
18
28
45
90
45
23
53
23
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0
60
28
74
71
35
18
28
18
18
18
28
45
23
90
45
23
53
28
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1
60
28
74
71
35
18
28
18
18
18
28
45
23
90
45
23
53
28
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1
60
28
74
71
35
18
28
18
18
18
28
45
23
90
28
45
23
53
60
74
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1
28
74
71
35
18
28
18
18
18
28
45
23
90
28
45
23
53
60
74
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1
28
74
71
35
18
28
18
18
18
28
45
23
90
45
23
53
60
28
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0
28
74
71
35
18
28
18
18
18
28
45
23
90
28
45
23
53
60
28
71
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1
74
71
35
18
28
18
18
18
28
45
23
90
28
45
23
53
60
28
71
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1
74
71
35
18
28
18
18
18
28
45
23
90
45
23
53
60
28
28
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0
74
71
35
18
28
18
18
18
28
45
23
90
28
45
23
53
60
28
74
35
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 1
71
35
18
28
18
18
18
28
45
23
90
45
23
53
60
28
74
28
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 1 0
71
35
18
28
18
18
18
28
45
90
45
23
53
60
28
74
71
23
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 1 0 0
35
18
28
18
18
28
45
90
45
23
53
60
28
74
71
18
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0
35
18
28
18
28
45
90
45
23
53
60
28
74
71
35
18
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 0
18
28
18
28
45
90
45
23
53
60
28
74
71
35
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 0 0
18
28
18
28
45
90
45
23
53
60
28
74
71
35
1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 0 0
Finishing Things Up
Total runtime:
O(n + (n / b) log n + 4b b2)
The runtime is
O(n + (n / b) log n + 4b b2)
Practical Concerns
Wait a Minute...
So what's next?
Next Time
Balanced Trees
B-Trees
Red/Black Trees