Range Minimum Queries

169
Advanced Algorithms – COMS31900 Range Minimum Queries Benjamin Sach

Transcript of Range Minimum Queries

Page 1: Range Minimum Queries

Advanced Algorithms – COMS31900

Range Minimum Queries

Benjamin Sach

Page 2: Range Minimum Queries

Range minimum query

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 3: Range Minimum Queries

Range minimum query

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

After preprocessing, a range minimum query is given by RMQ(i, j)

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 4: Range Minimum Queries

Range minimum query

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

After preprocessing, a range minimum query is given by RMQ(i, j)

the output is the location of the smallest element in A[i, j]

17 823 73 51 82 19 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 5: Range Minimum Queries

Range minimum query

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

After preprocessing, a range minimum query is given by RMQ(i, j)

the output is the location of the smallest element in A[i, j]

17 823 73 51 82 19 32 5 67 91 14 46

i = 3 j = 7

9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 6: Range Minimum Queries

Range minimum query

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

After preprocessing, a range minimum query is given by RMQ(i, j)

the output is the location of the smallest element in A[i, j]

17 823 73 51 82 19 32 5 67 91 14 46

i = 3 j = 7

19 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 7: Range Minimum Queries

Range minimum query

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

After preprocessing, a range minimum query is given by RMQ(i, j)

the output is the location of the smallest element in A[i, j]

17 823 73 51 82 19 32 5 67 91 14 46

i = 3 j = 7

RMQ(3, 7) = 6

19 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 8: Range Minimum Queries

Range minimum query

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

After preprocessing, a range minimum query is given by RMQ(i, j)

the output is the location of the smallest element in A[i, j]

17 823 73 51 82 19 32 5 67 91 14 46

e.g. RMQ(3, 7) = 6, which is the location of the smallest element in A[3, 7]

i = 3 j = 7

RMQ(3, 7) = 6

19 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 9: Range Minimum Queries

Range minimum query

i = 5 j = 11

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

After preprocessing, a range minimum query is given by RMQ(i, j)

the output is the location of the smallest element in A[i, j]

17 823 73 51 82 19 32 5 67 91 14 46

e.g. RMQ(3, 7) = 6, which is the location of the smallest element in A[3, 7]

RMQ(5, 11) = 8

e.g. RMQ(5, 11) = 8, which is the location of the smallest element in A[5, 11]

9 21 545

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 10: Range Minimum Queries

Range minimum query

i = 5 j = 11

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

After preprocessing, a range minimum query is given by RMQ(i, j)

the output is the location of the smallest element in A[i, j]

17 823 73 51 82 19 32 5 67 91 14 46

e.g. RMQ(3, 7) = 6, which is the location of the smallest element in A[3, 7]

RMQ(5, 11) = 8

e.g. RMQ(5, 11) = 8, which is the location of the smallest element in A[5, 11]

• We will discuss several algorithms which give trade-offs between

space used, prep. time and query time

9 21 545

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 11: Range Minimum Queries

Range minimum query

i = 5 j = 11

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

After preprocessing, a range minimum query is given by RMQ(i, j)

the output is the location of the smallest element in A[i, j]

17 823 73 51 82 19 32 5 67 91 14 46

e.g. RMQ(3, 7) = 6, which is the location of the smallest element in A[3, 7]

RMQ(5, 11) = 8

e.g. RMQ(5, 11) = 8, which is the location of the smallest element in A[5, 11]

• We will discuss several algorithms which give trade-offs between

space used, prep. time and query time

• Ideally we would like O(n) space, O(n) prep. time and O(1) query time

9 21 545

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 12: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

19

Page 13: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

19

Page 14: Range Minimum Queries

Block decomposition

smallest from each pair

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

19

Page 15: Range Minimum Queries

Block decomposition

smallest from each pair

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

19

Page 16: Range Minimum Queries

Block decomposition

smallest from each pair

19A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

19

Page 17: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

19

Page 18: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

19

Page 19: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

smallest from each four

1919

Page 20: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

smallest from each four

1919

Page 21: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

19

Page 22: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

19

Page 23: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

smallest from each eight

19

Page 24: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

smallest from each eight

8 19

Page 25: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

19

Page 26: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

19

Page 27: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

19

Page 28: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

Ak is an array of length nk so that for all i: Ak[i] = (x, v)

where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.

19

Page 29: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

Ak is an array of length nk so that for all i: Ak[i] = (x, v)

where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.

We store Ak for all k = 1, 2, 4, 8 . . . 6 n

19

Page 30: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

Ak is an array of length nk so that for all i: Ak[i] = (x, v)

where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.

