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
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
720
Redis design ideas and usage specifications
halfrost
0
550
Golang message streaming practice in Eleme
halfrost
0
540
SQL practical optimization
halfrost
0
480
Fundamentals of Cryptography
halfrost
0
380
The practice of spatial index in geographic service
halfrost
0
470
Getting started with Machine Learning
halfrost
0
360
Functional Reactive Programming
halfrost
0
400
Eleme Report
halfrost
1
460
Other Decks in Programming
See All in Programming
なぜ型を書くのか? TSKaigi2026で改めて考える #tskaigi_smarthr
kajitack
0
190
分散システム、なんですぐ死んでしまうん?耐障害性を高めたいあなたのためのレジリエンスパターン入門
mshibuya
2
750
Webフレームワークの ベンチマークについて
yusukebe
0
190
「なぜそう決めたのか」を残し続ける仕組み ― Notion AI カスタムエージェント × Slack連携による設計判断の自動記録 - NIKKEI Tech Talk #47
niftycorp
PRO
0
250
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
820
Creating Composable Callables in Contemporary C++
rollbear
0
180
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
230
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
280
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
230
AI がコードを書く時代における新卒エンジニアの仕事風景 (2026) / New Graduate Engineers in the Era of AI Coding (2026)
sushichan044
0
190
20260623_Loop Engineeringで自分の分身の問い合わせBotを作る
ryugen04
0
110
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
3
820
Featured
See All Featured
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
220
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
Tell your own story through comics
letsgokoyo
1
980
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
210
Making the Leap to Tech Lead
cromwellryan
135
10k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
640
It's Worth the Effort
3n
188
29k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
What does AI have to do with Human Rights?
axbom
PRO
1
2.2k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
240
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
2
280
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.2k
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