class Solution { public: vector> getSkyline(vector>& buildings) { sort(buildings.begin(), buildings.end()); int n = buildings.size(); vector> res; vector passed(n); priority_queue > x, y; for (int i = 0, cnt=0; cnt < 2*n; ) { if (x.empty() || (i < n && buildings[i][0] <= -x.top()[0])) { x.push({-buildings[i][0], i, -1}); x.push({-buildings[i][1], i, 1}); y.push({buildings[i][2], i}); ++i; } else { auto j = x.top(); while (x.size() && x.top()[0] == j[0]) { passed[x.top()[1]] = x.top()[2] > 0 ? true : false; x.pop(); cnt += 1; } while (y.size() && passed[y.top()[1]]) y.pop(); int h = y.empty() ? 0 : y.top()[0]; if (res.empty() || (res[res.size()-1][0] != -j[0] && res[res.size()-1][1] != h)) res.push_back({-j[0], h}); } } return res; } };