We store Ak for all k = 1, 2, 4, 8 . . . 6 n

How much space is this?

19

Page 31: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

Ak is an array of length nk so that for all i: Ak[i] = (x, v)

where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.

We store Ak for all k = 1, 2, 4, 8 . . . 6 n

How much space is this? O(n) in total

19

Page 32: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

Ak is an array of length nk so that for all i: Ak[i] = (x, v)

where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.

We store Ak for all k = 1, 2, 4, 8 . . . 6 n

How much space is this? O(n) in total

n2

n4

n8

n16

+

+

+

+

19

Page 33: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

Ak is an array of length nk so that for all i: Ak[i] = (x, v)

where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.

We store Ak for all k = 1, 2, 4, 8 . . . 6 n

How much space is this? O(n) in total

n2

n4

n8

n16

+

+

+

+

O(n)

19

Page 34: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

Ak is an array of length nk so that for all i: Ak[i] = (x, v)

where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.

We store Ak for all k = 1, 2, 4, 8 . . . 6 n

How much space is this? O(n) in total

19

Page 35: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

Ak is an array of length nk so that for all i: Ak[i] = (x, v)

where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.

We store Ak for all k = 1, 2, 4, 8 . . . 6 n

How much space is this? O(n) in total

How quickly can we build them?

19

Page 36: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

Ak is an array of length nk so that for all i: Ak[i] = (x, v)

where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.

We store Ak for all k = 1, 2, 4, 8 . . . 6 n

How much space is this? O(n) in total

How quickly can we build them?

construct the Ak

arrays bottom-up

19

Page 37: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

Ak is an array of length nk so that for all i: Ak[i] = (x, v)

where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.

We store Ak for all k = 1, 2, 4, 8 . . . 6 n

How much space is this? O(n) in total

How quickly can we build them?

compute this from

these in O(1) time

construct the Ak

arrays bottom-up

19

Page 38: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

Ak is an array of length nk so that for all i: Ak[i] = (x, v)

where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.

We store Ak for all k = 1, 2, 4, 8 . . . 6 n

How much space is this? O(n) in total

How quickly can we build them? O(n) preprocessing time

compute this from

these in O(1) time

construct the Ak

arrays bottom-up

19

Page 39: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

Ak is an array of length nk so that for all i: Ak[i] = (x, v)

where v is the minimum in A[ik, (i+ 1)k] and x is its location in A.

We store Ak for all k = 1, 2, 4, 8 . . . 6 n

How much space is this? O(n) in total

How quickly can we build them? O(n) preprocessing time

19

Page 40: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

19

Page 41: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

19

Page 42: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

RMQ(1,9)

19

Page 43: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

RMQ(1,9)

19

Page 44: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

RMQ(1,9)

19

Page 45: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

RMQ(1,9)

Repeat:

19

Page 46: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

RMQ(1,9)

Repeat:

19

Page 47: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

RMQ(1,9)

Repeat:

(break ties arbitrarily)

19

Page 48: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

RMQ(1,9)

Repeat:

19

Page 49: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

RMQ(1,9)

Repeat:

19

Page 50: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

RMQ(1,9)

Repeat:

19

Page 51: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

RMQ(1,9)

Repeat:

How many blocks do we pick?

19

Page 52: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

Repeat:

How many blocks do we pick?

19

Page 53: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

Repeat:

How many blocks do we pick?

19

Page 54: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

Repeat:

How many blocks do we pick?

19

Page 55: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

Repeat:

How many blocks do we pick?

19

Page 56: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

Repeat:

How many blocks do we pick?

19

never threein a row

Page 57: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

Repeat:

How many blocks do we pick?

19

never threein a row

Page 58: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

Repeat:

How many blocks do we pick?

at most 2 blocks of each size

19

never threein a row

Page 59: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

Repeat:

How many blocks do we pick?

at most 2 blocks of each size

19

Page 60: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

Repeat:

How many blocks do we pick?

at most 2 blocks of each size

19

never two onone side

Page 61: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

Repeat:

How many blocks do we pick?

at most 2 blocks of each size

19

never two onone side

Page 62: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

Repeat:

How many blocks do we pick?

at most 2 blocks of each size

19

Page 63: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

Repeat:

How many blocks do we pick?

at most 2 blocks of each size

19

Page 64: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

Repeat:

How many blocks do we pick?

at most 2 blocks of each size

19

no gaps

Page 65: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

Repeat:

How many blocks do we pick?

at most 2 blocks of each size

19

no gaps

Page 66: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

Repeat:

How many blocks do we pick?

at most 2 blocks of each size

19

10,000 foot view

no valleys

