C++ - O(nlogn) - Breakdown of the solution
\nIf a question has queries the number one thing that comes to mind is asking myself: Is this problem easier if I process the queries in a different order?\nFor
\nIf a question has queries the number one thing that comes to mind is asking myself: Is this problem easier if I process the queries in a different order?\nFor this problem if we process the queries starting with the ones that ask for the biggest rooms first the problem becomes easier.\nWe start with the query that asks for the biggest room, in a sorted array we remove all the rooms that are bigger or equal to this query and add them to our set.\nMoving forward we know that those rooms will be options to all other queries too, because the queries will need smaller or equal rooms.\n\nSo as we process queries we add to a set, every ID on the set is valid for the current query. Now the problem is to find the closest ID in the set.\nWe can use binary search to find the closest one. `lower_bound` will return the leftmost element that is equal or bigger. We might want the right most \nelement that is smaller too, so we need to look at the previous element returned by `lower_bound` too.\n\n```\nvector<int> closestRoom(vector<vector<int>>& rooms, vector<vector<int>>& queries)\n{\n    const int q = int(queries.size());\n    vector<int> res(q, -1);\n\n    // This vector will contain indices\'s from bigger to smaller, this way we process the queries in a different order\n    // but maintain the order of the queries in the result as we need to return them in the original order.\n    vector<int> qid(q, 0);\n    iota(qid.begin(), qid.end(), 0);\n    sort(qid.begin(), qid.end(), [&](const int& l, const int& r) { return queries[l][1] > queries[r][1]; });\n\n    // bigger rooms at the back of the vector.\n    sort(rooms.begin(), rooms.end(), [](const auto& l, const auto& r){ return l[1] < r[1]; });\n\n    set<int> room_ids = {};\n\n    for (const int& i : qid)\n    {\n        // We can pop back of a vector in O(1), erasing the front element is O(n). That why it\'s backwards.\n        // Add rooms that are bigger than the current query, this room will be bigger than all following queries.\n        while (!rooms.empty() && rooms.back()[1] >= queries[i][1])\n        {\n            room_ids.insert(rooms.back()[0]);\n            rooms.pop_back();\n        }\n\n        if (room_ids.empty())\n            continue;\n\n        // binary search on the room ids, lower_bound will return the next biggest element if the one searched is not there\n        // eg: 7 9 15 17 19, lower_bound( 10 ) == 15\n        auto ite = room_ids.lower_bound(queries[i][0]);\n        if (ite == room_ids.end()) // if we asked for an id too big just use the last one.\n            ite = prev(room_ids.end());\n        int dist = abs(*ite - queries[i][0]);\n        int id = *ite;\n\n        // now we try the smaller ID, if it\'s closer or equal in distance we take this one as the ID will be smaller. \n        ite = prev(ite);\n        if (ite != room_ids.end())\n        {\n            const int d = abs(*ite - queries[i][0]);\n            if (d <= dist) dist = d, id = *ite;\n        }\n        res[i] = id;\n    }\n    return res;\n}\n```
[]