Range Queries Part 3: Line Sweep, Subtree Queries, and Extra Topic - - PowerPoint PPT Presentation

range queries
SMART_READER_LITE
LIVE PREVIEW

Range Queries Part 3: Line Sweep, Subtree Queries, and Extra Topic - - PowerPoint PPT Presentation

Range Queries Part 3: Line Sweep, Subtree Queries, and Extra Topic in Segment Trees Lucca Siaudzionis and Jack Spalding-Jamieson 2020/02/11 University of British Columbia Announcements A3 is released. Its due Sunday, March 1st. You


slide-1
SLIDE 1

Range Queries

Part 3: Line Sweep, Subtree Queries, and Extra Topic in Segment Trees

Lucca Siaudzionis and Jack Spalding-Jamieson 2020/02/11

University of British Columbia

slide-2
SLIDE 2

Announcements

  • A3 is released. It’s due Sunday, March 1st.
  • You will have all the necessary material background by the end of Thursday, but you will be

able to almost all of the questions after today.

  • Piazza on the due date: we will no longer be responding to Piazza problems on the day of

the due-date.

  • Please start on the assignments earlier. (You have three weeks for the next one!)

1

slide-3
SLIDE 3

Project/Written Report

  • A description and rubric for the project are posted on the course website!
  • Topics should be approved before Tuesday, March 3rd. There is a list of cool topics on

the course website.

  • You should take a look at some of them today.

2

slide-4
SLIDE 4

Rectangles: Problem Description

Input: Up to n ≤ 106 axis-aligned rectangles with coordinates in the space −105 ≤ x, y ≤ 105, some of which may overlap. Output: The total area covered by any rectangle.

3

slide-5
SLIDE 5

Line Sweep (1)

Imagine a giant vertical line sweeping from left to right over the rectangles.

4

slide-6
SLIDE 6

Line Sweep (2)

Imagine a giant vertical line sweeping from left to right over the rectangles.

5

slide-7
SLIDE 7

Line Sweep (3)

Imagine a giant vertical line sweeping from left to right over the rectangles.

6

slide-8
SLIDE 8

Line Sweep (4)

Imagine a giant vertical line sweeping from left to right over the rectangles.

7

slide-9
SLIDE 9

Line Sweep (5)

Imagine a giant vertical line sweeping from left to right over the rectangles.

8

slide-10
SLIDE 10

Line Sweep (6)

Imagine a giant vertical line sweeping from left to right over the rectangles.

9

slide-11
SLIDE 11

Line Sweep (7)

Imagine a giant vertical line sweeping from left to right over the rectangles.

10

slide-12
SLIDE 12

Line Sweep Step

As we iterate through the x-coordinates, we will have a current set of intersecting rectangles, which are of the form of vertical segments. The total area we see at each step is the total length of our combined segments. How do we actually keep track of which vertical segments we’re intersecting with?

11

slide-13
SLIDE 13

Line Sweep: Segment Tree Solution

We can store our current vertical segments with a (lazy) segment tree! Whenever we encounter a rectangle with top coordinate y and height h, we add 1 to the range [y, y + h]. Whenever we encounter the end of such a rectangle, we remove 1 from the same range. We then use a new operation in our (lazy) segment tree: The total count of non-zero values.

  • This is very easy to do, but very hard to analyse (we will not be analysing it in class).
  • Create a RMQ segment tree that also stores the total length at each node, where length is

computed to be the width of the node’s interval if the minimum value is > 0, and is computed recursively otherwise (sum of answer for sub-segments).

  • Additionally, do not push the lazy value when querying nodes contained fully within the

query interval.

12

slide-14
SLIDE 14

Line Sweep with a Segment Tree (1)

13

slide-15
SLIDE 15

Line Sweep with a Segment Tree (2)

14

slide-16
SLIDE 16

Line Sweep with a Segment Tree (3)

15

slide-17
SLIDE 17

Line Sweep with a Segment Tree (4)

2

16

slide-18
SLIDE 18

Line Sweep with a Segment Tree (5)

2

17

slide-19
SLIDE 19

Line Sweep with a Segment Tree (6)

2 2 1

18

slide-20
SLIDE 20

Line Sweep with a Segment Tree (7)

2 2 1

19

slide-21
SLIDE 21

Line Sweep with a Segment Tree (8)

2 2 1 1

20

slide-22
SLIDE 22

Line Sweep with a Segment Tree (9)

2 2 1 1

21

slide-23
SLIDE 23

Line Sweep with a Segment Tree (10)

