segment tree ii - github...• lowest common ancestor • ! "#$%" −!(#$%") •...

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

Upload: others

Post on 22-Aug-2020

17 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

SegmentTreeIIJingboShang

UniversityofIllinois,Urbana-ChampaignFeb12,2018

Page 2: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

Outlines

• Warmup:RangeMinimumQuery• ComplexQuery• K-th SmallestElementProblem• Staticà Dynamic• SegmentTree+OtherDataStructure• PersistentSegmentTree

• LowestCommonAncestor• 𝑂 𝑁𝑙𝑜𝑔𝑁 − 𝑂(𝑙𝑜𝑔𝑁)• EulerTourOrder

Page 3: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

Outlines

• Warmup:RangeMinimumQuery• ComplexQuery• K-th SmallestElementProblem• Staticà Dynamic• SegmentTree+OtherDataStructure• PersistentSegmentTree

• LowestCommonAncestor• 𝑂 𝑁𝑙𝑜𝑔𝑁 − 𝑂(𝑙𝑜𝑔𝑁)• EulerTourOrder

Page 4: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

RangeMinimumQuery

• Afixedarray𝐴[1. . 𝑛]• 𝑚 queries• WhatistheminimumvalueinA[𝑖. . 𝑗]

• 2partsofcomplexity• Precomputation• Onlinequery

• Bruteforce• 𝑂 1 − 𝑂 𝑛• 𝑂 𝑛3 − 𝑂(1)

Page 5: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

SegmentTree

• Recalltheconstructionandintervalqueryweintroducedlastlecture…

• 𝑂 𝑛 − 𝑂 𝑙𝑜𝑔𝑛

Page 6: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

SparseTable

• 𝑆𝑇[𝑖][𝑗] = min 𝐴[𝑖. . 𝑖 + 2< − 1]• 𝑆𝑇[𝑖][𝑗] = min 𝑆𝑇 𝑖 𝑗 − 1 ,𝑆𝑇 𝑖 + 2<>? [𝑗 − 1]

• 1 ≤ 𝑖 ≤ 𝑛• 0 ≤ 𝑗 ≤ log(𝑛)

• 𝑂 𝑛𝑙𝑜𝑔𝑛 − 𝑂(1)

Page 7: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

Outlines

• Warmup:RangeMinimumQuery• ComplexQuery• K-th SmallestElementProblem• Staticà Dynamic• SegmentTree+OtherDataStructure• PersistentSegmentTree

• LowestCommonAncestor• 𝑂 𝑁𝑙𝑜𝑔𝑁 − 𝑂(𝑙𝑜𝑔𝑁)• EulerTourOrder

Page 8: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

ComplexQuery:SPOJGSS1

Page 9: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

MaintainInformationforComplexQuery

• Withinaninterval[𝐿, 𝑅]• maxSum:MaximumSumofSubsequences• leftSum:MaximumSumofSubsequencesstartingfrom𝐿• rightSum:MaximumSumofSubsequencesendingat𝑅• sum:thesumofelementsinthisinterval

• Howtomerge theinformationfrom[𝐿, 𝑚],[𝑚 + 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

Page 10: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

K-th smallestelement(static)

• Ineachinterval• Restorethesortedarrayofnumbers

• Binarysearchforanswer• QueryonSegmentTree• Binarysearchtofindtherankingofthecurrentanswerwithincurrentinterval• 𝑂(𝑄𝑙𝑜𝑔I𝑁)

Page 11: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

K-th smallestelement(dynamic)

• Replacethesortedarrayasabalancedbinarysearchtreemaintainingthesubtreesize• SegmentTreeNestBalancedBinarySearchTree• 𝑂(𝑄𝑙𝑜𝑔I𝑁)

Page 12: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

PersistentSegmentTree

• Handlinghistoricalqueries• Conceptsoftimestamps• Aftereachmodification,wehavetotakeasnapshotofthecurrentsegmenttree,andkeepgoing.• Duringqueryphase,wewillaskyouquestionsabouttheprevioussnapshotsofsegmenttrees.

• Sharememoriestomakeitefficient.• Asinglemodificationonlyaffects𝑂(𝑙𝑜𝑔𝑁) nodes• 𝑂(𝑄𝑙𝑜𝑔𝑁)memory,insteadof𝑂(𝑁).

Page 13: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

K-th smallestelement

• Stillbinarysearchforanswer• Forthecount,wecanusethePersistentSegmentTree

• BuildtheSegmentTreebasedonthevalues,insteadofpositions• Insertnumbersfromthefirsttothelast• Aftereachinsertion,itbecomesanewtimestamp• QuerybothL-th andR-th treestoknowhowmanynumbersaresmallerthanthecurrentanswer,whichtakes𝑂(𝑙𝑜𝑔𝑁) time• 𝑂(𝑄𝑙𝑜𝑔3𝑁)

Page 14: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

Outlines

• Warmup:RangeMinimumQuery• ComplexQuery• K-th SmallestElementProblem• Staticà Dynamic• SegmentTree+OtherDataStructure• PersistentSegmentTree

• LowestCommonAncestor• 𝑂 𝑁𝑙𝑜𝑔𝑁 − 𝑂(𝑙𝑜𝑔𝑁)• EulerTourOrder

Page 15: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

LowestCommonAncestor

• Arooted tree𝑇 of𝑛 nodes• 𝑚 queries• nodes𝑢 and𝑣,findthefurthest(i.e.,lowest) nodefromtherootthatisanancestorforboth 𝑢 and𝑣.

Page 16: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

LowestCommonAncestor

Page 17: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

Tree-DepthDecompose

• 𝑂 𝑛 − 𝑂( 𝑛)

• Simplelevel-decomposition

Page 18: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

DynamicProgramming

• JumpTable• 𝐽𝑢𝑚𝑝[𝑖][𝑗] isthenode2< levelsabove𝑖

• 𝐽𝑢𝑚𝑝[𝑖][0] isitsparent• 𝐽𝑢𝑚𝑝[𝑖][𝑗] = 𝐽𝑢𝑚𝑝[𝐽𝑢𝑚𝑝[𝑖][𝑗 − 1]][𝑗 − 1]

• SimilartoSTTable!

• 𝑂 𝑛𝑙𝑜𝑔𝑛 − 𝑂(𝑙𝑜𝑔𝑛)

Page 19: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

DynamicProgramming

• JumpTable• 𝐽𝑢𝑚𝑝[𝑖][𝑗] isthenode2< levelsabove𝑖

• 𝐽𝑢𝑚𝑝[𝑖][0] isitsparent

• 𝑢, 𝑣,assume𝑑𝑒𝑝𝑡ℎ 𝑢 ≥ 𝑑𝑒𝑝𝑡ℎ[𝑣]• Firstjumpfrom𝑢 to𝑑𝑒𝑝𝑡ℎ[𝑣]-level(depthjump)• Jumpfrom𝑢 and𝑣 atthesamewiththesame2< stepsize• 𝑗 fromlog𝑛 downto 1• Movetothereiftheyaredifferent

• Specialcasewhen𝑢 = 𝑣 afterthedepthjump

Page 20: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

FromLCAtoRMQ

• EulerTour• 2𝑛-length

Page 21: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

FromLCAtoRMQ

• LCA(𝑢,𝑣)• Thefirstpositionsof𝑢 and𝑣: 𝑝(𝑢) and𝑝(𝑣)• Theclosestnodefromtherootbetween𝑝(𝑢)and𝑝(𝑣)• à RMQproblem

• SegmentTree:𝑂(𝑛) − 𝑂 𝑙𝑜𝑔𝑛• STTable:𝑂 𝑛𝑙𝑜𝑔𝑛 − 𝑂(1)

Page 22: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

FromRMQtoLCA

• RMQà constructCartesianTreeà +/- 1RMQ• 𝑂(𝑛) − 𝑂(1)• Seemoreinthecitedtutorialintheend

• Notsousefulincontests

Page 23: Segment Tree II - GitHub...• Lowest Common Ancestor • ! "#$%" −!(#$%") • Euler Tour Order. Complex Query: SPOJ GSS1. Maintain Information for Complex Query • Within an interval

Questions?

• Homework• ComplexQuery:

• SPOJGSS3 (1)• POJ3468 (2)

• LCA&RMQ• SPOJRMQSQ (1)• SPOJLCA (2)

• K-th SmallestNumber• POJ2104 (4)• ZOJ2112 (6)

• Reference• Topcoder Tutorial

• https://www.topcoder.com/community/data-science/data-science-tutorials/range-minimum-query-and-lowest-common-ancestor/