no plateaus

Page 67: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

RMQ(1,9)

Repeat:

How many blocks do we pick?

at most 2 blocks of each size

19

Page 68: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

RMQ(1,9)

Repeat:

How many blocks do we pick?

at most 2 blocks of each size

There are O(logn) sizes

19

Page 69: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

RMQ(1,9)

Repeat:

How many blocks do we pick?

at most 2 blocks of each size

There are O(logn) sizes

Picking the blocks from

Ak takes O(1) time

19

Page 70: Range Minimum Queries

Block decomposition

A

n

17 823 73 51 82 32 5 67 91 14 46 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

21 4 6 8 11 13 14

17 8 51 19 5 14 9 21

2 6 8 13

8 19 5 9

2 8

8 5

8

5

A2

A4

A8

A16

How do we find RMQ(i,j)?

Find the largest block which

is completely contained within the query interval

but doesn’t overlap a block you chose before

The minimum is the smallest in all these blocksbecause they cover the query

RMQ(1,9)

Repeat:

How many blocks do we pick?

at most 2 blocks of each size

There are O(logn) sizes

Picking the blocks from

Ak takes O(1) time

So we have . . . O(n) space,

O(n) prep time

O(logn) query time

19

Page 71: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

Page 72: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

Page 73: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

2

The array R2 stores RMQ(i, i+ 1) for all i

A

Page 74: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

2

Page 75: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

2

Page 76: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

2

Page 77: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

2

Page 78: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

2

Page 79: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

2

Page 80: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

2

Page 81: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

2

Page 82: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

2

Page 83: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

2

Page 84: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

2stored in R2

Page 85: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

R4 stores RMQ(i, i+ 3) for all i

2stored in R2

Page 86: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

R4 stores RMQ(i, i+ 3) for all i

2stored in R2

4stored in R4

Page 87: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

R4 stores RMQ(i, i+ 3) for all i

R8 stores RMQ(i, i+ 7) for all i

2stored in R2

4stored in R4

Page 88: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

R4 stores RMQ(i, i+ 3) for all i

R8 stores RMQ(i, i+ 7) for all i

2stored in R2

4stored in R4

8stored in R8

Page 89: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

R4 stores RMQ(i, i+ 3) for all i

R8 stores RMQ(i, i+ 7) for all i

Rk stores RMQ(i, i+ k − 1) for all i

2stored in R2

4stored in R4

8stored in R8

Page 90: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

We build Rk for k = 2, 4, 8, 16 . . . 6 n

R4 stores RMQ(i, i+ 3) for all i

R8 stores RMQ(i, i+ 7) for all i

Rk stores RMQ(i, i+ k − 1) for all i

2stored in R2

4stored in R4

8stored in R8

Page 91: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

We build Rk for k = 2, 4, 8, 16 . . . 6 n

R4 stores RMQ(i, i+ 3) for all i

R8 stores RMQ(i, i+ 7) for all i

Rk stores RMQ(i, i+ k − 1) for all i

each of the O(logn) arrays uses O(n) space

2stored in R2

4stored in R4

8stored in R8

Page 92: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

We build Rk for k = 2, 4, 8, 16 . . . 6 n

R4 stores RMQ(i, i+ 3) for all i

R8 stores RMQ(i, i+ 7) for all i

Rk stores RMQ(i, i+ k − 1) for all i

each of the O(logn) arrays uses O(n) space

so O(n logn) total space

2stored in R2

4stored in R4

8stored in R8

Page 93: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

We build Rk for k = 2, 4, 8, 16 . . . 6 n

R4 stores RMQ(i, i+ 3) for all i

R8 stores RMQ(i, i+ 7) for all i

Rk stores RMQ(i, i+ k − 1) for all i

each of the O(logn) arrays uses O(n) space

so O(n logn) total space

2stored in R2

4stored in R4

8stored in R8

We build R2 from A in O(n) time

Page 94: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

We build Rk for k = 2, 4, 8, 16 . . . 6 n

R4 stores RMQ(i, i+ 3) for all i

R8 stores RMQ(i, i+ 7) for all i

Rk stores RMQ(i, i+ k − 1) for all i

each of the O(logn) arrays uses O(n) space

so O(n logn) total space

2stored in R2

4stored in R4

8stored in R8

We build R2 from A in O(n) time

We build R2k from Rk in O(n) time

Page 95: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

We build Rk for k = 2, 4, 8, 16 . . . 6 n

R4 stores RMQ(i, i+ 3) for all i

R8 stores RMQ(i, i+ 7) for all i

Rk stores RMQ(i, i+ k − 1) for all i

each of the O(logn) arrays uses O(n) space