2 2 1 1 2

22

slide-24
SLIDE 24

Line Sweep with a Segment Tree (11)

2 2 1 1 2

23

slide-25
SLIDE 25

Line Sweep with a Segment Tree (12)

2 2 1 1 2 4

24

slide-26
SLIDE 26

Line Sweep with a Segment Tree (13)

2 2 1 1 2 4

25

slide-27
SLIDE 27

Line Sweep with a Segment Tree (14)

2 2 1 1 2 4 2

26

slide-28
SLIDE 28

Line Sweep with a Segment Tree (15)

2 2 1 1 2 4 2

27

slide-29
SLIDE 29

Line Sweep with a Segment Tree (16)

2 2 1 1 2 4 2

28

slide-30
SLIDE 30

Discussion Problem (based on ICPC Pac-NW Regional 2018)

Input: Given 1 ≤ n ≤ 106 rectangle coordinates with −105 ≤ x, y ≤ 105. Output: The total amount of area covered by an odd number of rectangles.

1 1 1 2 2 3

1

29

slide-31
SLIDE 31

Discussion Problem (based on ICPC Pac-NW Regional 2018) - Insight

You can almost run the exact same algorithm, but now your segment tree has to do a different

  • peration: Output the sum of space that is covered by an odd number of intervals.

Notice how the update becomes a range xor.

30

slide-32
SLIDE 32

Subtree Queries

Input: A tree T with 1 ≤ n ≤ 106 nodes, and a value stored at each node, and 1 ≤ q ≤ 105 queries of the following forms: Type A: Add x to a node t and everything in the subtree rooted at t. Type B: What is the sum of all nodes in the subtree rooted at t?

31

slide-33
SLIDE 33

Euler Tour: Review

Recall Euler tours over trees.

32

slide-34
SLIDE 34

Euler Tour: Review

33

slide-35
SLIDE 35

Euler Tour: Review

34

slide-36
SLIDE 36

Euler Tour: Review

35

slide-37
SLIDE 37

Euler Tour: Review

36

slide-38
SLIDE 38

Euler Tour: Review

37

slide-39
SLIDE 39

Euler Tour: Review

38

slide-40
SLIDE 40

Euler Tour: Review

39

slide-41
SLIDE 41

Euler Tour: A Single Line

This is our final Euler tour. It’s only a single line!

40

slide-42
SLIDE 42

Euler Tour: Segment

41

slide-43
SLIDE 43

Euler Tour: Segment

42

slide-44
SLIDE 44

Euler Tour: Segment

43

slide-45
SLIDE 45

Euler Tour: Segment

44

slide-46
SLIDE 46

Euler Tour: Segment

45

slide-47
SLIDE 47

Euler Tour: Segment

46

slide-48
SLIDE 48

Euler Tour: Segment

47

slide-49
SLIDE 49

Euler Tour: Segment

48

slide-50
SLIDE 50

Euler Tour: Segment

49

slide-51
SLIDE 51

Euler Tour: Segment

50

slide-52
SLIDE 52

Euler Tour: Segment Tree

Create a segment tree over the Euler tour segment! Now we can support range queries and range updates on this segment. We want to support subtree queries and updates. Do these translate to segments? Yes! The segment for the subtree rooted at t is exactly the segment [l, r], where l is the first appearance of t in the euler tour, and r is the last appearance.

51

slide-53
SLIDE 53

Euler Tour: Subtree Segment

52

slide-54
SLIDE 54

Euler Tour: Subtree Segment

53

slide-55
SLIDE 55

Euler Tour: Subtree Segment

54

slide-56
SLIDE 56

Euler Tour: Subtree Segment

55

slide-57
SLIDE 57

Euler Tour: Subtree Segment

56

slide-58
SLIDE 58

Euler Tour: Subtree Segment

57

slide-59
SLIDE 59

Euler Tour: Subtree Segment

58

slide-60
SLIDE 60

Euler Tour: Subtree Segment

59

slide-61
SLIDE 61

Euler Tour: Subtree Segment

60

slide-62
SLIDE 62

Euler Tour: Subtree Segment

61

slide-63
SLIDE 63

Euler Tour: Subtree Segment

62

slide-64
SLIDE 64

Euler Tour: Subtree Segment Code

1

// after running, segment tree will be on the range [0,n-1]

2

// subtree queries for subtree root node i will be on the range [l[i],r[i]]

3

void euler(vector<vector<int>> &children, vector<int> &l, vector<int> &r,

4

int i, int &count) {

5

l[i] = count++;

6

for (int c : children[i])

7

euler(children,l,r,c,count);

8

r[i] = count;

9

}

