2019 10 Cornell Cs5199 Segment Trees
2019 10 Cornell Cs5199 Segment Trees
Raunak Kumar
Based on slides by Paul Liu, Kuba Karpierz, Bruno Vacherot, Raunak Kumar
and Jason Chiu for CPSC 490 – Problem Solving in Computer Science at The
University of British Columbia
2019/10/07
Cornell University
Data Structures
1
Data Structures
1
Data Structures
1
Range Query
2
Range Query
2
Range Query
2
Range Query with Updates
3
Potential Solutions
4
Potential Solutions
4
Potential Solutions
4
Range Query
5
Segment Trees
• Segment tree: group every pair together, then every pair of pairs,
then every pair of that, until you get 1 node
Source: http://scvalex.github.io/articles/SegmentTree.html
6
Segment Trees – Structure, Update
Structure
Point Update
• Update leaf
• Update ancestors
7
Segment Trees (Sum Query) – Structure, Update
Structure
Point Update
8
Segment Trees (Sum Query) – Point Update
9
Segment Trees (Sum Query) – Point Update
9
Segment Trees (Sum Query) – Point Update
9
Segment Trees (Sum Query) – Point Update
9
Segment Trees (Sum Query) – Point Update
9
Segment Trees – Range Query
Range query
10
Segment Trees – Range Query
Range query
10
Segment Trees (Sum Query) – Range Query
11
Segment Trees (Sum Query) – Range Query
11
Segment Trees (Sum Query) – Range Query
11
Segment Trees (Sum Query) – Range Query
11
Segment Trees (Sum Query) – Range Query
11
Segment Trees (Sum Query) – Range Query
11
Segment Trees (Sum Query) – Range Query
11
Segment Trees (Sum Query) – Range Query
11
Segment Tree - Construction
12
Segment Tree - Implementation
13
Segment Tree - Initialize
14
Segment Tree - Build
15
Segment Tree - Point Update
16
Segment Tree - Range Query
17
Problem 1
18
Problem 1 - Solution
Consider a range [l, r]. How could we get the maximum sum subarray
in this range?
• The answer lies entirely in the left child or entirely in the right.
• The answer could spans the left and right children.
• So ans(l, r) = max{ans(left), ans(right), sum(across)}.
19
Problem 1 - Solution
Consider a range [l, r]. How could we get the maximum sum subarray
in this range?
• The answer lies entirely in the left child or entirely in the right.
• The answer could spans the left and right children.
• So ans(l, r) = max{ans(left), ans(right), sum(across)}.
How do we get the maximum sum subarray that goes across the left
and right children?
19
Problem 1 - Solution
Consider a range [l, r]. How could we get the maximum sum subarray
in this range?
• The answer lies entirely in the left child or entirely in the right.
• The answer could spans the left and right children.
• So ans(l, r) = max{ans(left), ans(right), sum(across)}.
How do we get the maximum sum subarray that goes across the left
and right children?
Clearly, we need to store more information in the nodes.
19
Problem 1 - Solution
Update queries
20
Range Updates
21
Range Updates
21
Range Updates
21
Lazy Propagation
Basic idea
22
Lazy Propagation
Basic idea
22
Lazy Propagation – Lazy Update
23
Lazy Propagation – Lazy Update
23
Lazy Propagation – Lazy Update
23
Lazy Propagation – Lazy Update
23
Lazy Propagation – Lazy Update
23
Lazy Propagation – Lazy Update
23
Lazy Propagation – Lazy Update
23
Lazy Propagation – Lazy Update
23
Lazy Propagation – Lazy Update
23
Lazy Propagation – Lazy Update
23
Lazy Propagation – Range Query
24
Lazy Propagation – Range Query
24
Lazy Propagation – Range Query
24
Lazy Propagation – Range Query
24
Lazy Propagation – Range Query
24
Lazy Propagation – Range Query
24
Lazy Propagation – Range Query
24
Lazy Propagation – Range Query
24
Lazy Propagation – Range Query
24
Lazy Propagation – Range Query
24
Feedback
25