intSolution::setIntersection(vector<vector<int>> &A){ sort(begin(A), end(A), [](auto& a, auto& b) { if(a[1] != b[1]) return a[1] < b[1]; return a[0] > b[0]; }); int res = 2, mi = A[0][1] - 1, ma = A[0][1]; for(int i = 1; i < A.size(); i++) { int l = A[i][0], r = A[i][1]; if(ma < l) { res += 2; ma = r, mi = r - 1; } elseif(ma == l) { res += 1; mi = ma, ma = r; } elseif(mi < l) { res += 1; if(ma == r) mi = ma - 1; else mi = ma, ma = r; } } return res; }