range minimum queries - stanford university · minimum value in the range rather than the value...

271
Range Minimum Queries Part Two

Upload: others

Post on 30-Apr-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Range Minimum QueriesPart Two

Page 2: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Recap from Last Time

Page 3: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

41 59 26 5331 41 59 26 53 58 97 93

The RMQ Problem

● The Range Minimum Query (RMQ) problem is the following:

Given a fixed array A and two indices i ≤ j, what is the smallest element out of

A[i], A[i + 1], …, A[j – 1], A[j]?

31 58 97 93

Page 4: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Some Notation

● We'll say that an RMQ data structure has time complexity ⟨p(n), q(n)⟩ if

● preprocessing takes time at most p(n) and● queries take time at most q(n).

● Last time, we saw structures with the following runtimes:

● ⟨O(n2), O(1)⟩ (full preprocessing)● ⟨O(n log n), O(1)⟩ (sparse table)● ⟨O(n log log n), O(1)⟩ (hybrid approach)● ⟨O(n), O(n1/2)⟩ (blocking)● ⟨O(n), O(log n)⟩ (hybrid approach)● ⟨O(n), O(log log n)⟩ (hybrid approach)

Page 5: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

The Framework

31 41 59 26 53 58 97 93 23 84 62 64 33 83 27

Block-LevelRMQ

Block-LevelRMQ

Block-LevelRMQ

Block-LevelRMQ

Block-LevelRMQ

SummaryRMQ

31 26 23 62 27

● Split the input into blocks of size b.● Form an array of the block minima.● Construct a “summary” RMQ structure over the block minima.● Construct “block” RMQ structures for each block.● Aggregate the results together.

Page 6: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

The Framework

31 41 59 26 53 58 97 93 23 84 62 64 33 83 27

Block-LevelRMQ

Block-LevelRMQ

Block-LevelRMQ

Block-LevelRMQ

Block-LevelRMQ

SummaryRMQ

31 26 23 62 27

● Split the input into blocks of size b.● Form an array of the block minima.● Construct a “summary” RMQ structure over the block minima.● Construct “block” RMQ structures for each block.● Aggregate the results together.

Page 7: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

The Framework

31 41 59 26 53 58 97 93 23 84 62 64 33 83 27

Block-LevelRMQ

Block-LevelRMQ

Block-LevelRMQ

Block-LevelRMQ

Block-LevelRMQ

SummaryRMQ

31 26 23 62 27

● Split the input into blocks of size b.● Form an array of the block minima.● Construct a “summary” RMQ structure over the block minima.● Construct “block” RMQ structures for each block.● Aggregate the results together.

Page 8: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

The Framework

31 41 59 26 53 58 97 93 23 84 62 64 33 83 27

Block-LevelRMQ

Block-LevelRMQ

Block-LevelRMQ

Block-LevelRMQ

Block-LevelRMQ

SummaryRMQ

31 26 23 62 27

● Suppose we use a ⟨p₁(n), q₁(n)⟩-time RMQ solution for the summary and a ⟨p₂(n), q₂(n)⟩-time RMQ solution within each block. Let the block size be b.

● In the hybrid structure, the preprocessing time is

O(n + p₁(n / b) + (n / b) p₂(b))

Page 9: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

The Framework

31 41 59 26 53 58 97 93 23 84 62 64 33 83 27

Block-LevelRMQ

Block-LevelRMQ

Block-LevelRMQ

Block-LevelRMQ

Block-LevelRMQ

SummaryRMQ

31 26 23 62 27

● Suppose we use a ⟨p₁(n), q₁(n)⟩-time RMQ solution for the summary and a ⟨p₂(n), q₂(n)⟩-time RMQ solution within each block. Let the block size be b.

● In the hybrid structure, the query time is

O(q₁(n / b) + q₂(b))

Page 10: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Is there an ⟨O(n), O(1)⟩ solution to RMQ?

Yes!

Page 11: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

New Stuff!

Page 12: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

An Observation

Page 13: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

The Limits of Hybrids

● The preprocessing time on a hybrid structure is

O(n + p₁(n / b) + (n / b) p₂(b)). ● The query time is

O(q₁(n / b) + q₂(b)). ● What do p₂(n) and q₂(n) need to be if we want

to build a ⟨O(n), O(1)⟩ RMQ structure?

p₂(n) = O(n) q₂(n) = O(1)● Problem: We can’t build an optimal RMQ

structure unless we already have one!● Or can we?

Page 14: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

The Limits of Hybrids

● The preprocessing time on a hybrid structure is

O(n + p₁(n / b) + (n / b) p₂(b)).● The query time is

O(q₁(n / b) + q₂(b)).● What do p₂(n) and q₂(n) need to be if we want

to build a ⟨O(n), O(1)⟩ RMQ structure?

p₂(n) = O(n) q₂(n) = O(1)● Problem: We can’t build an optimal RMQ

structure unless we already have one!● Or can we?

Page 15: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

The Limits of Hybrids

The preprocessing time on a hybrid structure is

O(n + p₁(n / b) + (n / b) p₂(b)).

The query time is

O(q₁(n / b) + q₂(b)).

What do p₂(n) and q₂(n) need to be if we want to build a ⟨O(n), O(1)⟩ RMQ structure?

p₂(n) = O(n) q₂(n) = O(1)

Problem: We can’t build an optimal RMQ structure unless we already have one!

Or can we?

Construct an RMQ structure for each block.

Each block has size b.

Number of blocks: O(n / b).

Construct an RMQ structure for each block.

