[C++] Segment tree easy to understand
This is my code implement segment tree to solve this problem. If you find it helpful, please upvote. Thank you for reading.\n```\nvoid build(vector &seg, vector
This is my code implement segment tree to solve this problem. If you find it helpful, please upvote. Thank you for reading.\n```\nvoid build(vector<int> &seg, vector<vector<int>> &rooms, int node, int start, int end){\n    if(start == end){\n        seg[node] = rooms[start][1];\n        return;\n    }\n    int mid = (start+end)/2;\n    build(seg, rooms, node*2+1, start, mid);\n    build(seg, rooms, node*2+2, mid+1, end);\n    seg[node] = max(seg[node*2+1], seg[node*2+2]);\n}\nint get(vector<int> &seg, int node, int start, int end, int left, int right){\n    if(start>right || end<left){\n        return 0;\n    }\n    if(left<=start && end<=right){\n        return seg[node];\n    }\n    int mid = (start+end)/2;\n    return max(get(seg, node*2+1, start, mid, left, right), get(seg, node*2+2, mid+1, end, left, right));\n}\nclass Solution {\npublic:\n    vector<int> closestRoom(vector<vector<int>>& rooms, vector<vector<int>>& queries) {\n        sort(rooms.begin(), rooms.end());\n        vector<int> sorted_id;\n        for(auto room: rooms){\n            sorted_id.push_back(room[0]);\n        }\n        vector<int> seg(4*rooms.size()+5, 0);\n        build(seg, rooms, 0, 0, rooms.size()-1);\n        vector<int> ans;\n        for(auto query: queries){\n            auto it = lower_bound(sorted_id.begin(), sorted_id.end(), query[0]);\n            int barrier = it - sorted_id.begin();\n            int smaller = -1;\n            int larger = -1;\n            int low = barrier;\n            int high = rooms.size()-1;\n            while(low<=high){\n                int mid = (low+high)/2;\n                if(get(seg, 0, 0, rooms.size()-1, barrier, mid) >= query[1]){\n                    high = mid -1;\n                    larger = mid;\n                }\n                else{\n                    low = mid+1;\n                }\n            }\n            barrier -= 1;\n            low = 0;\n            high = barrier;\n            while(low <= high){\n                int mid = (low+high)/2;\n                if(get(seg, 0, 0, rooms.size()-1, mid, barrier) >= query[1]){\n                    low = mid +1;\n                    smaller = mid;\n                }\n                else{\n                    high = mid-1;\n                }\n            }\n            if(smaller == -1 && larger==-1){\n                ans.push_back(-1);\n            }\n            else if(smaller == -1){\n                ans.push_back(sorted_id[larger]);\n            }\n            else if(larger == -1){\n                ans.push_back(sorted_id[smaller]);\n            }\n            else{\n                if(abs(query[0]-sorted_id[smaller])<=abs(query[0]-sorted_id[larger])){\n                    ans.push_back(sorted_id[smaller]);\n                }\n                else{\n                    ans.push_back(sorted_id[larger]);\n                }\n            }\n        }\n        return ans;\n    }\n};
['Binary Search', 'Tree', 'C']