C++ Sort Set Solution O(NLogN) + O(KLogN)
Runtime: 644 ms, faster than 81.25% of C++ online submissions for Closest Room.\nMemory Usage: 149.3 MB, less than 53.75% of C++ online submissions for Closest 
Runtime: 644 ms, faster than 81.25% of C++ online submissions for Closest Room.\nMemory Usage: 149.3 MB, less than 53.75% of C++ online submissions for Closest Room.\n\n\n**Solution taken from votrubac.**\n\n```\nWe have to find the room id which is in the minimum distance with the query id and\nroom size is greater or equal the query size. So we can sort the rooms and queries based\non the size in descending order and then search the queries. From larger to smaller\nroom size we can insert ids in the Set and then use binary search to find the min distance\nid for query id. As we will add ids to Set N time, so complexity NLogN and also for K\nqueries we do binary search K times with complexity KLogN.\n\nTotal Complexity : O(NLogN) + O(KLogN)\n```\n\n```\nbool comparator(const vector<int>&a,const vector<int>&b)\n{\n    return a[1] > b[1];\n}\n\nclass Solution {\npublic:\n    vector<int> closestRoom(vector<vector<int>>& rooms, vector<vector<int>>& queries) {\n        \n        int qsize = queries.size();\n        vector<int>res(qsize);\n        \n        // add indexes of the queries as extra value\n        for(int i=0;i<qsize;i++)\n            queries[i].push_back(i);\n        \n        // sort rooms and queries in descending order of room size\n        sort(begin(rooms),end(rooms),comparator);\n        sort(begin(queries),end(queries),comparator);\n        \n        // ascending order of room ids\n        set<int>ids;\n        \n        for(int i=0,j=0;i<qsize;i++)\n        {\n            // insert all romm ids for rooms with size >= query size\n            for(;j<rooms.size() && rooms[j][1]>=queries[i][1];j++)\n                ids.insert(rooms[j][0]);\n            \n            // if ids empty then room size does not exist and so set -1\n            if(ids.empty())\n            {\n                res[queries[i][2]] = -1;\n                continue;\n            }\n            \n            int id,find,prevVal,nextVal;\n            \n            // find the upper bound of the query room id\n            find = queries[i][0];\n            auto iter = ids.upper_bound(find);\n            \n            // if iter is in the begin then no previous possible and begin value is the closest\n            if(iter==ids.begin()) id = *ids.begin();\n            else\n            {\n                // min distance id will be either in upper bound iter or its previous iter\n                \n                nextVal = iter==ids.end() ? INT_MAX : *iter;\n                prevVal = *prev(iter);\n                \n                id = abs(prevVal-find)<=abs(nextVal-find) ? prevVal : nextVal;\n            }\n            \n            res[queries[i][2]] = id;\n        }\n        \n        return res;\n    }\n};\n```\n\n\n
['C', 'Sorting', 'Binary Tree', 'Ordered Set']