Each block has size b.

Number of blocks: O(n / b).

Page 16: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Key Difference

● Our original problem is

Solve RMQ on a single array in time ⟨O(n), O(1)⟩

● The new problem is

Solve RMQ on a large number of small arrays with O(1) query time and total

preprocessing time O(n).● These are not the same problem.● Question: Why is this second problem any

easier than the first?

Page 17: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

An Observation

10 30 20 40 166 361 261 464

Page 18: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

30 20 40 361 261 46410 30 20 40 166 361 261 464

An Observation

10 166

Page 19: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

30 20 40 361 261 46410 30 20 40 166 361 261 464

An Observation

10 166

Page 20: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

30 20 40 361 261 46430 20 40 361 261 46410 166

An Observation

10 166

Page 21: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

30 36110 16630 20 40 361 261 46420 40 261 46410 166

An Observation

Page 22: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

30 36110 16630 20 40 361 261 46420 40 261 46410 166

An Observation

Page 23: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

30 20 361 26110 16630 20 361 26110 16640 46440 464

An Observation

Page 24: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

30 20 361 26110 16630 20 40 361 261 46440 46410 166

An Observation

Page 25: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

30 20 361 26110 16630 20 40 361 261 46440 46410 166

An Observation

Page 26: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

30 20 361 26110 16630 20 361 26110 16640 46440 464

An Observation

Claim: The indices of the answers to any range

minimum queries on these two arrays are the same.

Claim: The indices of the answers to any range

minimum queries on these two arrays are the same.

Page 27: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Modifying RMQ

● From this point forward, let's have RMQA(i, j) denote the index of the minimum value in the range rather than the value itself.

● Observation: If RMQ structures return indices rather than values, we can use a single RMQ structure for both of these arrays:

30 20 361 26110 16630 20 361 26110 16640 46440 464

Page 28: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Where We’re Going

22 29 55 35 19 60 43 67 91 44 35 53 74 71 11

Block-LevelRMQ

Block-LevelRMQ

Block-LevelRMQ

SummaryRMQ

22 19 43 35 11

● Suppose we use an ⟨O(n log n), O(1)⟩ sparse table for the top and the ⟨O(n2), O(1)⟩ precompute-all structures for the blocks.

● However, whenever possible, we share block-level RMQ structures across multiple blocks.

● Assuming there aren’t “too many” different types of blocks, and assuming we can find and group blocks efficiently, this overall strategy might let us reach a ⟨O(n), O(1)⟩ solution!

Page 29: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Two Big Questions

Page 30: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

How can we tell when two blockscan share RMQ structures?

(Without an answer, this whole approach doesn’t work!)

How many block types are there,as a function of b?

(Without an answer, we can’t measure efficiency!)

Page 31: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

How can we tell when two blockscan share RMQ structures?

(Without an answer, this whole approach doesn’t work!)

How many block types are there,as a function of b?

(Without an answer, we can’t measure efficiency!)

Page 32: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

How can we tell when two blockscan share RMQ structures?

(Without an answer, this whole approach doesn’t work!)

How many block types are there,as a function of b?

(We need to tune b to ensure that many blocks are shared. What value of b should we pick?)

Page 33: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

The Adventure Begins!

Page 34: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Some Notation

● Let B₁ and B₂ be blocks of length b.● We'll say that B₁ and B₂ have the same block

type (denoted B₁ ~ B₂) if the following holds:

For all 0 ≤ i ≤ j < b:RMQB₁(i, j) = RMQB₂(i, j)

● Intuitively, the RMQ answers for B₁ are always the same as the RMQ answers for B₂.

● If we build an RMQ to answer queries on some block B₁, we can reuse that RMQ structure on some other block B₂ iff B₁ ~ B₂.

Page 35: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Detecting Block Types

● For this approach to work, we need to be able to check whether two blocks have the same block type.

● Problem: Our formal definition of B₁ ~ B₂ is defined in terms of RMQ.● Not particularly useful a priori; we don't want to

have to compute RMQ structures on B₁ and B₂ to decide whether they have the same block type!

● Is there a simpler way to determine whether two blocks have the same type?

Page 36: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

An Initial Idea

● Since the elements of the array are ordered and we're looking for the smallest value in certain ranges, we might look at the permutation types of the blocks.

Claim: If B₁ and B₂ have the same permutation on their elements, then B₁ ~ B₂.

31 41 59

12 2 5

16 18 3

66 26 6

27 18 28

60 22 14

66 73 84

72 99 27

Page 37: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

An Initial Idea

● Since the elements of the array are ordered and we're looking for the smallest value in certain ranges, we might look at the permutation types of the blocks.

Claim: If B₁ and B₂ have the same permutation on their elements, then B₁ ~ B₂.

31 41 59

12 2 5

16 18 3

66 26 6

27 18 28

60 22 14

66 73 84

72 99 27

1 2 3 2 3 1 2 1 3 1 2 3

3 1 2 3 2 1 3 2 1 2 3 1

Page 38: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

An Initial Idea

● Since the elements of the array are ordered and we're looking for the smallest value in certain ranges, we might look at the permutation types of the blocks.

● Claim: If B₁ and B₂ have the same permutation

on their elements, then B₁ ~ B₂.

31 41 59

12 2 5

16 18 3

66 26 6

27 18 28

60 22 14

66 73 84

72 99 27

1 2 3 2 3 1 2 1 3 1 2 3

3 1 2 3 2 1 3 2 1 2 3 1

Page 39: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Some Problems

