C++ Clean Solution | Events/Line Sweep + Ordered Set
Intuition\n\n- See the request as events of start time and end time\n- Use Events/Line Sweep\n- Use ordered set to get the correct server\n\n# Complexity\n\n- T
# Intuition\n\n- See the request as events of start time and end time\n- Use Events/Line Sweep\n- Use ordered set to get the correct server\n\n# Complexity\n\n- Time complexity: $O(N \\log N + K \\log K)$\n\n- Space complexity: $O(N + K)$\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> busiestServers(int k, vector<int>& a, vector<int>& val) {\n        vector<tuple<int, int, int>> events;\n        int n = (int) a.size();\n        for (int i = 0; i < n; i++) {\n            events.emplace_back(a[i] + val[i], -1, i);\n            events.emplace_back(a[i], 1, i);\n        }\n        sort(events.begin(), events.end());\n        set<int> st;\n        for (int i = 0; i < k; i++) {\n            st.emplace(i);\n        }\n        vector<int> take(n, -1);\n        vector<int> freq(k);\n        int mxFreq = 0;\n        for (auto& [_, tp, i] : events) {\n            if (tp == -1) {\n                if (take[i] == -1) {\n                    continue;\n                }\n                mxFreq = max(mxFreq, ++freq[take[i]]);\n                st.emplace(take[i]);\n            } else if (tp == 1) {\n                auto it = st.lower_bound(i % k);\n                if (it != st.end()) {\n                    take[i] = *it;\n                    st.erase(take[i]);\n                    continue;\n                }\n                auto it2 = st.lower_bound(0);\n                if (it2 != st.end()) {\n                    take[i] = *it2;\n                    st.erase(take[i]);\n                }\n            }\n        }\n        vector<int> servers;\n        for (int i = 0; i < k; i++) {\n            if (freq[i] == mxFreq) {\n                servers.emplace_back(i);\n            }\n        }\n        return servers;\n    }\n};\n```
['C++']