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
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
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
able to almost all of the questions after today.
the due-date.
1
Project/Written Report
the course website.
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 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
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.
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).
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)
16
Line Sweep with a Segment Tree (5)
17
Line Sweep with a Segment Tree (6)
18
Line Sweep with a Segment Tree (7)
19
Line Sweep with a Segment Tree (8)
20
Line Sweep with a Segment Tree (9)
21
Line Sweep with a Segment Tree (10)
22
Line Sweep with a Segment Tree (11)
23
Line Sweep with a Segment Tree (12)
24
Line Sweep with a Segment Tree (13)
25
Line Sweep with a Segment Tree (14)
26
Line Sweep with a Segment Tree (15)
27
Line Sweep with a Segment Tree (16)
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
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
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<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
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:
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!
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
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:
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)
70
Extra Topic for Segment Trees: Demo (2)
71
Extra Topic for Segment Trees: Demo (3)
72
Extra Topic for Segment Trees: Demo (4)
73
Extra Topic for Segment Trees: Demo (5)
74
Extra Topic for Segment Trees: Demo (6)
75
Extra Topic for Segment Trees: Demo (7)
76
Extra Topic for Segment Trees: Demo (8)
8 16
77
Extra Topic for Segment Trees: Demo (9)
8 16
8 16
78
Extra Topic for Segment Trees: Demo (10)
8 16
8 16
8 16
79
Segment Trees: Conclusion
Segment trees are wonderful and simple data structures that can be used for all kinds of range query problems.
There are many more applications of them that we have not discussed. For example:
These are good report topics!
80