● There are two main problems with this approach.

● Problem One: It's possible for two blocks to have different permutations but the same block type.

All three of these blocks have the same block type but different permutation types:

Problem Two: The number of possible permutations of a block is b!.

b has to be absolutely minuscule for b! to be small.

Is there a better criterion we can use?

Page 40: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Some Problems

● There are two main problems with this approach.

● Problem One: It's possible for two blocks to have different permutations but the same block type.

● All three of these blocks have the same block type but different permutation types:

Problem Two: The number of possible permutations of a block is b!.

b has to be absolutely minuscule for b! to be small.

Is there a better criterion we can use?

261 268 161 161 261 167

4 5 1 1 4 3

167 166

3 2

166 268

2 5

167 261 161

3 4 1

268 166

5 2

Page 41: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Some Problems

● There are two main problems with this approach.

● Problem One: It's possible for two blocks to have different permutations but the same block type.

● All three of these blocks have the same block type but different permutation types:

● Problem Two: The number of possible permutations of a block is b!.

● b has to be absolutely minuscule for b! to be small.

Is there a better criterion we can use?

261 268 161 161 261 167

4 5 1 1 4 3

167 166

3 2

166 268

2 5

167 261 161

3 4 1

268 166

5 2

Page 42: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Some Problems

● There are two main problems with this approach.

● Problem One: It's possible for two blocks to have different permutations but the same block type.

● All three of these blocks have the same block type but different permutation types:

● Problem Two: The number of possible permutations of a block is b!.

● b has to be absolutely minuscule for b! to be small.

● Is there a better criterion we can use?

261 268 161 161 261 167

4 5 1 1 4 3

167 166

3 2

166 268

2 5

167 261 161

3 4 1

268 166

5 2

Page 43: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

Claim: This property must hold recursively on the subarrays to the left and right of the minimum.

Page 44: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

Claim: This property must hold recursively on the subarrays to the left and right of the minimum.

261 268 161 167 166

Page 45: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

Claim: This property must hold recursively on the subarrays to the left and right of the minimum.

261 268 161 167 166

14 22 11 43 35

75 35 80 85 83

6 5 3 9 7

Page 46: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

Claim: This property must hold recursively on the subarrays to the left and right of the minimum.

261 268 161 167 166

14 22 11 43 35

75 35 80 85 83

6 5 3 9 7

Page 47: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

Claim: This property must hold recursively on the subarrays to the left and right of the minimum.

261 268 161 167 166

14 22 11 43 35

75 35 80 85 83

6 5 3 9 7

Page 48: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

Claim: This property must hold recursively on the subarrays to the left and right of the minimum.

261 268 161 167 166

14 22 11 43 35

75 35 80 85 83

6 5 3 9 7

Page 49: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

Claim: This property must hold recursively on the subarrays to the left and right of the minimum.

261 268 161 167 166

14 22 11 43 35

6 5 3 9 7

Page 50: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

Claim: This property must hold recursively on the subarrays to the left and right of the minimum.

261 268 161 167 166

14 22 11 43 35

6 5 3 9 7

Page 51: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

● Claim: This property must hold recursively on

the subarrays to the left and right of the minimum.

261 268 161 167 166

14 22 11 43 35

6 5 3 9 7

Page 52: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

161 167 166

11 43 35

3 9 7

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

● Claim: This property must hold recursively on

the subarrays to the left and right of the minimum.

261 268

14 22

6 5

Page 53: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

161 167 166

11 43 35

3 9 7

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

● Claim: This property must hold recursively on

the subarrays to the left and right of the minimum.

261 268

14 22

6 5

Page 54: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

161 167 166

11 43 35

3 9 7

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

● Claim: This property must hold recursively on

the subarrays to the left and right of the minimum.

261 268

14 22

6 5

Page 55: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

161 167 166

11 43 35

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

● Claim: This property must hold recursively on

the subarrays to the left and right of the minimum.

261 268

14 22

Page 56: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

161 167 166

11 43 35

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

● Claim: This property must hold recursively on

the subarrays to the left and right of the minimum.

261 268

14 22

Page 57: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

161 167 166

11 43 35

An Observation

● Claim: If B₁ ~ B₂, the minimum elements of B₁ and B₂ must occur at the same position.

● Claim: This property must hold recursively on

the subarrays to the left and right of the minimum.

261 268

14 22

261 268 161 167 166

14 22 11 43 35

Page 58: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees

● A Cartesian tree for an array is a binary tree built as follows:

● The root of the tree is the minimum element of the array.

● Its left and right subtrees are formed by recursively building Cartesian trees for the subarrays to the left and right of the minimum.

● (Base case: if the array is empty, the Cartesian tree is empty.)

● This is mechanical description of Cartesian trees; it defines Cartesian trees by showing how to make them.

26 28 16 17 33

16

26

28

17

33

1 2 3 4 5

1

2

3

4

5

Page 59: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees

● A Cartesian tree can also be defined as follows:

The Cartesian tree for an array is a binary tree obeying the min-heap property whose inorder traversal gives back

the original array.

● This is called an operational description; it says what properties the tree has rather than how to find it.

● Having multiple descriptions of the same object is incredibly useful – this will be a recurring theme this quarter!

26 28 16 17 33

16

26

28

17

33

1 2 3 4 5

1

2

3

4

5

Page 60: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have isomorphic Cartesian trees.

Proof sketch:

(⇒) Induction. B₁ and B₂ have equal RMQs, so corresponding ranges have the same minima.

2 5 1 3 4

1

2

5