so O(n logn) total space

2stored in R2

4stored in R4

8stored in R8

We build R2 from A in O(n) time

We build R2k from Rk in O(n) time

how?

Page 96: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

We build Rk for k = 2, 4, 8, 16 . . . 6 n

R4 stores RMQ(i, i+ 3) for all i

R8 stores RMQ(i, i+ 7) for all i

Rk stores RMQ(i, i+ k − 1) for all i

each of the O(logn) arrays uses O(n) space

so O(n logn) total space

We build R2 from A in O(n) time

We build R2k from Rk in O(n) time

how?

Page 97: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

We build Rk for k = 2, 4, 8, 16 . . . 6 n

R4 stores RMQ(i, i+ 3) for all i

R8 stores RMQ(i, i+ 7) for all i

Rk stores RMQ(i, i+ k − 1) for all i

each of the O(logn) arrays uses O(n) space

so O(n logn) total space

We build R2 from A in O(n) time

We build R2k from Rk in O(n) time

how?

2k

k k

Page 98: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

We build Rk for k = 2, 4, 8, 16 . . . 6 n

R4 stores RMQ(i, i+ 3) for all i

R8 stores RMQ(i, i+ 7) for all i

Rk stores RMQ(i, i+ k − 1) for all i

each of the O(logn) arrays uses O(n) space

so O(n logn) total space

We build R2 from A in O(n) time

We build R2k from Rk in O(n) time

how?

2k

k k

the min in here

Page 99: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

We build Rk for k = 2, 4, 8, 16 . . . 6 n

R4 stores RMQ(i, i+ 3) for all i

R8 stores RMQ(i, i+ 7) for all i

Rk stores RMQ(i, i+ k − 1) for all i

each of the O(logn) arrays uses O(n) space

so O(n logn) total space

We build R2 from A in O(n) time

We build R2k from Rk in O(n) time

how?

2k

k k

the min in here

is the min of these two mins(which we have in Rk )

Page 100: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

We build Rk for k = 2, 4, 8, 16 . . . 6 n

R4 stores RMQ(i, i+ 3) for all i

R8 stores RMQ(i, i+ 7) for all i

Rk stores RMQ(i, i+ k − 1) for all i

each of the O(logn) arrays uses O(n) space

so O(n logn) total space

We build R2 from A in O(n) time

We build R2k from Rk in O(n) time

how?

Page 101: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

We build Rk for k = 2, 4, 8, 16 . . . 6 n

R4 stores RMQ(i, i+ 3) for all i

R8 stores RMQ(i, i+ 7) for all i

Rk stores RMQ(i, i+ k − 1) for all i

each of the O(logn) arrays uses O(n) space

so O(n logn) total space

2stored in R2

4stored in R4

8stored in R8

We build R2 from A in O(n) time

We build R2k from Rk in O(n) time

how?

Page 102: Range Minimum Queries

More space, faster queries

Key Idea precompute the answers for every interval of length 2, 4, 8, 16 . . .

The array R2 stores RMQ(i, i+ 1) for all i

A

We build Rk for k = 2, 4, 8, 16 . . . 6 n

R4 stores RMQ(i, i+ 3) for all i

R8 stores RMQ(i, i+ 7) for all i

Rk stores RMQ(i, i+ k − 1) for all i

each of the O(logn) arrays uses O(n) space

so O(n logn) total space

2stored in R2

4stored in R4

8stored in R8

We build R2 from A in O(n) time

We build R2k from Rk in O(n) time

how?

This takes O(n logn) prep time

Page 103: Range Minimum Queries

More space, faster queries

A

2stored in R2

4stored in R4

8stored in R8

we build Rk for k = 2, 4, 8, 16 . . . 6 n

Rk stores RMQ(i, i+ k − 1) for all i,

How do we compute RMQ(i, j)?

If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer

Page 104: Range Minimum Queries

More space, faster queries

A

2stored in R2

4stored in R4

8stored in R8

we build Rk for k = 2, 4, 8, 16 . . . 6 n

Rk stores RMQ(i, i+ k − 1) for all i,

How do we compute RMQ(i, j)?

If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer

8stored in R8

Page 105: Range Minimum Queries

More space, faster queries

A

2stored in R2

4stored in R4

8stored in R8

we build Rk for k = 2, 4, 8, 16 . . . 6 n

Rk stores RMQ(i, i+ k − 1) for all i,

How do we compute RMQ(i, j)?

If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer

these queries take O(1) time

8stored in R8

Page 106: Range Minimum Queries

More space, faster queries

A

2stored in R2

4stored in R4

8stored in R8

