Data Structures
-
Upload
thahir-shah -
Category
Documents
-
view
6 -
download
0
description
Transcript of Data Structures
![Page 1: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/1.jpg)
Data structures
Bjarki Ágúst GuðmundssonTómas Ken Magnússon
School of Computer ScienceReykjavík University
Árangursrík forritun og lausn verkefna
![Page 2: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/2.jpg)
Today we’re going to cover
▶ Review the Union-Find data structure, and look atapplications
▶ Study range queries▶ Learn about Segment Trees
2
![Page 3: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/3.jpg)
Union-Find
▶ We have n items▶ Maintains a collection of disjoint sets▶ Each of the n items is in exactly one set
▶ items = {1, 2, 3, 4, 5, 6}▶ collections = {1, 4}, {3, 5, 6}, {2}▶ collections = {1}, {2}, {3}, {4}, {5}, {6}
▶ Supports two operations efficiently: find(x) andunion(x,y).
3
![Page 4: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/4.jpg)
Union-Find
▶ items = {1, 2, 3, 4, 5, 6}▶ collections = {1, 4}, {3, 5, 6}, {2}
▶ find(x) returns a representative item from the setthat x is in
– find(1) = 1– find(4) = 1
– find(3) = 5– find(5) = 5– find(6) = 5
– find(2) = 2
▶ a and b are in the same set if and only iffind(a) == find(b)
4
![Page 5: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/5.jpg)
Union-Find
▶ items = {1, 2, 3, 4, 5, 6}▶ collections = {1, 4}, {3, 5, 6}, {2}
▶ union(x, y) merges the set containing x and the setcontaining y together.
– union(4, 2)– collections = {1, 2, 4}, {3, 5, 6}– union(3, 6)– collections = {1, 2, 4}, {3, 5, 6}– union(2, 6)– collections = {1, 2, 3, 4, 5, 6}
5
![Page 6: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/6.jpg)
Union-Find implementation▶ Quick Union with path compression▶ Extremely simple implementation▶ Extremely efficient
struct union_find {vector<int> parent;union_find(int n) {
parent = vector<int>(n);for (int i = 0; i < n; i++) {
parent[i] = i;}
}
// find and union};
6
![Page 7: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/7.jpg)
Union-Find implementation// find and union
int find(int x) {if (parent[x] == x) {
return x;} else {
parent[x] = find(parent[x]);return parent[x];
}}
void unite(int x, int y) {parent[find(x)] = find(y);
}
7
![Page 8: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/8.jpg)
Union-Find implementation (short)▶ If you’re in a hurry...
#define MAXN 1000int p[MAXN];
int find(int x) {return p[x] == x ? x : p[x] = find(p[x]); }
void unite(int x, int y) { p[find(x)] = find(y); }
for (int i = 0; i < MAXN; i++) p[i] = i;
8
![Page 9: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/9.jpg)
Union-Find applications
▶ Union-Find maintains a collection of disjoint sets▶ When are we dealing with such collections?▶ Most common example is in graphs
9
![Page 10: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/10.jpg)
Disjoint sets in graphs
12
3
4 5
6
7
▶ items = {1, 2, 3, 4, 5, 6, 7}▶ collections = {1, 4, 7}, {2}, {3, 5, 6}▶ union(2, 5)
10
![Page 11: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/11.jpg)
Disjoint sets in graphs
12
3
4 5
6
7
▶ items = {1, 2, 3, 4, 5, 6, 7}
▶ collections = {1, 4, 7}, {2}, {3, 5, 6}▶ union(2, 5)
10
![Page 12: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/12.jpg)
Disjoint sets in graphs
12
3
4 5
6
7
▶ items = {1, 2, 3, 4, 5, 6, 7}▶ collections = {1, 4, 7}, {2}, {3, 5, 6}
▶ union(2, 5)
10
![Page 13: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/13.jpg)
Disjoint sets in graphs
12
3
4 5
6
7
▶ items = {1, 2, 3, 4, 5, 6, 7}▶ collections = {1, 4, 7}, {2}, {3, 5, 6}▶ union(2, 5)
10
![Page 14: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/14.jpg)
Disjoint sets in graphs
12
3
4 5
6
7
▶ items = {1, 2, 3, 4, 5, 6, 7}▶ collections = {1, 4, 7}, {2, 3, 5, 6}
▶ union(6, 2)
11
![Page 15: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/15.jpg)
Disjoint sets in graphs
12
3
4 5
6
7
▶ items = {1, 2, 3, 4, 5, 6, 7}▶ collections = {1, 4, 7}, {2, 3, 5, 6}▶ union(6, 2)
11
![Page 16: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/16.jpg)
Disjoint sets in graphs
12
3
4 5
6
7
▶ items = {1, 2, 3, 4, 5, 6, 7}▶ collections = {1, 4, 7}, {2, 3, 5, 6}
12
![Page 17: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/17.jpg)
Example problem: Friends▶ http://uva.onlinejudge.org/external/106/10608.html
13
![Page 18: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/18.jpg)
Range queries
▶ We have an array A of size n▶ Given i, j, we want to answer:
– max(A[i],A[i+ 1], . . . ,A[j− 1],A[j])– min(A[i],A[i+ 1], . . . ,A[j− 1],A[j])– sum(A[i],A[i+ 1], . . . ,A[j− 1],A[j])
▶ We want to answer these queries efficiently, i.e.without looking through all elements
▶ Sometimes we also want to update elements
14
![Page 19: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/19.jpg)
Range sum on a static array▶ Let’s look at range sums on a static array (i.e.updating is not supported)
1 0 7 8 5 9 3
▶ sum(0, 6)
= 33
▶ sum(2, 5)
= 29
▶ sum(2, 2)
= 7
▶ How do we support these queries efficiently?
15
![Page 20: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/20.jpg)
Range sum on a static array▶ Let’s look at range sums on a static array (i.e.updating is not supported)
1 0 7 8 5 9 3
▶ sum(0, 6)
= 33
▶ sum(2, 5)
= 29
▶ sum(2, 2)
= 7
▶ How do we support these queries efficiently?
15
![Page 21: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/21.jpg)
Range sum on a static array▶ Let’s look at range sums on a static array (i.e.updating is not supported)
1 0 7 8 5 9 3
▶ sum(0, 6) = 33
▶ sum(2, 5)
= 29
▶ sum(2, 2)
= 7
▶ How do we support these queries efficiently?
15
![Page 22: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/22.jpg)
Range sum on a static array▶ Let’s look at range sums on a static array (i.e.updating is not supported)
1 0 7 8 5 9 3
▶ sum(0, 6) = 33
▶ sum(2, 5)
= 29
▶ sum(2, 2)
= 7
▶ How do we support these queries efficiently?
15
![Page 23: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/23.jpg)
Range sum on a static array▶ Let’s look at range sums on a static array (i.e.updating is not supported)
1 0 7 8 5 9 3
▶ sum(0, 6) = 33
▶ sum(2, 5) = 29
▶ sum(2, 2)
= 7
▶ How do we support these queries efficiently?
15
![Page 24: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/24.jpg)
Range sum on a static array▶ Let’s look at range sums on a static array (i.e.updating is not supported)
1 0 7 8 5 9 3
▶ sum(0, 6) = 33
▶ sum(2, 5) = 29
▶ sum(2, 2)
= 7
▶ How do we support these queries efficiently?
15
![Page 25: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/25.jpg)
Range sum on a static array▶ Let’s look at range sums on a static array (i.e.updating is not supported)
1 0 7 8 5 9 3
▶ sum(0, 6) = 33
▶ sum(2, 5) = 29
▶ sum(2, 2) = 7
▶ How do we support these queries efficiently?
15
![Page 26: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/26.jpg)
Range sum on a static array▶ Let’s look at range sums on a static array (i.e.updating is not supported)
1 0 7 8 5 9 3
▶ sum(0, 6) = 33
▶ sum(2, 5) = 29
▶ sum(2, 2) = 7
▶ How do we support these queries efficiently?
15
![Page 27: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/27.jpg)
Range sum on a static array▶ Simplification: only support queries of the form
sum(0, j)▶ Notice that sum(i, j) = sum(0, j)− sum(0, i− 1)
1 0 7 8 5 9 3
=
1 0 7 8 5 9 3
−
1 0 7 8 5 9 3
16
![Page 28: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/28.jpg)
Range sum on a static array▶ So we’re only interested in prefix sums▶ But there are only n of them...▶ Just compute them all once in the beginning
1 0 7 8 5 9 3
1 1 8 16 21 30 33
▶ O(n) time to preprocess▶ O(1) time each query
▶ Can we support updating efficiently?
No, at least notwithout modification
17
![Page 29: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/29.jpg)
Range sum on a static array▶ So we’re only interested in prefix sums▶ But there are only n of them...▶ Just compute them all once in the beginning
1 0 7 8 5 9 31
1 8 16 21 30 33
▶ O(n) time to preprocess▶ O(1) time each query
▶ Can we support updating efficiently?
No, at least notwithout modification
17
![Page 30: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/30.jpg)
Range sum on a static array▶ So we’re only interested in prefix sums▶ But there are only n of them...▶ Just compute them all once in the beginning
1 0 7 8 5 9 31 1
8 16 21 30 33
▶ O(n) time to preprocess▶ O(1) time each query
▶ Can we support updating efficiently?
No, at least notwithout modification
17
![Page 31: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/31.jpg)
Range sum on a static array▶ So we’re only interested in prefix sums▶ But there are only n of them...▶ Just compute them all once in the beginning
1 0 7 8 5 9 31 1 8
16 21 30 33
▶ O(n) time to preprocess▶ O(1) time each query
▶ Can we support updating efficiently?
No, at least notwithout modification
17
![Page 32: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/32.jpg)
Range sum on a static array▶ So we’re only interested in prefix sums▶ But there are only n of them...▶ Just compute them all once in the beginning
1 0 7 8 5 9 31 1 8 16
21 30 33
▶ O(n) time to preprocess▶ O(1) time each query
▶ Can we support updating efficiently?
No, at least notwithout modification
17
![Page 33: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/33.jpg)
Range sum on a static array▶ So we’re only interested in prefix sums▶ But there are only n of them...▶ Just compute them all once in the beginning
1 0 7 8 5 9 31 1 8 16 21
30 33
▶ O(n) time to preprocess▶ O(1) time each query
▶ Can we support updating efficiently?
No, at least notwithout modification
17
![Page 34: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/34.jpg)
Range sum on a static array▶ So we’re only interested in prefix sums▶ But there are only n of them...▶ Just compute them all once in the beginning
1 0 7 8 5 9 31 1 8 16 21 30
33
▶ O(n) time to preprocess▶ O(1) time each query
▶ Can we support updating efficiently?
No, at least notwithout modification
17
![Page 35: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/35.jpg)
Range sum on a static array▶ So we’re only interested in prefix sums▶ But there are only n of them...▶ Just compute them all once in the beginning
1 0 7 8 5 9 31 1 8 16 21 30 33
▶ O(n) time to preprocess▶ O(1) time each query
▶ Can we support updating efficiently?
No, at least notwithout modification
17
![Page 36: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/36.jpg)
Range sum on a static array▶ So we’re only interested in prefix sums▶ But there are only n of them...▶ Just compute them all once in the beginning
1 0 7 8 5 9 31 1 8 16 21 30 33
▶ O(n) time to preprocess▶ O(1) time each query
▶ Can we support updating efficiently?
No, at least notwithout modification
17
![Page 37: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/37.jpg)
Range sum on a static array▶ So we’re only interested in prefix sums▶ But there are only n of them...▶ Just compute them all once in the beginning
1 0 7 8 5 9 31 1 8 16 21 30 33
▶ O(n) time to preprocess▶ O(1) time each query
▶ Can we support updating efficiently? No, at least notwithout modification
17
![Page 38: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/38.jpg)
Range sum on a dynamic array▶ What if we want to support:
– sum over a range– updating an element
1 0 7 8 5 9 3
▶ sum(0, 6)
= 33
▶ update(3,−2)
▶ sum(0, 6)
= 23
▶ How do we support these queries efficiently?
18
![Page 39: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/39.jpg)
Range sum on a dynamic array▶ What if we want to support:
– sum over a range– updating an element
1 0 7 8 5 9 3
▶ sum(0, 6)
= 33
▶ update(3,−2)
▶ sum(0, 6)
= 23
▶ How do we support these queries efficiently?
18
![Page 40: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/40.jpg)
Range sum on a dynamic array▶ What if we want to support:
– sum over a range– updating an element
1 0 7 8 5 9 3
▶ sum(0, 6) = 33
▶ update(3,−2)
▶ sum(0, 6)
= 23
▶ How do we support these queries efficiently?
18
![Page 41: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/41.jpg)
Range sum on a dynamic array▶ What if we want to support:
– sum over a range– updating an element
1 0 7 8 5 9 3
▶ sum(0, 6) = 33
▶ update(3,−2)
▶ sum(0, 6)
= 23
▶ How do we support these queries efficiently?
18
![Page 42: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/42.jpg)
Range sum on a dynamic array▶ What if we want to support:
– sum over a range– updating an element
1 0 7 -2 5 9 3
▶ sum(0, 6) = 33
▶ update(3,−2)
▶ sum(0, 6)
= 23
▶ How do we support these queries efficiently?
18
![Page 43: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/43.jpg)
Range sum on a dynamic array▶ What if we want to support:
– sum over a range– updating an element
1 0 7 -2 5 9 3
▶ sum(0, 6) = 33
▶ update(3,−2)
▶ sum(0, 6)
= 23
▶ How do we support these queries efficiently?
18
![Page 44: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/44.jpg)
Range sum on a dynamic array▶ What if we want to support:
– sum over a range– updating an element
1 0 7 -2 5 9 3
▶ sum(0, 6) = 33
▶ update(3,−2)
▶ sum(0, 6) = 23
▶ How do we support these queries efficiently?
18
![Page 45: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/45.jpg)
Range sum on a dynamic array▶ What if we want to support:
– sum over a range– updating an element
1 0 7 -2 5 9 3
▶ sum(0, 6) = 33
▶ update(3,−2)
▶ sum(0, 6) = 23
▶ How do we support these queries efficiently?
18
![Page 46: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/46.jpg)
Segment Tree
1 0 7 8 5 9 3
1 15 14
16 17
33
1 0 7 8 5 9 3
▶ Each vertex contains the sum of some segment ofthe array
19
![Page 47: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/47.jpg)
Segment Tree
1 0 7 8 5 9 3
1 15 14
16 17
33
1 0 7 8 5 9 3
▶ Each vertex contains the sum of some segment ofthe array
19
![Page 48: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/48.jpg)
Segment Tree
1 0 7 8 5 9 3
1 15 14
16 17
33
1 0 7 8 5 9 3
▶ Each vertex contains the sum of some segment ofthe array
19
![Page 49: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/49.jpg)
Segment Tree
1 0 7 8 5 9 3
1 15 14
16 17
33
1 0 7 8 5 9 3
▶ Each vertex contains the sum of some segment ofthe array
19
![Page 50: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/50.jpg)
Segment Tree
1 0 7 8 5 9 3
1 15 14
16 17
33
1 0 7 8 5 9 3
▶ Each vertex contains the sum of some segment ofthe array
19
![Page 51: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/51.jpg)
Segment Tree
1 0 7 8 5 9 3
1 15 14
16 17
33
1 0 7 8 5 9 3
▶ Each vertex contains the sum of some segment ofthe array
19
![Page 52: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/52.jpg)
Segment Tree - Codestruct segment_tree {
segment_tree *left, *right;int from, to, value;segment_tree(int from, int to)
: from(from), to(to), left(NULL), right(NULL), value(0) { }};
segment_tree* build(const vector<int> &arr, int l, int r) {if (l > r) return NULL;segment_tree *res = new segment_tree(l, r);if (l == r) {
res->value = arr[l];} else {
int m = (l + r) / 2;res->left = build(arr, l, m);res->right = build(arr, m + 1, r);if (res->left != NULL) res->value += res->left->value;if (res->right != NULL) res->value += res->right->value;
}return res;
}
20
![Page 53: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/53.jpg)
Querying a Segment Tree
1 0 7 8 5 9
1 0 7 8 5 9
3
1 15 14
1 15 141 15 14
16
16
17
33
1 0 7 8 5 9 3
▶ sum(0, 5)
= 16 + 14 = 30
▶ We only need to consider a few vertices to get theentire range
▶ But how do we find them?
21
![Page 54: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/54.jpg)
Querying a Segment Tree
1 0 7 8 5 9
1 0 7 8 5 9
3
1 15 14
1 15 141 15 14
16
16
17
33
1 0 7 8 5 9 3
▶ sum(0, 5)
= 16 + 14 = 30
▶ We only need to consider a few vertices to get theentire range
▶ But how do we find them?
21
![Page 55: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/55.jpg)
Querying a Segment Tree
1 0 7 8 5 9
1 0 7 8 5 9 3
1 15 14
1 15 141 15 14
16
16
17
33
1 0 7 8 5 9 3
▶ sum(0, 5)
= 16 + 14 = 30
▶ We only need to consider a few vertices to get theentire range
▶ But how do we find them?
21
![Page 56: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/56.jpg)
Querying a Segment Tree
1 0 7 8 5 9
1 0 7 8 5 9
3
1 15 14
1 15 14
1 15 14
16
16
17
33
1 0 7 8 5 9 3
▶ sum(0, 5)
= 16 + 14 = 30
▶ We only need to consider a few vertices to get theentire range
▶ But how do we find them?
21
![Page 57: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/57.jpg)
Querying a Segment Tree
1 0 7 8 5 9
1 0 7 8 5 9
3
1 15 141 15 14
1 15 14
1616 17
33
1 0 7 8 5 9 3
▶ sum(0, 5)
= 16 + 14 = 30
▶ We only need to consider a few vertices to get theentire range
▶ But how do we find them?
21
![Page 58: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/58.jpg)
Querying a Segment Tree
1 0 7 8 5 9
1 0 7 8 5 9
3
1 15 141 15 14
1 15 14
16
16 17
33
1 0 7 8 5 9 3
▶ sum(0, 5) = 16 + 14 = 30
▶ We only need to consider a few vertices to get theentire range
▶ But how do we find them?
21
![Page 59: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/59.jpg)
Querying a Segment Tree
1 0 7 8 5 9
1 0 7 8 5 9
3
1 15 141 15 14
1 15 14
16
16 17
33
1 0 7 8 5 9 3
▶ sum(0, 5) = 16 + 14 = 30
▶ We only need to consider a few vertices to get theentire range
▶ But how do we find them?
21
![Page 60: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/60.jpg)
Querying a Segment Tree
1 0 7 8 5 9
1 0 7 8 5 9
3
1 15 141 15 14
1 15 14
16
16 17
33
1 0 7 8 5 9 3
▶ sum(0, 5) = 16 + 14 = 30
▶ We only need to consider a few vertices to get theentire range
▶ But how do we find them?21
![Page 61: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/61.jpg)
Querying a Segment Tree
1 0 7 8 5 9 3
3
1 15 14
14
16 17
16 1716 17
33
33
1 0 7 8 5 9 3
▶ sum(0, 5)
22
![Page 62: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/62.jpg)
Querying a Segment Tree
1 0 7 8 5 9 3
3
1 15 14
14
16 17
16 1716 17
33
33
1 0 7 8 5 9 3
▶ sum(0, 5)
22
![Page 63: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/63.jpg)
Querying a Segment Tree
1 0 7 8 5 9 3
3
1 15 14
14
16 17
16 1716 17
33
33
1 0 7 8 5 9 3
▶ sum(0, 5)
22
![Page 64: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/64.jpg)
Querying a Segment Tree
1 0 7 8 5 9 3
3
1 15 14
14
16 17
16 17
16 17
33
33
1 0 7 8 5 9 3
▶ sum(0, 5)
22
![Page 65: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/65.jpg)
Querying a Segment Tree
1 0 7 8 5 9
3
3
1 15
14
14
16 1716 17
16 17
33
33
1 0 7 8 5 9 3
▶ sum(0, 5)
22
![Page 66: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/66.jpg)
Querying a Segment Tree
1 0 7 8 5 9 3
3
1 15
14
14
16 1716 17
16 17
33
33
1 0 7 8 5 9 3
▶ sum(0, 5)
22
![Page 67: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/67.jpg)
Querying a Segment Tree - Code
int query(segment_tree *tree, int l, int r) {if (tree == NULL) return 0;if (l <= tree->from && tree->to <= r) return tree->value;if (tree->to < l) return 0;if (r < tree->from) return 0;return query(tree->left, l, r) + query(tree->right, l, r);
}
23
![Page 68: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/68.jpg)
Updating a Segment Tree
1 0 7 8
855
5 9 3
1 15
151212
14
16
161313
17
33
333030
1 0 7 8 5 9 3
▶ update(3, 5)
24
![Page 69: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/69.jpg)
Updating a Segment Tree
1 0 7 8
855
5 9 3
1 15
151212
14
16
161313
17
33
333030
1 0 7 8 5 9 3
▶ update(3, 5)
24
![Page 70: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/70.jpg)
Updating a Segment Tree
1 0 7
8
8
55
5 9 3
1 15
151212
14
16
161313
17
33
333030
1 0 7 8 5 9 3
▶ update(3, 5)
24
![Page 71: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/71.jpg)
Updating a Segment Tree
1 0 7
88
5
5
5 9 3
1 15
151212
14
16
161313
17
33
333030
1 0 7 5 5 9 3
▶ update(3, 5)
24
![Page 72: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/72.jpg)
Updating a Segment Tree
1 0 7
885
5 5 9 3
1
15
15
1212
14
16
161313
17
33
333030
1 0 7 5 5 9 3
▶ update(3, 5)
24
![Page 73: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/73.jpg)
Updating a Segment Tree
1 0 7
885
5 5 9 3
1
1515
12
12
14
16
161313
17
33
333030
1 0 7 5 5 9 3
▶ update(3, 5)
24
![Page 74: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/74.jpg)
Updating a Segment Tree
1 0 7
885
5 5 9 3
1
151512
12 14
16
16
1313
17
33
333030
1 0 7 5 5 9 3
▶ update(3, 5)
24
![Page 75: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/75.jpg)
Updating a Segment Tree
1 0 7
885
5 5 9 3
1
151512
12 14
1616
13
13
17
33
333030
1 0 7 5 5 9 3
▶ update(3, 5)
24
![Page 76: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/76.jpg)
Updating a Segment Tree
1 0 7
885
5 5 9 3
1
151512
12 14
161613
13 17
33
33
3030
1 0 7 5 5 9 3
▶ update(3, 5)
24
![Page 77: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/77.jpg)
Updating a Segment Tree
1 0 7
885
5 5 9 3
1
151512
12 14
161613
13 17
3333
30
30
1 0 7 5 5 9 3
▶ update(3, 5)
24
![Page 78: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/78.jpg)
Updating a Segment Tree
1 0 7
885
5 5 9 3
1
151512
12 14
161613
13 17
333330
30
1 0 7 5 5 9 3
▶ update(3, 5)
24
![Page 79: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/79.jpg)
Updating a Segment Tree - Code
int update(segment_tree *tree, int i, int val) {if (tree == NULL) return 0;if (tree->to < i) return tree->value;if (i < tree->from) return tree->value;if (tree->from == tree->to && tree->from == i) {
tree->value = val;} else {
tree->value = update(tree->left, i, val) + update(tree->right, i, val);}return tree->value;
}
25
![Page 80: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/80.jpg)
Segment Tree▶ Now we can
– build a Segment Tree
in O(n)
– query a range
in O(logn)
– update a single value
in O(logn)
▶ But how efficient are these operations?
▶ Trivial to use Segment Trees for min, max, gcd, andother similar operators, basically the same code
▶ Also possible to update a range of values in O(logn)(Google for Segment Trees with Lazy Propagation ifyou want to learn more)
26
![Page 81: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/81.jpg)
Segment Tree▶ Now we can
– build a Segment Tree
in O(n)
– query a range
in O(logn)
– update a single value
in O(logn)
▶ But how efficient are these operations?
▶ Trivial to use Segment Trees for min, max, gcd, andother similar operators, basically the same code
▶ Also possible to update a range of values in O(logn)(Google for Segment Trees with Lazy Propagation ifyou want to learn more)
26
![Page 82: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/82.jpg)
Segment Tree▶ Now we can
– build a Segment Tree in O(n)– query a range
in O(logn)
– update a single value
in O(logn)
▶ But how efficient are these operations?
▶ Trivial to use Segment Trees for min, max, gcd, andother similar operators, basically the same code
▶ Also possible to update a range of values in O(logn)(Google for Segment Trees with Lazy Propagation ifyou want to learn more)
26
![Page 83: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/83.jpg)
Segment Tree▶ Now we can
– build a Segment Tree in O(n)– query a range in O(logn)– update a single value
in O(logn)
▶ But how efficient are these operations?
▶ Trivial to use Segment Trees for min, max, gcd, andother similar operators, basically the same code
▶ Also possible to update a range of values in O(logn)(Google for Segment Trees with Lazy Propagation ifyou want to learn more)
26
![Page 84: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/84.jpg)
Segment Tree▶ Now we can
– build a Segment Tree in O(n)– query a range in O(logn)– update a single value in O(logn)
▶ But how efficient are these operations?
▶ Trivial to use Segment Trees for min, max, gcd, andother similar operators, basically the same code
▶ Also possible to update a range of values in O(logn)(Google for Segment Trees with Lazy Propagation ifyou want to learn more)
26
![Page 85: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/85.jpg)
Segment Tree▶ Now we can
– build a Segment Tree in O(n)– query a range in O(logn)– update a single value in O(logn)
▶ But how efficient are these operations?
▶ Trivial to use Segment Trees for min, max, gcd, andother similar operators, basically the same code
▶ Also possible to update a range of values in O(logn)(Google for Segment Trees with Lazy Propagation ifyou want to learn more)
26
![Page 86: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/86.jpg)
Segment Tree▶ Now we can
– build a Segment Tree in O(n)– query a range in O(logn)– update a single value in O(logn)
▶ But how efficient are these operations?
▶ Trivial to use Segment Trees for min, max, gcd, andother similar operators, basically the same code
▶ Also possible to update a range of values in O(logn)(Google for Segment Trees with Lazy Propagation ifyou want to learn more)
26
![Page 87: Data Structures](https://reader033.fdocuments.us/reader033/viewer/2022051419/563dba57550346aa9aa4c0e9/html5/thumbnails/87.jpg)
Example problem: Potentiometers▶ http://uva.onlinejudge.org/external/120/12086.html
27