CPSC 490: Problem Solving in Computer Science which is greater than - - PowerPoint PPT Presentation

cpsc 490 problem solving in computer science
SMART_READER_LITE
LIVE PREVIEW

CPSC 490: Problem Solving in Computer Science which is greater than - - PowerPoint PPT Presentation

Lecture 14: Subtree queries and updates, line sweep Henry Xia, Brandon Zhang based on CPSC 490 slides from 2014-2018 2019-03-07 University of British Columbia CPSC 490: Problem Solving in Computer Science which is greater than or equal to x


slide-1
SLIDE 1

CPSC 490: Problem Solving in Computer Science

Lecture 14: Subtree queries and updates, line sweep

Henry Xia, Brandon Zhang

based on CPSC 490 slides from 2014-2018

2019-03-07

University of British Columbia

slide-2
SLIDE 2

Problem 1 – Binary search?

Given an array A, answer many queries of the form “What is the smallest number in A[l..r] which is greater than or equal to x”?

1

slide-3
SLIDE 3

Problem 1 – Solution

At each node, store a sorted list of the elements in that segment.

  • How big are all our arrays in total?

O n n

  • How to merge our children? We could just concatenate the lists and sort them, or we

can do the merge that mergesort uses.

  • To handle a query:
  • Consider the O

n segments that our range decomposes into. We want to find the smallest number in these segments which is x.

  • This is just the minimum over each of these segments.
  • In each segment, binary search to find the minimum element in the segment which is

x.

  • Overall, O

2 n per query.

Takeaway: we can store O segment size information for each segment! If we use sets instead of lists, we can support point updates as well!

2

slide-4
SLIDE 4

Problem 1 – Solution

At each node, store a sorted list of the elements in that segment.

  • How big are all our arrays in total? O(n log n)
  • How to merge our children? We could just concatenate the lists and sort them, or we

can do the merge that mergesort uses.

  • To handle a query:
  • Consider the O

n segments that our range decomposes into. We want to find the smallest number in these segments which is x.

  • This is just the minimum over each of these segments.
  • In each segment, binary search to find the minimum element in the segment which is

x.

  • Overall, O

2 n per query.

Takeaway: we can store O segment size information for each segment! If we use sets instead of lists, we can support point updates as well!

2

slide-5
SLIDE 5

Problem 1 – Solution

At each node, store a sorted list of the elements in that segment.

  • How big are all our arrays in total? O(n log n)
  • How to merge our children?

We could just concatenate the lists and sort them, or we can do the merge that mergesort uses.

  • To handle a query:
  • Consider the O

n segments that our range decomposes into. We want to find the smallest number in these segments which is x.

  • This is just the minimum over each of these segments.
  • In each segment, binary search to find the minimum element in the segment which is

x.

  • Overall, O

2 n per query.

Takeaway: we can store O segment size information for each segment! If we use sets instead of lists, we can support point updates as well!

2

slide-6
SLIDE 6

Problem 1 – Solution

At each node, store a sorted list of the elements in that segment.

  • How big are all our arrays in total? O(n log n)
  • How to merge our children? We could just concatenate the lists and sort them, or we

can do the merge that mergesort uses.

  • To handle a query:
  • Consider the O

n segments that our range decomposes into. We want to find the smallest number in these segments which is x.

  • This is just the minimum over each of these segments.
  • In each segment, binary search to find the minimum element in the segment which is

x.

  • Overall, O

2 n per query.

Takeaway: we can store O segment size information for each segment! If we use sets instead of lists, we can support point updates as well!

2

slide-7
SLIDE 7

Problem 1 – Solution

At each node, store a sorted list of the elements in that segment.

  • How big are all our arrays in total? O(n log n)
  • How to merge our children? We could just concatenate the lists and sort them, or we

can do the merge that mergesort uses.

  • To handle a query:
  • Consider the O(log n) segments that our range decomposes into. We want to find the

smallest number in these segments which is ≥ x.

  • This is just the minimum over each of these segments.
  • In each segment, binary search to find the minimum element in the segment which is ≥ x.
  • Overall, O(log2 n) per query.

Takeaway: we can store O segment size information for each segment! If we use sets instead of lists, we can support point updates as well!

2

slide-8
SLIDE 8

Problem 1 – Solution

At each node, store a sorted list of the elements in that segment.

  • How big are all our arrays in total? O(n log n)
  • How to merge our children? We could just concatenate the lists and sort them, or we

can do the merge that mergesort uses.

  • To handle a query:
  • Consider the O(log n) segments that our range decomposes into. We want to find the

smallest number in these segments which is ≥ x.

  • This is just the minimum over each of these segments.
  • In each segment, binary search to find the minimum element in the segment which is ≥ x.
  • Overall, O(log2 n) per query.

Takeaway: we can store O(segment size) information for each segment! If we use sets instead of lists, we can support point updates as well!

2

slide-9
SLIDE 9

Problem 2 – Tree queries

Given a tree with a value at each node, answer these queries quickly:

  • 1. Set the value of every node in v’s subtree to x.
  • 2. Get the sum of values in v’s subtree.

Our segment tree only works on arrays! Idea: just convert the tree into an array!

3

slide-10
SLIDE 10

Problem 2 – Tree queries

Given a tree with a value at each node, answer these queries quickly:

  • 1. Set the value of every node in v’s subtree to x.
  • 2. Get the sum of values in v’s subtree.

Our segment tree only works on arrays! Idea: just convert the tree into an array!

3

slide-11
SLIDE 11

Euler tour on a tree – subtree queries

