1
1
// / Source : https://leetcode.com/problems/the-skyline-problem/description/
2
2
// / Author : liuyubobobo
3
3
// / Time : 2017-10-28
4
+ // / updated: 2019-03-14
4
5
5
6
#include < iostream>
6
7
#include < vector>
10
11
11
12
using namespace std ;
12
13
14
+
15
+ // / Using Segment Tree
16
+ // / Time Complexity: O(nlogn)
17
+ // / Space Complexity: O(n)
13
18
class SegmentTree {
14
19
15
20
private:
16
21
int n;
17
- vector<int > tree;
18
- vector<int > lazy;
22
+ vector<int > tree, lazy;
19
23
20
24
public:
21
- SegmentTree (int n){
22
-
23
- this ->n = n;
24
-
25
- int size = 4 *n;
26
- for (int i = 0 ; i < size ; i ++){
27
- tree.push_back (0 );
28
- lazy.push_back (0 );
29
- }
30
- }
25
+ SegmentTree (int n): n(n), tree(4 * n, 0 ), lazy(4 * n, 0 ){}
31
26
32
27
void add (int l, int r, int h){
33
28
update (0 , 0 , n-1 , l, r, h);
@@ -41,19 +36,19 @@ class SegmentTree{
41
36
void update (int treeID, int treeL, int treeR, int l, int r, int h){
42
37
43
38
if (lazy[treeID] != 0 ){
44
- tree[treeID] = max (tree[treeID], lazy[treeID]);
39
+ tree[treeID] = max (tree[treeID], lazy[treeID]); // max
45
40
if (treeL != treeR){
46
- lazy[2 * treeID + 1 ] = max (lazy[treeID], lazy[2 * treeID + 1 ]);
47
- lazy[2 * treeID + 2 ] = max (lazy[treeID], lazy[2 * treeID + 2 ]);
41
+ lazy[2 * treeID + 1 ] = max (lazy[treeID], lazy[2 * treeID + 1 ]); // max
42
+ lazy[2 * treeID + 2 ] = max (lazy[treeID], lazy[2 * treeID + 2 ]); // max
48
43
}
49
44
lazy[treeID] = 0 ;
50
45
}
51
46
52
47
if (treeL == l && treeR == r){
53
- tree[treeID] = max (tree[treeID], h);
48
+ tree[treeID] = max (tree[treeID], h); // max
54
49
if (treeL != treeR){
55
- lazy[2 * treeID + 1 ] = max (h, lazy[2 * treeID + 1 ]);
56
- lazy[2 * treeID + 2 ] = max (h, lazy[2 * treeID + 2 ]);
50
+ lazy[2 * treeID + 1 ] = max (h, lazy[2 * treeID + 1 ]); // max
51
+ lazy[2 * treeID + 2 ] = max (h, lazy[2 * treeID + 2 ]); // max
57
52
}
58
53
return ;
59
54
}
@@ -75,10 +70,10 @@ class SegmentTree{
75
70
int query (int treeID, int treeL, int treeR, int index){
76
71
77
72
if (lazy[treeID] != 0 ){
78
- tree[treeID] = max (tree[treeID], lazy[treeID]);
73
+ tree[treeID] = max (tree[treeID], lazy[treeID]); // max
79
74
if (treeL != treeR){
80
- lazy[2 * treeID + 1 ] = max (lazy[treeID], lazy[2 * treeID + 1 ]);
81
- lazy[2 * treeID + 2 ] = max (lazy[treeID], lazy[2 * treeID + 2 ]);
75
+ lazy[2 * treeID + 1 ] = max (lazy[treeID], lazy[2 * treeID + 1 ]); // max
76
+ lazy[2 * treeID + 2 ] = max (lazy[treeID], lazy[2 * treeID + 2 ]); // max
82
77
}
83
78
lazy[treeID] = 0 ;
84
79
}
@@ -97,13 +92,14 @@ class SegmentTree{
97
92
}
98
93
};
99
94
95
+
100
96
class Solution {
101
97
public:
102
98
vector<pair<int , int >> getSkyline (vector<vector<int >>& buildings) {
103
99
104
100
// Coordinate compression
105
101
set<int > unique_points;
106
- for (vector<int > info: buildings){
102
+ for (const vector<int >& info: buildings){
107
103
unique_points.insert (info[0 ]);
108
104
unique_points.insert (info[1 ]-1 );
109
105
unique_points.insert (info[1 ]);
@@ -118,13 +114,13 @@ class Solution {
118
114
119
115
// segment tree
120
116
SegmentTree stree (pos.size ());
121
- for (vector<int > info: buildings)
117
+ for (const vector<int >& info: buildings)
122
118
stree.add (indexes[info[0 ]], indexes[info[1 ]-1 ], info[2 ]);
123
119
124
120
// get results
125
121
vector<pair<int , int >> res;
126
122
unique_points.clear ();
127
- for (vector<int > info: buildings){
123
+ for (const vector<int >& info: buildings){
128
124
unique_points.insert (info[0 ]);
129
125
unique_points.insert (info[1 ]);
130
126
}
@@ -141,6 +137,7 @@ class Solution {
141
137
}
142
138
};
143
139
140
+
144
141
int main () {
145
142
146
143
int n = 5 ;
0 commit comments