3

4

2 3 1 4 5

1

2

3

4

5

“same shape”

“same shape”

Page 61: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have isomorphic Cartesian trees.

● Proof sketch:● (⇒) Induction. B₁ and B₂ have equal RMQs, so

corresponding ranges have minima at the same positions.

Page 62: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have isomorphic Cartesian trees.

● Proof sketch:● (⇒) Induction. B₁ and B₂ have equal RMQs, so

corresponding ranges have minima at the same positions.

Page 63: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have isomorphic Cartesian trees.

● Proof sketch:● (⇒) Induction. B₁ and B₂ have equal RMQs, so

corresponding ranges have minima at the same positions.

Page 64: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have isomorphic Cartesian trees.

● Proof sketch:● (⇒) Induction. B₁ and B₂ have equal RMQs, so

corresponding ranges have minima at the same positions.

Page 65: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have isomorphic Cartesian trees.

● Proof sketch:● (⇒) Induction. B₁ and B₂ have equal RMQs, so

corresponding ranges have minima at the same positions.

Page 66: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have isomorphic Cartesian trees.

● Proof sketch:● (⇒) Induction. B₁ and B₂ have equal RMQs, so

corresponding ranges have minima at the same positions.

Page 67: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have isomorphic Cartesian trees.

● Proof sketch:● (⇒) Induction. B₁ and B₂ have equal RMQs, so

corresponding ranges have minima at the same positions.

Page 68: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have isomorphic Cartesian trees.

● Proof sketch:● (⇐) Induction. It's possible to answer RMQ using a

recursive walk on the Cartesian tree.

Page 69: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have isomorphic Cartesian trees.

● Proof sketch:● (⇐) Induction. It's possible to answer RMQ using a

recursive walk on the Cartesian tree.

31 41 59 26 53 58 97 23 93 84 33 64 62 83 27

Page 70: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have isomorphic Cartesian trees.

● Proof sketch:● (⇐) Induction. It's possible to answer RMQ using a

recursive walk on the Cartesian tree.

31 41 59 26 53 58 97 23 93 84 33 64 62 83 27

23

26

62

27

83

3331

41

59

53

58

97 64

84

93

Page 71: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have isomorphic Cartesian trees.

● Proof sketch:● (⇐) Induction. It's possible to answer RMQ using a

recursive walk on the Cartesian tree.

31 41 59 26 53 58 97 23 93 84 33 64 62 83 27

23

26

62

27

83

3331

41

59

53

58

97 64

84

93

Page 72: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have isomorphic Cartesian trees.

● Proof sketch:● (⇐) Induction. It's possible to answer RMQ using a

recursive walk on the Cartesian tree.

31 41 59 26 53 58 97 23 93 84 33 64 62 83 27

23

26

62

27

83

3331

41

59

53

58

97 64

84

93

Page 73: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have isomorphic Cartesian trees.

● Proof sketch:● (⇐) Induction. It's possible to answer RMQ using a

recursive walk on the Cartesian tree.

31 41 59 26 53 58 97 23 93 84 33 64 62 83 27

23

26

62

27

83

3331

41

59

53

58

97 64

84

93

Page 74: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have isomorphic Cartesian trees.

● Proof sketch:● (⇐) Induction. It's possible to answer RMQ using a

recursive walk on the Cartesian tree.

31 41 59 26 53 58 97 23 93 84 33 64 62 83 27

23

26

62

27

83

3331

41

59

53

58

97 64

84

93

Page 75: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have isomorphic Cartesian trees.

● Proof sketch:● (⇐) Induction. It's possible to answer RMQ using a

recursive walk on the Cartesian tree.

31 41 59 26 53 58 97 23 93 84 33 64 62 83 27

23

26

62

27

83

3331

41

59

53

58

97 64

84

93

Page 76: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Trees and RMQ

● Theorem: Let B₁ and B₂ be blocks of length b. Then B₁ ~ B₂ iff B₁ and B₂ have isomorphic Cartesian trees.

● Proof sketch:● (⇐) Induction. It's possible to answer RMQ using a

recursive walk on the Cartesian tree.

31 41 59 26 53 58 97 23 93 84 33 64 62 83 27

23

26

62

27

83

3331

41

59

53

58

97 64

84

93

Page 77: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

How can we tell when two blockscan share RMQ structures?

When those blocks have isomorphic Cartesian trees!But how do we check that?

How many block types are there,as a function of b?

¯\_(ツ )_/¯

Page 78: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

How can we tell when two blockscan share RMQ structures?

When those blocks have isomorphic Cartesian trees!But how do we check that?

How many block types are there,as a function of b?

¯\_(ツ )_/¯

Page 79: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

How can we tell when two blockscan share RMQ structures?

When those blocks have isomorphic Cartesian trees!But how do we check that?

How many block types are there,as a function of b?

¯\_(ツ )_/¯

Page 80: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

How can we tell when two blockscan share RMQ structures?

When those blocks have isomorphic Cartesian trees!But how do we check that?

How many block types are there,as a function of b?

¯\_(ツ )_/¯

Page 81: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

How quickly can we build a Cartesian tree?

Page 82: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Building Cartesian Trees

● Here's a naïve algorithm for constructing Cartesian trees:● Find the minimum value.● Recursively build a Cartesian tree for the array to the left of the

minimum.● Recursively build a Cartesian tree with the elements to the right

of the minimum.● Return the overall tree.

● How efficient is this approach?

261 161

161

261 166

167268

268 167 166261 161 167 166268

