[C++] Segment Tree + Binary Search
For each arrival, just find nearest index free using binary search + segment tree. Here is my implementation. If you find it helpful, please upvote. thank you f
For each arrival, just find nearest index free using binary search + segment tree. Here is my implementation. If you find it helpful, please upvote. thank you for reading.\n\n# Code\n```\nvoid update(int node, int start, int end, int pos, int val, vector<int> &seg){\n    if(start > pos || end < pos){\n        return;\n    }\n    if(start == end){\n        seg[node] = val;\n        return;\n    }\n    int mid = (start+end)/2;\n    update(node*2+1, start, mid, pos, val, seg);\n    update(node*2+2, mid+1, end, pos, val, seg);\n    seg[node] = min(seg[node*2+1], seg[node*2+2]);\n}\n\nint get(int node, int start, int end, int left, int right, vector<int> &seg){\n    if(start > right || end < left){\n        return INT_MAX;\n    }\n    if(left<=start && end <= right){\n        return seg[node];\n    }\n    int mid = (start+end)/2;\n    return min(get(node*2+1, start, mid, left, right, seg), get(node*2+2, mid+1, end, left, right, seg));\n}\n\nclass Solution {\npublic:\n    vector<int> busiestServers(int k, vector<int>& arrival, vector<int>& load) {\n        vector<int> seg(4*k+4, 0);\n        vector<int> contain(k, 0);\n        for(int i=0; i<arrival.size(); i++){\n            \n            int low = (i%k);\n            int high = k-1;\n            int index = -1;\n            while(low <= high){\n                int mid = (low+high)/2;\n                \n                if(get(0, 0, k-1, i%k, mid, seg) <= arrival[i]){\n                    index = mid;\n                    high = mid-1;\n                }\n                else{\n                    low = mid+1;\n                }\n            }\n            if(index == -1){\n                low = 0;\n                high = (i%k)-1;\n                while(low <= high){\n                    int mid = (low+high)/2;\n                    if(get(0, 0, k-1, 0, mid, seg) <= arrival[i]){\n                        index = mid;\n                        high = mid-1;\n                    }\n                    else{\n                        low = mid+1;\n                    }\n                }\n            }\n            if(index != -1){\n                update(0, 0, k-1, index, arrival[i]+load[i], seg);\n                contain[index] += 1;\n            }\n        }\n        vector<int> ans;\n        int maxx = -1;\n        for(int i=0; i<k; i++){\n            if(contain[i] > maxx){\n                ans.clear();\n                maxx = contain[i];\n            }\n            if(contain[i] == maxx){\n                ans.push_back(i);\n            }\n        }\n        return ans;\n    }\n};\n```
['Binary Search', 'Segment Tree', 'C++']