forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
160 lines (127 loc) · 4.09 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/// Source : https://leetcode.com/problems/the-skyline-problem/description/
/// Author : liuyubobobo
/// Time : 2017-10-28
/// updated: 2019-03-14
#include <iostream>
#include <vector>
#include <set>
#include <unordered_map>
#include <cassert>
using namespace std;
/// Using Segment Tree
/// Time Complexity: O(nlogn)
/// Space Complexity: O(n)
class SegmentTree{
private:
int n;
vector<int> tree, lazy;
public:
SegmentTree(int n): n(n), tree(4 * n, 0), lazy(4 * n, 0){}
void add(int l, int r, int h){
update(0, 0, n-1, l, r, h);
}
int query(int index){
return query(0, 0, n-1, index);
}
private:
void update(int treeID, int treeL, int treeR, int l, int r, int h){
if(lazy[treeID] != 0){
tree[treeID] = max(tree[treeID], lazy[treeID]); // max
if(treeL != treeR){
lazy[2 * treeID + 1] = max(lazy[treeID], lazy[2 * treeID + 1]); // max
lazy[2 * treeID + 2] = max(lazy[treeID], lazy[2 * treeID + 2]); // max
}
lazy[treeID] = 0;
}
if(treeL == l && treeR == r){
tree[treeID] = max(tree[treeID], h); // max
if(treeL != treeR){
lazy[2 * treeID + 1] = max(h, lazy[2 * treeID + 1]); // max
lazy[2 * treeID + 2] = max(h, lazy[2 * treeID + 2]); // max
}
return;
}
int mid = (treeL + treeR) / 2;
if(r <= mid)
update(2 * treeID + 1, treeL, mid, l, r, h);
else if(l >= mid + 1)
update(2 * treeID + 2, mid + 1, treeR, l, r, h);
else{
update(2 * treeID + 1, treeL, mid, l, mid, h);
update(2 * treeID + 2, mid + 1, treeR, mid + 1, r, h);
}
return;
}
int query(int treeID, int treeL, int treeR, int index){
if(lazy[treeID] != 0){
tree[treeID] = max(tree[treeID], lazy[treeID]); // max
if(treeL != treeR){
lazy[2 * treeID + 1] = max(lazy[treeID], lazy[2 * treeID + 1]); // max
lazy[2 * treeID + 2] = max(lazy[treeID], lazy[2 * treeID + 2]); // max
}
lazy[treeID] = 0;
}
if(treeL == treeR){
assert(treeL == index);
return tree[treeID];
}
int mid = (treeL + treeR) / 2;
if(index <= mid)
return query(2 * treeID + 1, treeL, mid, index);
return query(2 * treeID + 2, mid + 1, treeR, index);
}
};
class Solution {
public:
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
// Coordinate compression
set<int> unique_points;
for(const vector<int>& info: buildings){
unique_points.insert(info[0]);
unique_points.insert(info[1]-1);
unique_points.insert(info[1]);
}
unordered_map<int, int> indexes;
vector<int> pos;
for(int p: unique_points){
indexes[p] = pos.size();
pos.push_back(p);
}
// segment tree
SegmentTree stree(pos.size());
for(const vector<int>& info: buildings)
stree.add(indexes[info[0]], indexes[info[1]-1], info[2]);
// get results
vector<pair<int, int>> res;
unique_points.clear();
for(const vector<int>& info: buildings){
unique_points.insert(info[0]);
unique_points.insert(info[1]);
}
int last = 0;
for(int p: unique_points){
int h = stree.query(indexes[p]);
if(h != last)
res.push_back(make_pair(p, h));
last = h;
}
return res;
}
};
int main() {
int n = 5;
int buildings[5][3] = {
{2, 9, 10},
{3, 7, 15},
{5, 12, 12},
{15, 20, 10},
{19, 24, 8}
};
vector<vector<int>> vec;
for(int i = 0 ; i < n ; i ++)
vec.push_back(vector<int>(buildings[i], buildings[i] + 3));
vector<pair<int, int>> res = Solution().getSkyline(vec);
for(pair<int, int> p: res)
cout << p.first << " " << p.second << endl;
return 0;
}