Page 83: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Building Cartesian Trees

● This algorithm works by● doing a linear scan over the array to find the minimum value, then● recursively processing the left and right halves on the array.

● This is a divide-and-conquer algorithm! Here’s a runtime recurrence:

T(n) = T(nleft) + T(nright) + O(n)● Question: What does this recurrence solve to? (Hint: where

have you seen this recurrence?)● This is the same recurrence relation that comes up in the

analysis of quicksort!● If the min is always in the middle, runtime is Θ(n log n).● If the min is always all the way to the side, runtime is Θ(n2).

● Can we do better?

Page 84: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● It's possible to build a Cartesian tree over an array of length k faster than the naive algorithm.

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

93 84 33 64 62 83

93

84

33

64

62

83

Page 85: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● It's possible to build a Cartesian tree over an array of length k faster than the naive algorithm.

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393 84 33 64 62 83

93

84

33

64

62

83

63

Page 86: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● It's possible to build a Cartesian tree over an array of length k faster than the naive algorithm.

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393 84 33 64 62 83

93

84

33

64

62

83

63

Observation 1: After adding this node, it must be the rightmost node in the tree. (An inorder traversal of

a Cartesian tree gives back the original array.)

Observation 1: After adding this node, it must be the rightmost node in the tree. (An inorder traversal of

a Cartesian tree gives back the original array.)

Page 87: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● It's possible to build a Cartesian tree over an array of length k faster than the naive algorithm.

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393 84 33 64 62 83

93

84

64 83

63

Observation 1: After adding this node, it must be the rightmost node in the tree. (An inorder traversal of

a Cartesian tree gives back the original array.)

Observation 1: After adding this node, it must be the rightmost node in the tree. (An inorder traversal of

a Cartesian tree gives back the original array.)

33

62

Page 88: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● It's possible to build a Cartesian tree over an array of length k faster than the naive algorithm.

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393 84 33 64 62 83

93

84

64 83

63

33

62

Observation 2: Cartesian trees are min-heaps (each node’s value is at least as

large as its parent’s).

Observation 2: Cartesian trees are min-heaps (each node’s value is at least as

large as its parent’s).

Page 89: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● It's possible to build a Cartesian tree over an array of length k faster than the naive algorithm.

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393 84 33 64 62 83

93

84

64

83

63

33

62

Observation 2: Cartesian trees are min-heaps (each node’s value is at least as

large as its parent’s).

Observation 2: Cartesian trees are min-heaps (each node’s value is at least as

large as its parent’s).

Page 90: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● It's possible to build a Cartesian tree over an array of length k faster than the naive algorithm.

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393 84 33 64 62 83

93

84

64

83

63

33

62

Observation 2: Cartesian trees are min-heaps (each node’s value is at least as

large as its parent’s).

Observation 2: Cartesian trees are min-heaps (each node’s value is at least as

large as its parent’s).

Page 91: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● It's possible to build a Cartesian tree over an array of length k faster than the naive algorithm.

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393 84 33 64 62 83

93

84

64

83

33

62

Observation 2: Cartesian trees are min-heaps (each node’s value is at least as

large as its parent’s).

Observation 2: Cartesian trees are min-heaps (each node’s value is at least as

large as its parent’s).

63

Page 92: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● It's possible to build a Cartesian tree over an array of length k faster than the naive algorithm.

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393 84 33 64 62 83

93

84

64

83

33

62

63

58

58

Page 93: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● It's possible to build a Cartesian tree over an array of length k faster than the naive algorithm.

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393 84 33 64 62 83

93

84

64

83

33

62

63

58

58

Page 94: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● It's possible to build a Cartesian tree over an array of length k faster than the naive algorithm.

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393 84 33 64 62 83

93

84

64

83

33

62

63

58

58

Page 95: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● It's possible to build a Cartesian tree over an array of length k faster than the naive algorithm.

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393 84 33 64 62 83

93

84

64

83

33

62

63

58

58

Page 96: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● It's possible to build a Cartesian tree over an array of length k faster than the naive algorithm.

● High-level idea: Build a Cartesian tree for the first element, then the first two, then the first three, then the first four, etc.

6393 84 33 64 62 83

93

84

64

83

33

62

63

58

58

63

Page 97: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

Last Popped

Page 98: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

Last Popped

Page 99: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

93

Last Popped

Page 100: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

Last Popped

93

Page 101: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

Last Popped

93 93

Page 102: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

Last Popped

93

Page 103: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

84

Last Popped

93

Page 104: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

84

Last Popped

Page 105: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

84

Last Popped

Page 106: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

33

84

Last Popped

84 84

Page 107: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

33

84

Last Popped

84

Page 108: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

33

33

84

Last Popped

84

Page 109: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

Last Popped

Page 110: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

Last Popped

Page 111: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

64Last Popped

Page 112: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

64

62

Last Popped

Page 113: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

62

Last Popped

6464

Page 114: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

62

Last Popped

64

Page 115: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

62

62

Last Popped

64

Page 116: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

62

Last Popped

Page 117: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

62

Last Popped

Page 118: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

62

Last Popped

Page 119: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

83

62

Last Popped

Page 120: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

83

63

62

Last Popped

Page 121: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

63

62

Last Popped

83

83

Page 122: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

63

62

Last Popped

83

Page 123: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

63

62

Last Popped

83

Page 124: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

63

63

62

Last Popped

Page 125: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

63

63

58

62

Last Popped

Page 126: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

63

58

62

Last Popped

63

63

Page 127: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

83

63

58

62

Last Popped

