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. 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 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 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 Line Sweep (1)

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

4 Line Sweep (2)

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

5 Line Sweep (3)

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

6 Line Sweep (4)

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

7 Line Sweep (5)

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

8 Line Sweep (6)

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

9 Line Sweep (7)

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

10 Line Sweep Step

As we iterate through the x-coordinates, we will have a current 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 Line Sweep: Segment 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 Line Sweep with a Segment Tree (1)

13 Line Sweep with a Segment Tree (2)

14 Line Sweep with a Segment Tree (3)

15 Line Sweep with a Segment Tree (4)

2

16 Line Sweep with a Segment Tree (5)

2

17 Line Sweep with a Segment Tree (6)

2 2

1

18 Line Sweep with a Segment Tree (7)

2 2

1

19 Line Sweep with a Segment Tree (8)

2 2

1 1

20 Line Sweep with a Segment Tree (9)

2 2

1 1

21 Line Sweep with a Segment Tree (10)

2 2 2

1 1

22 Line Sweep with a Segment Tree (11)

2 2 2

1 1

23 Line Sweep with a Segment Tree (12)

2 2 2 4 1 1

24 Line Sweep with a Segment Tree (13)

2 2 2 4 1 1

25 Line Sweep with a Segment Tree (14)

2 2 2 2 4 1 1

26 Line Sweep with a Segment Tree (15)

2 2 2 2 4 1 1

27 Line Sweep with a Segment Tree (16)

2 2 2 2 4 1 1

28 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

2 3 2 1 1 1

29 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 operation: Output the sum of space that is covered by an odd number of intervals. Notice how the update becomes a range xor.

30 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 Euler Tour: Review

Recall Euler tours over trees.

32 Euler Tour: Review

33 Euler Tour: Review

34 Euler Tour: Review

35 Euler Tour: Review

36 Euler Tour: Review

37 Euler Tour: Review

38 Euler Tour: Review

39 Euler Tour: A Single Line

This is our final Euler tour. It’s only a single line! 40 Euler Tour: Segment

41 Euler Tour: Segment

42 Euler Tour: Segment

43 Euler Tour: Segment

44 Euler Tour: Segment

45 Euler Tour: Segment

46 Euler Tour: Segment

47 Euler Tour: Segment

48 Euler Tour: Segment

49 Euler Tour: Segment

50 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 Euler Tour: Subtree Segment

52 Euler Tour: Subtree Segment

53 Euler Tour: Subtree Segment

54 Euler Tour: Subtree Segment

55 Euler Tour: Subtree Segment

56 Euler Tour: Subtree Segment

57 Euler Tour: Subtree Segment

58 Euler Tour: Subtree Segment

59 Euler Tour: Subtree Segment

60 Euler Tour: Subtree Segment

61 Euler Tour: Subtree Segment

62 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>&children, vector&l, vector&r,

4 int i, int&count) {

5 l[i]= count++;

6 for(intc : children[i])

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

8 r[i]= count;

9 }

63 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 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 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 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! [a, b] = [4, 10] 4 6 7 7 9 11 15 17

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

67 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 4 6 7 11 7 9 15 17 4 7 6 11 7 15 9 17 7 4 6 11 7 15 17 9

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

68 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 Extra Topic for Segment Trees: Demo (1)

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

70 Extra Topic for Segment Trees: Demo (2)

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

71 Extra Topic for Segment Trees: Demo (3)

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

72 Extra Topic for Segment Trees: Demo (4)

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

73 Extra Topic for Segment Trees: Demo (5)

7 9 15 17 6 11 4

74 Extra Topic for Segment Trees: Demo (6)

4

6 11

7 9 15 17

75 Extra Topic for Segment Trees: Demo (7)

4 [a, b] = [8, 16]

6 11

7 9 15 17

76 Extra Topic for Segment Trees: Demo (8)

8 16 0 4 [a, b] = [8, 16]

6 11

7 9 15 17

77 Extra Topic for Segment Trees: Demo (9)

8 16 0 4 [a, b] = [8, 16]

8 16 1 6 11

7 9 15 17

78 Extra Topic for Segment Trees: Demo (10)

8 16 0 4 [a, b] = [8, 16]

8 16 1 6 11 Answer: 3

8 16 2 7 9 15 17

79 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