we build Rk for k = 2, 4, 8, 16 . . . 6 n

Rk stores RMQ(i, i+ k − 1) for all i,

How do we compute RMQ(i, j)?

If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer

these queries take O(1) time

Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2k

Page 107: Range Minimum Queries

More space, faster queries

A

2stored in R2

4stored in R4

8stored in R8

we build Rk for k = 2, 4, 8, 16 . . . 6 n

Rk stores RMQ(i, i+ k − 1) for all i,

How do we compute RMQ(i, j)?

If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer

these queries take O(1) time

Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2k

Page 108: Range Minimum Queries

More space, faster queries

A

2stored in R2

4stored in R4

8stored in R8

we build Rk for k = 2, 4, 8, 16 . . . 6 n

Rk stores RMQ(i, i+ k − 1) for all i,

How do we compute RMQ(i, j)?

If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer

these queries take O(1) time

Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)

Page 109: Range Minimum Queries

More space, faster queries

A

2stored in R2

4stored in R4

8stored in R8

we build Rk for k = 2, 4, 8, 16 . . . 6 n

Rk stores RMQ(i, i+ k − 1) for all i,

How do we compute RMQ(i, j)?

If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer

these queries take O(1) time

Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)

(these two queries take O(1) time)

Page 110: Range Minimum Queries

More space, faster queries

A

2stored in R2

4stored in R4

8stored in R8

we build Rk for k = 2, 4, 8, 16 . . . 6 n

Rk stores RMQ(i, i+ k − 1) for all i,

How do we compute RMQ(i, j)?

If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer

these queries take O(1) time

Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)

(these two queries take O(1) time)

Page 111: Range Minimum Queries

More space, faster queries

A

2stored in R2

4stored in R4

8stored in R8

we build Rk for k = 2, 4, 8, 16 . . . 6 n

Rk stores RMQ(i, i+ k − 1) for all i,

How do we compute RMQ(i, j)?

If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer

these queries take O(1) time

Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)

(these two queries take O(1) time)

This takes O(1) time but why does it work?

Page 112: Range Minimum Queries

More space, faster queries

A

we build Rk for k = 2, 4, 8, 16 . . . 6 n

Rk stores RMQ(i, i+ k − 1) for all i,

How do we compute RMQ(i, j)?

If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer

these queries take O(1) time

Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)

(these two queries take O(1) time)

This takes O(1) time but why does it work?

Page 113: Range Minimum Queries

More space, faster queries

A

we build Rk for k = 2, 4, 8, 16 . . . 6 n

Rk stores RMQ(i, i+ k − 1) for all i,

How do we compute RMQ(i, j)?

If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer

these queries take O(1) time

Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)

(these two queries take O(1) time)

This takes O(1) time but why does it work?

i j`

Page 114: Range Minimum Queries

More space, faster queries

A

we build Rk for k = 2, 4, 8, 16 . . . 6 n

Rk stores RMQ(i, i+ k − 1) for all i,

How do we compute RMQ(i, j)?

If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer

these queries take O(1) time

Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)

(these two queries take O(1) time)

This takes O(1) time but why does it work?

i j`

kk

Page 115: Range Minimum Queries

More space, faster queries

A

we build Rk for k = 2, 4, 8, 16 . . . 6 n

Rk stores RMQ(i, i+ k − 1) for all i,

How do we compute RMQ(i, j)?

If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer

these queries take O(1) time

Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)

(these two queries take O(1) time)

This takes O(1) time but why does it work?

i j`

kk

these overlap

because 2k > `

Page 116: Range Minimum Queries

More space, faster queries

A

we build Rk for k = 2, 4, 8, 16 . . . 6 n

Rk stores RMQ(i, i+ k − 1) for all i,

How do we compute RMQ(i, j)?

If the interval length, ` = (j − i+ 1), is a power-of-two - just look up the answer

these queries take O(1) time

Otherwise, find the k = 2, 4, 8, 16 . . . such that k 6 ` < 2kCompute the minimum of RMQ(i, i+ k − 1) and RMQ(j − k + 1, j)

(these two queries take O(1) time)

This takes O(1) time but why does it work?

i j`

kk

these overlap

because 2k > `

the min is either in

here or in here

Page 117: Range Minimum Queries

Range minimum query (intermediate) summary

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

After preprocessing, a range minimum query is given by RMQ(i, j)

the output is the location of the smallest element in A[i, j]

17 823 73 51 82 19 32 5 67 91 14 46

i = 3 j = 7

RMQ(3, 7) = 6

19 9 21 54

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 118: Range Minimum Queries

Range minimum query (intermediate) summary

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

