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

Post on 22-Aug-2020

17 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

SegmentTreeIIJingboShang

UniversityofIllinois,Urbana-ChampaignFeb12,2018

Outlines

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

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

Outlines

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

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

RangeMinimumQuery

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

• 2partsofcomplexity• Precomputation• Onlinequery

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

SegmentTree

• Recalltheconstructionandintervalqueryweintroducedlastlecture…

• 𝑂 𝑛 − 𝑂 𝑙𝑜𝑔𝑛

SparseTable

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

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

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

Outlines

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

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

ComplexQuery:SPOJGSS1

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

K-th smallestelement(static)

• Ineachinterval• Restorethesortedarrayofnumbers

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

K-th smallestelement(dynamic)

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

PersistentSegmentTree

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

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

K-th smallestelement

• Stillbinarysearchforanswer• Forthecount,wecanusethePersistentSegmentTree

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

Outlines

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

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

LowestCommonAncestor

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

LowestCommonAncestor

Tree-DepthDecompose

• 𝑂 𝑛 − 𝑂( 𝑛)

• Simplelevel-decomposition

DynamicProgramming

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

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

• SimilartoSTTable!

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

DynamicProgramming

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

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

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

• Specialcasewhen𝑢 = 𝑣 afterthedepthjump

FromLCAtoRMQ

• EulerTour• 2𝑛-length

FromLCAtoRMQ

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

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

FromRMQtoLCA

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

• Notsousefulincontests

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/

top related