Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Segment Tree Basics
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
halfrost
January 15, 2020
Programming
1.1k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Segment Tree Basics
halfrost
January 15, 2020
More Decks by halfrost
See All by halfrost
Redis multi-data center two-way synchronization
halfrost
0
770
Redis design ideas and usage specifications
halfrost
0
600
Golang message streaming practice in Eleme
halfrost
0
570
SQL practical optimization
halfrost
0
500
Fundamentals of Cryptography
halfrost
0
420
The practice of spatial index in geographic service
halfrost
0
500
Getting started with Machine Learning
halfrost
0
370
Functional Reactive Programming
halfrost
0
430
Eleme Report
halfrost
1
490
Other Decks in Programming
See All in Programming
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
480
フロントエンドUIフレームワークのこれまでとこれから
ssssota
5
2.9k
Kiroで創り、AgentCoreで繋ぐ!AWSで実践する「AI-DLC」から「AIエージェント統合」までの最新地図
licux
4
740
速習iPhone Duo対応
yuukiw00w
1
710
仕様駆動開発による爆速プロダクト開発 / Bakusoku Spec Driven Development
kobakei
0
120
SREの越境 / SRE Collaboration
y0hgi
2
270
ゲームコントローラやキーボードのファームウェアをSwiftで書く
kishikawakatsumi
1
250
App Intentsのビルドプロセスを支える技術
kntkymt
0
420
モデルのリファクタリングが難しいと思ったら、そもそも複雑だったのはビジネス仕様だった ? / is-the-business-domain-the-real-complexity
hatsu38
0
390
モバイル交通系ICへのチャージ実例から考える、クロスプラットフォーム開発におけるiOS実機テスト設計とCI運用
yusuga
1
530
iOSDC Japan 2026 - Swiftで作って学ぼう!データベース自作入門
kaseken
2
310
AI Agent時代のリアーキテクチャ戦略と実践
hokaccha
9
4.9k
Featured
See All Featured
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.3k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
1k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
700
Typedesign – Prime Four
hannesfritz
42
3.2k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
2k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
240
The Curse of the Amulet
leimatthew05
3
15k
From π to Pie charts
rasagy
1
380
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
200
Building an army of robots
kneath
307
46k
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