63

slide-65
SLIDE 65

Discussion Problem: LCA (again)

Input: A tree with 1 ≤ n ≤ 105 nodes, 1 ≤ q ≤ 105 queries asking for the lowest common ancestor (LCA) of two nodes u, v in the tree. Output: For each query, output the LCA of the two nodes. Can we solve this problem with a segment tree?

64

slide-66
SLIDE 66

Discussion Problem: LCA (again) - Insight

If we complete the Euler tour as normal (not how it was done in the code from before), then the LCA of u and v is the smallest-depth node appearing in between u and v in the Euler tour (the smallest depth along a segment!).

65

slide-67
SLIDE 67

Extra Topic for Segment Trees: Problem Statement

Input: An array of size 1 ≤ n ≤ 105, followed by q queries of the following kinds:

  • Point updates to the array.
  • Range queries of the form ”How many entries are there in the range [l, r] with values in

the range [a, b]?”. Output: The answer to each query.

66

slide-68
SLIDE 68

Extra Topic for Segment Trees: Vectors (1)

If we have a sorted list/vector of elements, it’s easy to find how many entries are in the corresponding range: Use binary search to find each endpoint of the range!

4 6 7 7 9 11 15 17 [a, b] = [4, 10]

This takes O(log n). In order to support updates, we need to use an order statistics tree (or a treap) instead of a vector.

67

slide-69
SLIDE 69

Extra Topic for Segment Trees: Vectors (2)

If we have k sorted lists/vectors, we can repeat this process to find the total count in each range for a total of O(k log n) time. Inside each node of a segment tree, we can store the sorted vector of elements in the range of the node:

4 6 7 7 9 11 15 17 6 7 7 9 11 15 17 4 6 7 9 11 15 17 4 6 7 7 9 11 15 17 4 7

This table also gives us the amount of space this segment tree takes up: O(n log n).

68

slide-70
SLIDE 70

Extra Topic for Segment Trees: Vectors (3)

Given this table, we need only find the disjoint set of segments that correspond to our solution. This is done with standard querying for segment trees. There are O(log n) = k such segments, so the overall complexity of our solution is O(n log2 n)

69

slide-71
SLIDE 71

Extra Topic for Segment Trees: Demo (1)

4 6 7 7 9 11 15 17 6 7 7 9 11 15 17 4 6 7 9 11 15 17 4 6 7 7 9 11 15 17 4 7

70

slide-72
SLIDE 72

Extra Topic for Segment Trees: Demo (2)

6 7 7 9 11 15 17 4 6 7 9 11 15 17 4 6 7 7 9 11 15 17 4 7

71

slide-73
SLIDE 73

Extra Topic for Segment Trees: Demo (3)

7 9 15 17 6 7 9 11 15 17 4 6 7 7 9 11 15 17 4 7

72

slide-74
SLIDE 74

Extra Topic for Segment Trees: Demo (4)

7 9 15 17 6 7 9 11 15 17 6 7 7 9 11 15 17 4

73

slide-75
SLIDE 75

Extra Topic for Segment Trees: Demo (5)

7 9 15 17 6 11 4

74

slide-76
SLIDE 76

Extra Topic for Segment Trees: Demo (6)

7 9 15 17 6 11 4

75

slide-77
SLIDE 77

Extra Topic for Segment Trees: Demo (7)

[a, b] = [8, 16]

7 9 15 17 6 11 4

76

slide-78
SLIDE 78

Extra Topic for Segment Trees: Demo (8)

4

[a, b] = [8, 16]

8 16

7 9 15 17 6 11

77

slide-79
SLIDE 79

Extra Topic for Segment Trees: Demo (9)

4

[a, b] = [8, 16]

8 16

7 9 15 17 6 11

8 16

1

78

slide-80
SLIDE 80

Extra Topic for Segment Trees: Demo (10)

4

[a, b] = [8, 16]

8 16

7 9 15 17 6 11

8 16

1

8 16

2 Answer: 3

79

slide-81
SLIDE 81

Segment Trees: Conclusion

Segment trees are wonderful and simple data structures that can be used for all kinds of range query problems.

  • They are very memory efficient and efficient in practice.
  • They can be used to store all kinds of data for many unusual problems.

There are many more applications of them that we have not discussed. For example:

  • Dynamic segment trees
  • 2D/3D segment trees (segment trees that store other segment trees at the nodes)
  • Persistent segment trees

These are good report topics!

80