6362

62

Page 128: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

83

63

58

62

Last Popped

62

Page 129: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

83

63

58

62

Last Popped

62

Page 130: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

83

63

58

62

Last Popped

62

Page 131: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● We can implement this algorithm efficiently by maintaining a stack of the nodes in the right spine.

● Pop the stack until the stack top is less than or equal to the new value (or the stack is empty). Remember the last node popped this way.

● Rewire the tree by

● making the stack top point to the new node, and

● making the most-recently-popped node the new node’s left child.

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

63

58

58

Last Popped

Page 132: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● How fast is this new approach on an array of k elements?

● Adding each element to the tree might take time O(k), since we may have to pop O(k) elements off the stack.

● Since there are k elements, that gives a time bound of O(k2).

● Question: Is this bound tight?

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

63

58

58

Page 133: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

A Better Approach

● Claim: This algorithm takes time O(k) on an array of size k.

● Idea: Each element is pushed onto the stack exactly once, when it’s created. Each element can therefore be popped at most once.

● Therefore, there are at O(k) pushes and O(k) pops, so the runtime is O(k).

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

63

58

58

Page 134: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

How can we tell when two blockscan share RMQ structures?

When those blocks have isomorphic Cartesian trees!And we can check this in time O(b)!

But there are lots of pairs of blocks to check!

How many block types are there,as a function of b?

¯\_(ツ )_/¯

Page 135: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

How can we tell when two blockscan share RMQ structures?

When those blocks have isomorphic Cartesian trees!And we can check this in time O(b)!

But there are lots of pairs of blocks to check!

How many block types are there,as a function of b?

¯\_(ツ )_/¯

Page 136: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Theorem: The number of Cartesian trees for arrays of length b is at most 4b.

In case you're curious, the actual number is

,

which is roughly equal to

.

Look up the Catalan numbers for more information!

4b

b3 /2√π

1b+1 (2b

b )

Page 137: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Proof Approach

● Our stack-based algorithm for generating Cartesian trees produces a Cartesian tree for every possible input array.

● Therefore, if we can count the number of possible executions of that algorithm, we can count the number of Cartesian trees.

● Using a simple counting scheme, we can show that there are at most 4b possible executions.

Page 138: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

6393 84 33 64 62 83 5863

0 means pop; 1 means push

Page 139: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

0 means pop; 1 means push

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

Page 140: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

93

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

10 means pop; 1 means push

Page 141: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

93

84

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

10 means pop; 1 means push

Page 142: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 00 means pop; 1 means push

Page 143: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 00 means pop; 1 means push

Page 144: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

84

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 10 means pop; 1 means push

Page 145: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

84

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 10 means pop; 1 means push

Page 146: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

84

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 10 means pop; 1 means push

Page 147: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

33

84

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 00 means pop; 1 means push

Page 148: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

33

84

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 00 means pop; 1 means push

Page 149: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

33

33

84

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 10 means pop; 1 means push

Page 150: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 10 means pop; 1 means push

Page 151: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 10 means pop; 1 means push

Page 152: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 10 means pop; 1 means push

Page 153: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 10 means pop; 1 means push

Page 154: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

64

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 10 means pop; 1 means push

Page 155: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

64

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 10 means pop; 1 means push

Page 156: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 00 means pop; 1 means push

Page 157: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 00 means pop; 1 means push

Page 158: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 00 means pop; 1 means push

Page 159: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

62

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 10 means pop; 1 means push

Page 160: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 10 means pop; 1 means push

Page 161: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 10 means pop; 1 means push

Page 162: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 10 means pop; 1 means push

Page 163: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

83

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 1 10 means pop; 1 means push

Page 164: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

83

63

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 1 10 means pop; 1 means push

Page 165: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

63

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 1 1 00 means pop; 1 means push

Page 166: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

63

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 1 1 00 means pop; 1 means push

Page 167: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

63

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 1 1 00 means pop; 1 means push

Page 168: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

63

63

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 1 1 0 10 means pop; 1 means push

Page 169: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

63

63

58

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 1 1 0 10 means pop; 1 means push

Page 170: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

63

58

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 1 1 0 1 00 means pop; 1 means push

Page 171: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

83

63

58

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 1 1 0 1 00 means pop; 1 means push

0

Page 172: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

83

63

58

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 1 1 0 1 00 means pop; 1 means push

0

Page 173: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

83

63

58

62

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 1 1 0 1 00 means pop; 1 means push

0

Page 174: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

63

58

58

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 1 1 0 1 00 means pop; 1 means push

0 1

Page 175: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

33

64

62

83

63

58

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 1 1 0 1 00 means pop; 1 means push

0 1 0

Page 176: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

64

62

83

63

58

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 1 1 0 1 00 means pop; 1 means push

0 1 0 0

Page 177: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

64

62

83

63

58

● There are at most 2b stack operations during the execution of the algorithm: b pushes and no more than b pops.

● Represent the execution of the algorithm as a 2b-bit number, where 1 means “push” and 0 means “pop.” We'll pad the end with 0's (pretend we pop everything from the stack).

● This number is the Cartesian tree number of a block.

1 0 1 0 1 1 0 1 1 0 1 0 0 1 0 0

Page 178: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Cartesian Tree Numbers

6393 84 33 64 62 83 5863

93

84

33

64

62

83

63

58

● Two blocks can share an RMQ structure iff they have the same Cartesian tree.

● Observation: If all we care about is finding blocks that can share RMQ structures, we never need to build Cartesian trees! Instead, we can just compute the Cartesian tree number for each block.