After preprocessing, a range minimum query is given by RMQ(i, j)

the output is the location of the smallest element in A[i, j]

17 823 73 51 82 19 32 5 67 91 14 46

i = 3 j = 7

RMQ(3, 7) = 6

19 9 21 54

Solution 1

O(n) space

O(n) prep time

O(logn) query time

Solution 2

O(n logn) space

O(n logn) prep time

O(1) query time

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 119: Range Minimum Queries

Range minimum query (intermediate) summary

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

After preprocessing, a range minimum query is given by RMQ(i, j)

the output is the location of the smallest element in A[i, j]

17 823 73 51 82 19 32 5 67 91 14 46

i = 3 j = 7

RMQ(3, 7) = 6

19 9 21 54

Can we do better?Solution 1

O(n) space

O(n) prep time

O(logn) query time

Solution 2

O(n logn) space

O(n logn) prep time

O(1) query time

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 120: Range Minimum Queries

Range minimum query (intermediate) summary

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

After preprocessing, a range minimum query is given by RMQ(i, j)

the output is the location of the smallest element in A[i, j]

17 823 73 51 82 19 32 5 67 91 14 46

i = 3 j = 7

RMQ(3, 7) = 6

19 9 21 54

Can we do better?Solution 1

O(n) space

O(n) prep time

O(logn) query time

Solution 2

O(n logn) space

O(n logn) prep time

O(1) query time

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

(yes)

Page 121: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

n

Page 122: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

n

Page 123: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

n

n = nlogn

Page 124: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

log n

n

n = nlogn

Page 125: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

log n

the smallest of these

is stored here

n

n = nlogn

Page 126: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

n

n = nlogn

Page 127: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

L0L1

L2L3

L4L5 Ln

Page 128: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

L0L1

L2L3

L4L5 Ln

Page 129: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

all of these

are stored here

L0L1

L2L3

L4L5 Ln

Page 130: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

L0L1

L2L3

L4L5 Ln

Page 131: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

L0L1

L2L3

L4L5 Ln

Page 132: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

using Solution 2

L0L1

L2L3

L4L5 Ln

Page 133: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

using Solution 2

Solution 2 on A

O(n logn) prep time

O(1) query time

O(n logn) space

Recall . . .

L0L1

L2L3

L4L5 Ln

Page 134: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

using Solution 2

Solution 2 on A

O(n logn) prep time

O(1) query time

O(n logn) space

Recall . . .

L0L1

L2L3

L4L5 Ln

Page 135: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

using Solution 2

Solution 2 on A

O(n logn) prep time

O(1) query time

O(n logn) space

Recall . . .

Solution 2 on H

O(n log n) prep time

O(1) query time

O(n log n) space

L0L1

L2L3

L4L5 Ln

Page 136: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

using Solution 2

Solution 2 on A

O(n logn) prep time

O(1) query time

O(n logn) space

Recall . . .

Solution 2 on H

O(n log n) prep time

O(1) query time

O(n log n) space = O((

nlogn

)log(

nlogn

))

L0L1

L2L3

L4L5 Ln

Page 137: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

using Solution 2

Solution 2 on A

O(n logn) prep time

O(1) query time

O(n logn) space

Recall . . .

Solution 2 on H

O(n log n) prep time

O(1) query time

O(n log n) space = O((

nlogn

)log(

nlogn

))= O(n)

L0L1

L2L3

L4L5 Ln

Page 138: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

using Solution 2

Solution 2 on A

O(n logn) prep time

O(1) query time

O(n logn) space

Recall . . .

Solution 2 on H

O(n log n) prep time

O(1) query time

O(n log n) space = O((

nlogn

)log(

nlogn

))= O(n)

= O(n)

L0L1

L2L3

L4L5 Ln

Page 139: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

using Solution 2

Solution 2 on A

O(n logn) prep time

O(1) query time

O(n logn) space

Recall . . .

Solution 2 on H

O(n log n) prep time

O(1) query time

O(n log n) space = O((

nlogn

)log(

nlogn

))= O(n)

= O(n)

in O(n) space/prep time

L0L1

L2L3

L4L5 Ln

Page 140: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

using Solution 2 in O(n) space/prep time

L0L1

L2L3

L4L5 Ln

Page 141: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

using Solution 2 in O(n) space/prep time

Preprocess each array Li (which has length logn) to answer RMQs. . .

using Solution 2

L0L1

L2L3

L4L5 Ln

Page 142: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

using Solution 2 in O(n) space/prep time

Preprocess each array Li (which has length logn) to answer RMQs. . .

using Solution 2

Solution 2 on Li

O(1) query timeO((logn) log logn)) space/prep time

