cpsc 490 problem solving in computer science
play

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


  1. 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

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

  3. 2 n per query. 2 x . If we use sets instead of lists, we can support point updates as well! Takeaway: we can store O segment size information for each segment! • Overall, O x . • In each segment, binary search to find the minimum element in the segment which is • This is just the minimum over each of these segments. smallest number in these segments which is At each node, store a sorted list of the elements in that segment. n segments that our range decomposes into. We want to find the • Consider the O • To handle a query: can do the merge that mergesort uses. • How to merge our children? We could just concatenate the lists and sort them, or we n O n • How big are all our arrays in total? Problem 1 – Solution

  4. 2 n per query. 2 • This is just the minimum over each of these segments. If we use sets instead of lists, we can support point updates as well! Takeaway: we can store O segment size information for each segment! • Overall, O x . • In each segment, binary search to find the minimum element in the segment which is x . At each node, store a sorted list of the elements in that segment. smallest number in these segments which is n segments that our range decomposes into. We want to find the • Consider the O • To handle a query: can do the merge that mergesort uses. • How to merge our children? We could just concatenate the lists and sort them, or we Problem 1 – Solution • How big are all our arrays in total? O ( n log n )

  5. 2 n per query. 2 x . If we use sets instead of lists, we can support point updates as well! Takeaway: we can store O segment size information for each segment! • Overall, O x . • In each segment, binary search to find the minimum element in the segment which is • This is just the minimum over each of these segments. smallest number in these segments which is At each node, store a sorted list of the elements in that segment. n segments that our range decomposes into. We want to find the • Consider the O • To handle a query: can do the merge that mergesort uses. We could just concatenate the lists and sort them, or we • How to merge our children? Problem 1 – Solution • How big are all our arrays in total? O ( n log n )

  6. 2 n per query. 2 • This is just the minimum over each of these segments. If we use sets instead of lists, we can support point updates as well! Takeaway: we can store O segment size information for each segment! • Overall, O x . • In each segment, binary search to find the minimum element in the segment which is x . At each node, store a sorted list of the elements in that segment. smallest number in these segments which is n segments that our range decomposes into. We want to find the • Consider the O • To handle a query: can do the merge that mergesort uses. • How to merge our children? We could just concatenate the lists and sort them, or we Problem 1 – Solution • How big are all our arrays in total? O ( n log n )

  7. At each node, store a sorted list of the elements in that segment. • 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: • This is just the minimum over each of these segments. 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 Problem 1 – Solution • How big are all our arrays in total? O ( n log n ) • Consider the O (log n ) segments that our range decomposes into. We want to find the smallest number in these segments which is ≥ x . • In each segment, binary search to find the minimum element in the segment which is ≥ x . • Overall, O (log 2 n ) per query.

  8. At each node, store a sorted list of the elements in that segment. • 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: • This is just the minimum over each of these segments. If we use sets instead of lists, we can support point updates as well! 2 Problem 1 – Solution • How big are all our arrays in total? O ( n log n ) • Consider the O (log n ) segments that our range decomposes into. We want to find the smallest number in these segments which is ≥ x . • In each segment, binary search to find the minimum element in the segment which is ≥ x . • Overall, O (log 2 n ) per query. Takeaway: we can store O ( segment size ) information for each segment!

  9. 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 Problem 2 – Tree queries

  10. 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 Problem 2 – Tree queries

  11. List nodes in pre-order traversal, then subtree = subarray! [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 4 Euler tour on a tree – subtree queries first ( u ) = pre-order number, last ( u ) = pre-order number + subtree size - 1

  12. • Set the value of each node in a subtree: range assignment to the subtree’s segment Flatten the tree into its Euler tour. • Get the sum: range query 5 Problem 2 – Solution

  13. List node every time you enter and exit on a “tour” then LCA is easy! [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 Euler tour on a tree – LCA All we need to do is take the minimum element from first ( u ) to last ( v ) .

  14. 1. Query for the distance from node x to the root. Given a tree T with edge weights, support the following queries: 2. Add weight w to an edge. 7 Problem 3 – Path queries

  15. 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). • Edge update: subtree will increase by w , and everything else is unchanged. 8 Problem 3 – Solution • Distance from node to root = point query on segment tree • If we add weight w to the edge ( u , parent ( u )) , the distance to root for everything in u ’s • Perform a range update of + w to the segment representing u ’s subtree.

  16. 1. Query for the distance between nodes u and v . Given a tree T with edge weights, support the following queries: 2. Add weight w to an edge. 9 Problem 4 – Path queries, harder

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

  18. 11 Merge these overlapping intervals into bigger, non-overlapping intervals! Line sweeps

  19. 12 Imagine sweeping a vertical line from leħt to right. 1D line sweep – Solution

  20. 12 Imagine sweeping a vertical line from leħt to right. 1D line sweep – Solution

  21. 12 Imagine sweeping a vertical line from leħt to right. 1D line sweep – Solution

  22. 12 Imagine sweeping a vertical line from leħt to right. 1D line sweep – Solution

  23. 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: Be careful when handling events at the same position! 13 1D line sweep – Solution • n goes from 0 to positive → start a new interval • n goes from positive to 0 → end the current interval

  24. Source: North American Invitational Programming Contest 2019 14 Problem 5 – Intersecting rectangles Input : N axis-aligned rectangles. Output : whether any of the rectangle boundaries intersect.

  25. 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. 15 Problem 5 – Solution • “Leħt boundary” event ( y l , y u ) • Check if the range sum [ y l , y u ] is > 0—if so, intersection • Add + 1 to each of the two endpoints • “Right boundary” event ( y l , y u ) • Add − 1 to each of the two endpoints • Check if the range sum [ y l , y u ] is > 0—if so, intersection

  26. rectangles). 16 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

  27. 17 Imagine sweeping a line from leħt to right. Problem 6 – Solution

  28. 17 Imagine sweeping a line from leħt to right. Problem 6 – Solution

  29. 17 Imagine sweeping a line from leħt to right. Problem 6 – Solution

  30. 17 Imagine sweeping a line from leħt to right. Problem 6 – Solution

Recommend


More recommend