1 0 1 0 1 1 0 1 1 0 1 0 0 1 0 0

Page 179: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45 90 45 23 53 60 28 74 71 35

Page 180: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45 90 45 23 53 60 28 74 71 35

Page 181: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1

90 45 23 53 60 28 74 71 35

27

Page 182: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1

90 45 23 53 60 28 74 71 35

27

Page 183: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0

90 45 23 53 60 28 74 71 35

Page 184: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1

90 45 23 53 60 28 74 71 35

18

Page 185: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1

90 45 23 53 60 28 74 71 35

18

Page 186: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1

90 45 23 53 60 28 74 71 35

18 28

Page 187: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1

90 45 23 53 60 28 74 71 35

18 28

Page 188: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0

90 45 23 53 60 28 74 71 35

18

Page 189: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1

90 45 23 53 60 28 74 71 35

18 18

Page 190: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1

90 45 23 53 60 28 74 71 35

18 18

Page 191: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1

90 45 23 53 60 28 74 71 35

18 18 28

Page 192: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1

90 45 23 53 60 28 74 71 35

18 18 28

Page 193: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1

90 45 23 53 60 28 74 71 35

18 18 28 45

Page 194: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1

90 45 23 53 60 28 74 71 35

18 18 28 45

Page 195: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1

90 45 23 53 60 28 74 71 35

18 18 28 45 90

Page 196: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1

90 45 23 53 60 28 74 71 35

18 18 28 45 90

Page 197: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0

90 45 23 53 60 28 74 71 35

18 18 28 45

Page 198: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1

90 45 23 53 60 28 74 71 35

18 18 28 45 45

Page 199: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1

90 45 23 53 60 28 74 71 35

18 18 28 45 45

Page 200: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

18 18 28 45

Page 201: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0

18 18 28

Page 202: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0

18 18

Page 203: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1

18 18 23

Page 204: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1

18 18 23

Page 205: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1

18 18 23 53

Page 206: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1

18 18 23 53

Page 207: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1

18 18 23 53 60

Page 208: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1

18 18 23 53 60

Page 209: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0

18 18 23 53

Page 210: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0

18 18 23

Page 211: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1

18 18 23 28

Page 212: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1

18 18 23 28

Page 213: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1

18 18 23 28 74

Page 214: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1

18 18 23 28 74

Page 215: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0

18 18 23 28

Page 216: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1

18 18 23 28 71

Page 217: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1

18 18 23 28 71

Page 218: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1 0

18 18 23 28

Page 219: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1 0 1

18 18 23 28 35

Page 220: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1 0 1 0

18 18 23 28

Page 221: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1 0 1 0 0

18 18 23

Page 222: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0

18 18

Page 223: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 0

18

Page 224: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 0 0

Page 225: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Treeless Tree Numbers

27 18 28 18 28 45

1 0 1 1 0 1 1 1 1 0 1 0

90 45 23 53 60 28 74 71 35

0 0 1 1 1 0 0 1 1 0 1 0 1 0 0 0 0 0

Page 226: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

How can we tell when two blockscan share RMQ structures?

When they have the same Cartesian tree number!And we can check this in time O(b)!

How many block types are there,as a function of b?

At most 4b, because of the above algorithm!

Page 227: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

How can we tell when two blockscan share RMQ structures?

When they have the same Cartesian tree number!And we can check this in time O(b)!

How many block types are there,as a function of b?

At most 4b, because of the above algorithm!

Page 228: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

How can we tell when two blockscan share RMQ structures?

When they have the same Cartesian tree number!And we can check this in time O(b)!

And it’s easier to store numbers than trees!

How many block types are there,as a function of b?

At most 4b, because of the above algorithm!

Page 229: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

How can we tell when two blockscan share RMQ structures?

When they have the same Cartesian tree number!And we can check this in time O(b)!

And it’s easier to store numbers than trees!

How many block types are there,as a function of b?

At most 4b, because of the above algorithm!

Page 230: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

How can we tell when two blockscan share RMQ structures?

When they have the same Cartesian tree number!And we can check this in time O(b)!

And it’s easier to store numbers than trees!

How many block types are there,as a function of b?

At most 4b, because of the above algorithm!

Page 231: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Putting it all Together

Page 232: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

66 13 59 75 55 39 25 17 31 84 44 22

Summary RMQ(Sparse Table)

13 39 17 22

000000

000001

111111

Page 233: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

66 13 59 75 55 39 25 17 31 84 44 22

Summary RMQ(Sparse Table)

13 39 17 22

000000

000001

111111

101100

Block-levelRMQ

Page 234: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

66 13 59 75 55 39 25 17 31 84 44 22

Summary RMQ(Sparse Table)

13 39 17 22

000000

000001

111111

101100

Block-levelRMQ

101100

Page 235: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

66 13 59 75 55 39 25 17 31 84 44 22

Summary RMQ(Sparse Table)

13 39 17 22

000000

000001

111111

101100

Block-levelRMQ

101100

101010

Block-levelRMQ

Page 236: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

66 13 59 75 55 39 25 17 31 84 44 22

Summary RMQ(Sparse Table)

13 39 17 22

000000

000001

111111

101100

Block-levelRMQ

101100

101010

Block-levelRMQ

Page 237: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

66 13 59 75 55 39 25 17 31 84 44 22

Summary RMQ(Sparse Table)

13 39 17 22

000000

000001

111111

101100

Block-levelRMQ

101100

101010

Block-levelRMQ

Page 238: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