L0L1

L2L3

L4L5 Ln

Page 143: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

using Solution 2 in O(n) space/prep time

Preprocess each array Li (which has length logn) to answer RMQs. . .

using Solution 2 in O(logn log logn) space/prep time

Solution 2 on Li

O(1) query timeO((logn) log logn)) space/prep time

L0L1

L2L3

L4L5 Ln

Page 144: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

using Solution 2 in O(n) space/prep time

Preprocess each array Li (which has length logn) to answer RMQs. . .

using Solution 2 in O(logn log logn) space/prep time

Total space = O(n) +O(n logn log logn)

L0L1

L2L3

L4L5 Ln

Page 145: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

using Solution 2 in O(n) space/prep time

Preprocess each array Li (which has length logn) to answer RMQs. . .

using Solution 2 in O(logn log logn) space/prep time

Total space = O(n) +O(n logn log logn)

space for RMQ structures

for all the Li arraysspace for RMQ structure for H

L0L1

L2L3

L4L5 Ln

Page 146: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

using Solution 2 in O(n) space/prep time

Preprocess each array Li (which has length logn) to answer RMQs. . .

using Solution 2 in O(logn log logn) space/prep time

Total space = O(n) +O(n logn log logn)

space for RMQ structures

for all the Li arraysspace for RMQ structure for H

= O(n log logn)

L0L1

L2L3

L4L5 Ln

Page 147: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

using Solution 2 in O(n) space/prep time

Preprocess each array Li (which has length logn) to answer RMQs. . .

using Solution 2 in O(logn log logn) space/prep time

Total space = O(n) +O(n logn log logn) = O(n log logn)

L0L1

L2L3

L4L5 Ln

Page 148: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

Preprocess the array H (which has length n = nlogn ) to answer RMQs. . .

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

using Solution 2 in O(n) space/prep time

Preprocess each array Li (which has length logn) to answer RMQs. . .

using Solution 2 in O(logn log logn) space/prep time

Total space = O(n) +O(n logn log logn) = O(n log logn)

Total prep. time = O(n log logn)

L0L1

L2L3

L4L5 Ln

Page 149: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

L0L1

L2L3

L4L5 Ln

Page 150: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

How do we answer a query in A?

L0L1

L2L3

L4L5 Ln

Page 151: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

i j

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

How do we answer a query in A?

L0L1

L2L3

L4L5 Ln

Page 152: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

i j

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

How do we answer a query in A?

Do at most one query in H . . .

and one query in at most two different Li

then take the smallest

L0L1

L2L3

L4L5 Ln

Page 153: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

i j

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

How do we answer a query in A?

Do at most one query in H . . .

and one query in at most two different Li

then take the smallest

L0L1

L2L3

L4L5 Ln

Page 154: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

i j

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

How do we answer a query in A?

Do at most one query in H . . .

and one query in at most two different Li

then take the smallest

i′ j′

L0L1

L2L3

L4L5 Ln

Page 155: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

i j

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

How do we answer a query in A?

Do at most one query in H . . .

and one query in at most two different Li

then take the smallest

i′ =⌈

ilogn

⌉j′ =

⌊j

logn

⌋i′ j′

L0L1

L2L3

L4L5 Ln

Page 156: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

i j

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

How do we answer a query in A?

Do at most one query in H . . .

and one query in at most two different Li

then take the smallest

i′ =⌈

ilogn

⌉j′ =

⌊j

logn

⌋i′ j′

indices into H

L0L1

L2L3

L4L5 Ln

Page 157: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

i j

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

How do we answer a query in A?

Do at most one query in H . . .

and one query in at most two different Li

then take the smallest

i′ =⌈

ilogn

⌉j′ =

⌊j

logn

⌋i′ j′

L0L1

L2L3

L4L5 Ln

Page 158: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

i j

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

How do we answer a query in A?

Do at most one query in H . . .

and one query in at most two different Li

then take the smallest

i′ =⌈

ilogn

⌉j′ =

⌊j

logn

⌋i′ j′

L0L1

L2L3

L4L5 Ln

Page 159: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

i j

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

How do we answer a query in A?

Do at most one query in H . . .

and one query in at most two different Li

then take the smallest

i′ =⌈

ilogn

⌉j′ =

⌊j

logn

⌋i′ j′

L0L1

L2L3

L4L5 Ln

Page 160: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

i j

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

How do we answer a query in A?

Do at most one query in H . . .

and one query in at most two different Li (here we query L1 and L5)

then take the smallest

i′ =⌈

ilogn

⌉j′ =

⌊j

logn

⌋i′ j′

L0L1

L2L3

