Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Segment Tree Basics
Search
halfrost
January 15, 2020
Programming
0
970
Segment Tree Basics
halfrost
January 15, 2020
Tweet
Share
More Decks by halfrost
See All by halfrost
Redis multi-data center two-way synchronization
halfrost
0
620
Redis design ideas and usage specifications
halfrost
0
460
Golang message streaming practice in Eleme
halfrost
0
440
SQL practical optimization
halfrost
0
420
Fundamentals of Cryptography
halfrost
0
330
The practice of spatial index in geographic service
halfrost
0
430
Getting started with Machine Learning
halfrost
0
320
Functional Reactive Programming
halfrost
0
330
Eleme Report
halfrost
1
400
Other Decks in Programming
See All in Programming
Querying Design System デザインシステムの意思決定を支える構造検索
ikumatadokoro
1
1.2k
競馬で学ぶ機械学習の基本と実践 / Machine Learning with Horse Racing
shoheimitani
14
13k
イベントストーミングのはじめかた / Getting Started with Event Storming
nrslib
1
630
Eloquentを使ってどこまでコードの治安を保てるのか?を新人が考察してみた
itokoh0405
0
3.2k
ソフトウェア設計の課題・原則・実践技法
masuda220
PRO
20
10k
Stay Hacker 〜九州で生まれ、Perlに出会い、コミュニティで育つ〜
pyama86
2
1.9k
問題の見方を変える「システム思考」超入門
panda_program
0
300
CloudflareのSandbox SDKを試してみた
syumai
0
170
What's New in Web AI?
christianliebel
PRO
0
130
早すぎ?超先読み Go 1.26 Draft - Preview the contents of the Go 1.26 Draft Release Notes
tomtwinkle
0
350
OSS開発者の憂鬱
yusukebe
12
4.6k
歴史から学ぶ「Why PHP?」 PHPを書く理由を改めて理解する / Learning from History: “Why PHP?” Rediscovering the Reasons for Writing PHP
seike460
PRO
0
160
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
355
21k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.5k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.1k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
Building a Scalable Design System with Sketch
lauravandoore
463
33k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
YesSQL, Process and Tooling at Scale
rocio
174
15k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.8k
Stop Working from a Prison Cell
hatefulcrawdad
272
21k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
670
GraphQLとの向き合い方2022年版
quramy
49
14k
Transcript
Segment Tree 基础篇
Range Minimum/Maximum Query •需求:在饿了么的⼀个⽤户的历史订单中,找出该⽤户指定⼀段 时间内单笔订单最贵/最便宜的订单 One problem •O(n), when query
K —> ∞ times ?
Range Minimum/Maximum Query •需求:在饿了么的⼀个⽤户的历史订单中,求出找出该⽤户指定 ⼀段时间内累积消费总⾦额 Another problem •O(n), when query
K —> ∞ times ? •prefixSum, time O(n), query O(1)
Range Minimum/Maximum Query •需求:在 12306 ⽹站上,统计⼀条线路上任意指定两个站点之间 的可售出的票数总和 One more thing
•O(n), when query K —> ∞ times ? •prefixSum, time O(n), update O(n), query O(1)
How to ? Range Minimum/Maximum Query 5 O(log n) or
O(1) ?
Presentation agenda 6 What How to use Leetcode example What
is segment tree 2 3 1 How Example
Define Segment Tree 7 1. ❌ Complete Binary Tree 2.
∈ Balanced Binary Tree 3. As Full binary tree
How many node Segment Tree 8 0-level : 1 1-level
: 2 2-level : 4 3-level : 8 4-level : 16 (n-1)—level :2^(n-1) n-level:2^n Σ 0-(n-1) level:Σ = 2^n - 1 ≈ n-level => 2n Mathematical Induction:Σ = 4n (n = 2^k or n = 2^k + 1)
How many node Segment Tree 9 •When n≥3, [1,n] segment
split [1,n] into 2*⌊log (n-1)⌋ sub- Interval •4n ≥ 2*⌊log (n-1)⌋ <= 2n ≥ log (n-1) <= 4^n ≥ n-1
How to create Segment Tree 10
How to query Segment Tree 11 ❗
How to update Segment Tree 12 ❗
How to update Segment Tree 13 •Update [2,3] ? •❌
O(n)
How to update Segment Tree 14 •Update [2,3] ? •✅O(log
n)
How to update Segment Tree 15
How to query Segment Tree 16 •push_down
How to update Segment Tree 17 •push_down + push_up
How to low space Segment Tree 18 •Leetcode 715
Compared ST VS Array 19 20% 80% 60% 40% 2585
785 Update: Array O(n) | Segment Tree O(log n) Query: Array O(n) | Segment Tree O(log n)
Segment Tree 20 Example
Segment Tree 21 Example
Segment Tree 22 Example
Boyer-Moore Majority Vote Algorithm 23 Example
Segment Tree 24 Example
Segment Tree 25 Example •judge threshold •count = map[1:[0 1
4 5] 2:[2 3]] •eg: 1 in [0,5] count , twice binary search, find [lowerBound, upperBound) •(upperBound - lowerBound) ≥ threshold
Segment Tree 26 •Leetcode 327. Count of Range Sum •Leetcode
715. Range Module •Leetcode 699. Falling Squares & 732. My Calendar III •Leetcode 850. Rectangle Area II •Leetcode 218. The Skyline Problem Exercise
Advance 27 15% 35% 50% •One point update (update: min/change,
query: sum/min) •Interval update (update: min/change, query: sum/hash) •Discretization •Interval merge •Scan line •Binary Index Tree