66 13 59 75 55 39 25 17 31 84 44 22

Summary RMQ(Sparse Table)

13 39 17 22

000000

000001

111111

101100

Block-levelRMQ

101100

101010

Block-levelRMQ

Page 239: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

66 13 59 75 55 39 25 17 31 84 44 22

Summary RMQ(Sparse Table)

13 39 17 22

Block-levelRMQ

Block-levelRMQ

Page 240: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

How Efficient is This?

Page 241: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

We’re using the hybrid approach,and all the types we’re using have

constant query times.

Query time: O(1)

Page 242: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + (n / b) log (n / b) + b2 4b)

Our preprocessing time is

Page 243: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + (n / b) log (n / b) + b2 4b)

Our preprocessing time is

Construct at most 4b RMQ structures at a cost

of O(b2) each.

Construct at most 4b RMQ structures at a cost

of O(b2) each.

Build a sparse table on blocks of

size n / b.

Build a sparse table on blocks of

size n / b.

Compute block minima; compute Cartesian tree numbers of each block.

Compute block minima; compute Cartesian tree numbers of each block.

Page 244: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + (n / b) log (n / b) + b2 4b)

This term grows exponentially in n unless

we pick b = O(log n).

This term grows exponentially in n unless

we pick b = O(log n).

This term will be superlinear unless we

pick b = Ω(log n).

This term will be superlinear unless we

pick b = Ω(log n).

Our preprocessing time is

Page 245: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + (n / b) log (n / b) + b2 4b)

Our preprocessing time is

Page 246: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + (n / b) log n + b2 4b)

Our preprocessing time is

Page 247: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + (n / b) log n + b2 4b)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.

Our preprocessing time is

Page 248: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + (n / b) log n + b2 4b)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.

Our preprocessing time is

Page 249: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + (n / k log₄ n) log n + b2 4b)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.

Our preprocessing time is

Page 250: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + (n / k log₄ n) log n + b2 4b)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.

Our preprocessing time is

Page 251: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + (n / log n) log n + b2 4b)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.

Our preprocessing time is

Page 252: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + (n / log n) log n + b2 4b)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.

Our preprocessing time is

Page 253: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + n + b2 4b)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.

Our preprocessing time is

Page 254: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + n + b2 4b)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.

Our preprocessing time is

Page 255: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + n + b2 4b)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.

Our preprocessing time is

Page 256: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + n + b2 4k log₄ n)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.

Our preprocessing time is

Page 257: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + n + b2 4 log₄ nᵏ)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.

Our preprocessing time is

Page 258: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + n + b2 4 log₄ nᵏ)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.

Our preprocessing time is

Page 259: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + n + b2 nk)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.

Our preprocessing time is

Page 260: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + n + (k log₄ n)2 nk)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.

Our preprocessing time is

Page 261: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + n + (k log₄ n)2 nk)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.

Our preprocessing time is

Page 262: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + n + (log n)2 nk)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.

Our preprocessing time is

Page 263: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + n + (log n)2 nk)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.

Our preprocessing time is

Page 264: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + n + (log n)2 nk)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.Now, set k = ½.Now, set k = ½.

Our preprocessing time is

Page 265: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + n + (log n)2 n1/2)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.Now, set k = ½.Now, set k = ½.

Our preprocessing time is

Page 266: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n + n + n)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.Now, set k = ½.Now, set k = ½.

Our preprocessing time is

Page 267: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

O(n)

Suppose we pick b = k log₄ n

for some constant k.

Suppose we pick b = k log₄ n

for some constant k.Now, set k = ½.Now, set k = ½.

Our preprocessing time is

Page 268: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

The Fischer-Heun Structure

● This data structure is called Fischer-Heun structure. It uses a modified version of our hybrid RMQ framework:● Set b = ½ log₄ n = ¼ log₂ n.● Split the input into blocks of size b. Compute an array of

minimum values from each block.● Build a sparse table on that array of minima.● Build per-block RMQ structures for each block, using

Cartesian tree numbers to avoid recomputing RMQ structures unnecessarily.

● Make queries using the standard hybrid solution approach.

● This is an ⟨O(n), O(1)⟩ solution to RMQ!

Page 269: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

The Method of Four Russians

● The technique employed here is an example of the Method of Four Russians.● Break the problem of size n into subproblems of

size b, plus some top-level problem of size n / b.– This is called a macro/micro decomposition.

● Solve all possible subproblems of size b.– Here, we only solved the subproblems that actually came

up in the original array, but that’s just an optimization.● Solve the overall problem by combining solutions

to the micro and macro problems.● This is a great way to shave off log factors,

which are both theoretical and practical wins!

Page 270: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Why Study RMQ?

● I chose RMQ as our first problem for a few reasons:

● See different approaches to the same problem. Different intuitions produced different runtimes.

● Build data structures out of other data structures. Many modern data structures use other data structures as building blocks, and it's very evident here.

● See the Method of Four Russians. This trick looks like magic the first few times you see it and shows up in lots of places.

● Explore modern data structures. This is relatively recent data structure (2005), and I wanted to show you that the field is still very active!

● So what's next?

Page 271: Range Minimum Queries - Stanford University · minimum value in the range rather than the value itself. Observation: If RMQ structures return indices rather than values, we can use

Next Time

● String Data Structures● Text, bytes, and genomes are everywhere.

How do we store them?● Tries

● A canonical string data structure.● Suffix Trees

● Exposing the hidden structures of strings.