L4L5 Ln

Page 161: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

i j

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

How do we answer a query in A?

Do at most one query in H . . .

and one query in at most two different Li (here we query L1 and L5)

This takes O(1) total query timethen take the smallest

i′ =⌈

ilogn

⌉j′ =

⌊j

logn

⌋i′ j′

L0L1

L2L3

L4L5 Ln

Page 162: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

i j

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

How do we answer a query in A?

Do at most one query in H . . .

and one query in at most two different Li (here we query L1 and L5)

This takes O(1) total query timethen take the smallest

i′ =⌈

ilogn

⌉j′ =

⌊j

logn

⌋i′ j′

L0L1

L2L3

L4L5 Ln

Page 163: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

i j

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

How do we answer a query in A?

Do at most one query in H . . .

and one query in at most two different Li (here we query L1 and L5)

This takes O(1) total query timethen take the smallest

i′ =⌈

ilogn

⌉j′ =

⌊j

logn

⌋i′ j′

Solution 3

O(n log logn) space O(n log logn) prep time O(1) query time

L0L1

L2L3

L4L5 Ln

Page 164: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

i j

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

How do we answer a query in A?

Do at most one query in H . . .

and one query in at most two different Li (here we query L1 and L5)

This takes O(1) total query timethen take the smallest

i′ =⌈

ilogn

⌉j′ =

⌊j

logn

⌋i′ j′

Solution 4

O(n log log logn) space O(n log log logn) prep time O(1) query time

L0L1

L2L3

L4L5 Ln

Page 165: Range Minimum Queries

Low-resolution RMQ

A

Key Idea replace A with a smaller, ‘low resolution’ array H

H

n

i j

and many small arrays L0, L1, L2 . . . ‘for the details’

n

n = nlogn

How do we answer a query in A?

Do at most one query in H . . .

and one query in at most two different Li (here we query L1 and L5)

This takes O(1) total query timethen take the smallest

i′ =⌈

ilogn

⌉j′ =

⌊j

logn

⌋i′ j′

Solution 4

O(n log log logn) space O(n log log logn) prep time O(1) query time

how?

L0L1

L2L3

L4L5 Ln

Page 166: Range Minimum Queries

Range minimum query summary

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

After preprocessing, a range minimum query is given by RMQ(i, j)

the output is the location of the smallest element in A[i, j]

17 823 73 51 82 19 32 5 67 91 14 46

i = 3 j = 7

RMQ(3, 7) = 6

19 9 21 54

Solution 1

O(n) space

O(n) prep time

O(logn) query time

Solution 2

O(n logn) space

O(n logn) prep time

O(1) query time

Solution 3

O(n log logn) space

O(n log logn) prep time

O(1) query time

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 167: Range Minimum Queries

Range minimum query summary

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

After preprocessing, a range minimum query is given by RMQ(i, j)

the output is the location of the smallest element in A[i, j]

17 823 73 51 82 19 32 5 67 91 14 46

i = 3 j = 7

RMQ(3, 7) = 6

19 9 21 54

Can we do O(n) space and O(1) query time?

Solution 1

O(n) space

O(n) prep time

O(logn) query time

Solution 2

O(n logn) space

O(n logn) prep time

O(1) query time

Solution 3

O(n log logn) space

O(n log logn) prep time

O(1) query time

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 168: Range Minimum Queries

Range minimum query summary

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

After preprocessing, a range minimum query is given by RMQ(i, j)

the output is the location of the smallest element in A[i, j]

17 823 73 51 82 19 32 5 67 91 14 46

i = 3 j = 7

RMQ(3, 7) = 6

19 9 21 54

Can we do O(n) space and O(1) query time? Yes. . .

Solution 1

O(n) space

O(n) prep time

O(logn) query time

Solution 2

O(n logn) space

O(n logn) prep time

O(1) query time

Solution 3

O(n log logn) space

O(n log logn) prep time

O(1) query time

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Page 169: Range Minimum Queries

Range minimum query summary

A

Preprocess an integer array A (length n) to answer range minimum queries. . .

n

After preprocessing, a range minimum query is given by RMQ(i, j)

the output is the location of the smallest element in A[i, j]

17 823 73 51 82 19 32 5 67 91 14 46

i = 3 j = 7

RMQ(3, 7) = 6

19 9 21 54

Can we do O(n) space and O(1) query time? Yes. . . but not until next lecture

Solution 1

O(n) space

O(n) prep time

O(logn) query time

Solution 2

O(n logn) space

O(n logn) prep time

O(1) query time

Solution 3

O(n log logn) space

O(n log logn) prep time

O(1) query time

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15