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
1k
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
630
Redis design ideas and usage specifications
halfrost
0
480
Golang message streaming practice in Eleme
halfrost
0
460
SQL practical optimization
halfrost
0
440
Fundamentals of Cryptography
halfrost
0
340
The practice of spatial index in geographic service
halfrost
0
440
Getting started with Machine Learning
halfrost
0
330
Functional Reactive Programming
halfrost
0
340
Eleme Report
halfrost
1
420
Other Decks in Programming
See All in Programming
生成AIを利用するだけでなく、投資できる組織へ
pospome
2
450
Honoを使ったリモートMCPサーバでAIツールとの連携を加速させる!
tosuri13
1
130
Implementation Patterns
denyspoltorak
0
170
大規模Cloud Native環境におけるFalcoの運用
owlinux1000
0
250
生成AI時代を勝ち抜くエンジニア組織マネジメント
coconala_engineer
0
40k
Giselleで作るAI QAアシスタント 〜 Pull Requestレビューに継続的QAを
codenote
0
340
これならできる!個人開発のすゝめ
tinykitten
PRO
0
150
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
220
.NET Conf 2025 の興味のあるセッ ションを復習した / dotnet conf 2025 quick recap for backend engineer
tomohisa
0
110
コントリビューターによるDenoのすゝめ / Deno Recommendations by a Contributor
petamoriken
0
170
從冷知識到漏洞,你不懂的 Web,駭客懂 - Huli @ WebConf Taiwan 2025
aszx87410
2
3.4k
まだ間に合う!Claude Code元年をふりかえる
nogu66
5
940
Featured
See All Featured
Designing Experiences People Love
moore
143
24k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.6k
The Cost Of JavaScript in 2023
addyosmani
55
9.4k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
120
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.1k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.9k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.8k
4 Signs Your Business is Dying
shpigford
187
22k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
61
48k
Raft: Consensus for Rubyists
vanstee
141
7.3k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
1
100
Prompt Engineering for Job Search
mfonobong
0
140
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