List nodes in pre-order traversal, then subtree = subarray! first(u) = pre-order number, last(u) = pre-order number + subtree size - 1

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

4

slide-12
SLIDE 12

Problem 2 – Solution

Flatten the tree into its Euler tour.

  • Set the value of each node in a subtree: range assignment to the subtree’s segment
  • Get the sum: range query

5

slide-13
SLIDE 13

Euler tour on a tree – LCA

List node every time you enter and exit on a “tour” then LCA is easy! All we need to do is take the minimum element from first(u) to last(v). [1, 2, 3, 3, 2, 4, 4, 2, 1, 5, 6, 7, 7, 6, 8, 8, 6, 5, 1, 9, 10, 10, 9, 1] Note the node labels need to be the preorder labels.

6

slide-14
SLIDE 14

Problem 3 – Path queries

Given a tree T with edge weights, support the following queries:

  • 1. Query for the distance from node x to the root.
  • 2. Add weight w to an edge.

7

slide-15
SLIDE 15

Problem 3 – Solution

Euler tour segment tree!

  • First, fill the leaves of the segment tree with the initial distance to root (we can

compute this e.g. with DFS).

  • Distance from node to root = point query on segment tree
  • Edge update:
  • If we add weight w to the edge (u, parent(u)), the distance to root for everything in u’s

subtree will increase by w, and everything else is unchanged.

  • Perform a range update of +w to the segment representing u’s subtree.

8

slide-16
SLIDE 16

Problem 4 – Path queries, harder

Given a tree T with edge weights, support the following queries:

  • 1. Query for the distance between nodes u and v.
  • 2. Add weight w to an edge.

9

slide-17
SLIDE 17

Problem 4 – Solution

Just observe that dist(u, v) = dist(u, root) + dist(v, root) − 2 · dist(lca(u, v), root). Note that it’s hard to query for e.g. max weight on path, since we don’t have the equivalent of −.

10

slide-18
SLIDE 18

Line sweeps

Merge these overlapping intervals into bigger, non-overlapping intervals!

11

slide-19
SLIDE 19

1D line sweep – Solution

Imagine sweeping a vertical line from leħt to right.

12

slide-20
SLIDE 20

1D line sweep – Solution

Imagine sweeping a vertical line from leħt to right.

12

slide-21
SLIDE 21

1D line sweep – Solution

Imagine sweeping a vertical line from leħt to right.

12

slide-22
SLIDE 22

1D line sweep – Solution

Imagine sweeping a vertical line from leħt to right.

12

slide-23
SLIDE 23

1D line sweep – Solution

Add an “event” for each interval endpoint. Process each event in order of increasing x-coordinate (“sweeping” the line leħt to right). Maintain a variable n storing the number of intervals that are currently under our sweep line.

  • If we reach a “close interval” event, decrement n.
  • If we reach an “open interval” event, increment n.

State transitions:

  • n goes from 0 to positive → start a new interval
  • n goes from positive to 0 → end the current interval

Be careful when handling events at the same position!

13

slide-24
SLIDE 24

Problem 5 – Intersecting rectangles

Input: N axis-aligned rectangles. Output: whether any of the rectangle boundaries intersect.

Source: North American Invitational Programming Contest 2019

14

slide-25
SLIDE 25

Problem 5 – Solution

Sweep from leħt to right. Each “event” is a vertical rectangle border. Maintain a segment tree storing the number of horizontal lines on that y-coordinate.

  • “Leħt boundary” event (yl, yu)
  • Check if the range sum [yl, yu] is > 0—if so, intersection
  • Add +1 to each of the two endpoints
  • “Right boundary” event (yl, yu)
  • Add −1 to each of the two endpoints
  • Check if the range sum [yl, yu] is > 0—if so, intersection

15

slide-26
SLIDE 26

Problem 6 – Area

Input: N axis-aligned rectangles. Output: the total area of the union of the rectangles (i.e. how much paint to cover all the rectangles).

16

slide-27
SLIDE 27

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-28
SLIDE 28

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-29
SLIDE 29

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-30
SLIDE 30

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-31
SLIDE 31

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-32
SLIDE 32

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-33
SLIDE 33

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-34
SLIDE 34

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-35
SLIDE 35

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-36
SLIDE 36

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-37
SLIDE 37

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-38
SLIDE 38

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-39
SLIDE 39

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-40
SLIDE 40

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-41
SLIDE 41

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-42
SLIDE 42

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-43
SLIDE 43

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-44
SLIDE 44

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-45
SLIDE 45

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-46
SLIDE 46

Problem 6 – Solution

Imagine sweeping a line from leħt to right.

17

slide-47
SLIDE 47

Problem 6 – Solution

Maintain a segment tree storing the number of rectangles covering a position (which we can use to compute the total length covered by rectangles). Events: leħt and right boundaries of each rectangle. Actions:

  • Leħt bound of rectangle → insert segment
  • Right bound → remove segment
  • Before events at x, add ∆x·(total covered length) to area

18

slide-48
SLIDE 48

Sparse segment trees

What if the coordinates in the problem are really big (say, up to 1018)? We can’t make a segment tree of this size! Observation: O(log n) segments accessed per query/update, so most of the segments in the big segment tree are “useless”. Instead of preallocating a big array of nodes, only create nodes when we need to recurse down onto them.

19

slide-49
SLIDE 49

Some things we didn’t talk about

  • 2D/3D segment trees
  • Persistent segment trees
  • Other range-query structures (square root decomposition, binary indexed trees)
  • Dynamic structures (splay tree, treap, link cut tree)
  • Abuse of binary search trees

20