<<

Segment II Jingbo Shang University of Illinois, Urbana-Champaign Feb 12, 2018 Outlines

• Warmup: Range Minimum Query • Complex Query • K-th Smallest Element Problem • Static à Dynamic • Segment Tree + Other • Persistent Segment Tree • Lowest Common Ancestor • � ����� − �(����) • Euler Tour Order Outlines

• Warmup: Range Minimum Query • Complex Query • K-th Smallest Element Problem • Static à Dynamic • Segment Tree + Other Data Structure • Persistent Segment Tree • Lowest Common Ancestor • � ����� − �(����) • Euler Tour Order Range Minimum Query

• A fixed array �[1. . �] • � queries • What is the minimum value in A[�. .�]

• 2 parts of complexity • Pre computation • Online query • Bruteforce • � 1 − � � • � � − �(1) Segment Tree

• Recall the construction and interval query we introduced last lecture…

• � � − � ���� Sparse Table

• ��[�][�] = min �[�. . � + 2 − 1] • ��[�][�] = min �� � � − 1 ,�� � + 2 [� − 1]

• 1 ≤ � ≤ � • 0 ≤ � ≤ log (�)

• � ����� − �(1) Outlines

• Warmup: Range Minimum Query • Complex Query • K-th Smallest Element Problem • Static à Dynamic • Segment Tree + Other Data Structure • Persistent Segment Tree • Lowest Common Ancestor • � ����� − �(����) • Euler Tour Order Complex Query: SPOJ GSS1 Maintain Information for Complex Query

• Within an interval [�, �] • maxSum: Maximum Sum of Subsequences • leftSum: Maximum Sum of Subsequences starting from � • rightSum: Maximum Sum of Subsequences ending at � • sum: the sum of elements in this interval • How to merge the information from [�, �], [� + 1, �]? • maxSum = max{L.maxSum, R.maxSum, L.rightSum + R.leftSum} • leftSum = max{L.leftSum, L.sum + R.leftSum} • rightSum = max{R.rightSum, R.sum + L.rightSum} • sum = L.sum + R.sum K-th smallest element (static)

• In each interval • Restore the sorted array of numbers

• Binary search for answer • Query on Segment Tree • Binary search to find the ranking of the current answer within current interval • �(�����) K-th smallest element (dynamic)

• Replace the sorted array as a balanced binary maintaining the subtree size • Segment Tree Nest Balanced • �(�����) Persistent Segment Tree

• Handling historical queries • Concepts of timestamps • After each modification, we have to take a snapshot of the current segment tree, and keep going. • During query phase, we will ask you questions about the previous snapshots of segment trees. • Share memories to make it efficient. • A single modification only affects �(����) nodes • �(�����) memory, instead of �(�). K-th smallest element

• Still binary search for answer • For the count, we can use the Persistent Segment Tree

• Build the Segment Tree based on the values, instead of positions • Insert numbers from the first to the last • After each insertion, it becomes a new timestamp • Query both L-th and R-th trees to know how many numbers are smaller than the current answer, which takes �(����) time • �(�����) Outlines

• Warmup: Range Minimum Query • Complex Query • K-th Smallest Element Problem • Static à Dynamic • Segment Tree + Other Data Structure • Persistent Segment Tree • Lowest Common Ancestor • � ����� − �(����) • Euler Tour Order Lowest Common Ancestor

• A rooted tree � of � nodes • � queries • nodes � and �, find the furthest (i.e., lowest) node from the root that is an ancestor for both � and �. Lowest Common Ancestor Tree-Depth Decompose

• � � − �( �)

• Simple level-decomposition

• Jump Table • ����[�][�] is the node 2 levels above � • ����[�][0] is its parent • ����[�][�] = ����[����[�][� − 1]][� − 1] • Similar to ST Table!

• � ����� − �(����) Dynamic Programming

• Jump Table • ����[�][�] is the node 2 levels above � • ����[�][0] is its parent

• �, �, assume ����ℎ � ≥ ����ℎ[�] • First jump from � to ����ℎ[�]-level (depth jump) • Jump from � and � at the same with the same 2 step size • � from log � downto 1 • Move to there if they are different • Special case when � = � after the depth jump From LCA to RMQ

• Euler Tour • 2�-length From LCA to RMQ

• LCA(�,�) • The first positions of � and �: �(�) and �(�) • The closest node from the root between �(�) and �(�) • à RMQ problem • Segment Tree: �(�) − � ���� • ST Table: � ����� − �(1) From RMQ to LCA

• RMQ à construct à +/- 1 RMQ • �(�) − �(1) • See more in the cited tutorial in the end

• Not so useful in contests Questions?

• Homework • Complex Query: • SPOJ GSS3 (1) • POJ 3468 (2) • LCA & RMQ • SPOJ RMQSQ (1) • SPOJ LCA (2) • K-th Smallest Number • POJ 2104 (4) • ZOJ 2112 (6) • Reference • Topcoder Tutorial • https://www.topcoder.com/community/data-science/data-science-tutorials/range-minimum- query-and-lowest-common-ancestor/