Using Map and set
IntuitionWe use two data structures:ApproachOperations:Complexity
Time complexity:
Add Task - 	O(log⁡k)
Edit Task - O(log⁡k)
Remove Task -O(log⁡k)
Execute Top -
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We use two data structures:

    PriorityMap: A map that maps priority to a set of {taskId, userId} pairs.
        The map automatically keeps priorities sorted in descending order.
        The set ensures tasks with the same priority are sorted by taskId in ascending order, which simplifies the tie-breaking condition.

    TaskMap: A unordered_map that maps taskId to {priority, userId}.
        This allows O(1) access to a task's priority and user ID for operations like edit and removal.

# Approach
<!-- Describe your approach to solving the problem. -->
Operations:

    Add a Task:
        Insert {taskId, userId} into the set of the corresponding priority in PriorityMap.
        Store {priority, userId} in TaskMap.

    Edit a Task's Priority:
        Retrieve the task's current priority and user ID using TaskMap.
        Remove the task from the old priority's set in PriorityMap.
        Add the task to the new priority's set and update its priority in TaskMap.

    Remove a Task:
        Retrieve the task's current priority and user ID using TaskMap.
        Remove the task from the old priority's set in PriorityMap.
        Delete the task from TaskMap.

    Execute the Top Task:
        Retrieve the task with the highest priority using the first element in PriorityMap.
        Use rbegin() to get the task with the largest taskId within that priority.
        Remove the task from both PriorityMap and TaskMap.
        Return the associated userId. If no tasks are available, return -1.
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
Add Task - 	$$O(log⁡k)$$
Edit Task - $$O(log⁡k)$$
Remove Task -$$O(log⁡k)$$
Execute Top - $$O(log⁡k)$$

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
$$O(n)$$
# Code
```cpp []

class TaskManager {
public:
    map<int, set<pair<int, int>>, greater<int>> PriorityMap;
    unordered_map<int, pair<int, int>> TaskMap;

    TaskManager(vector<vector<int>> &tasks) {
        for (auto &task : tasks) {
            int userId = task[0];
            int taskId = task[1];
            int priority = task[2];
            add(userId, taskId, priority);
        }
    }

    void add(int userId, int taskId, int priority) {
        PriorityMap[priority].emplace(taskId, userId);
        TaskMap[taskId] = {priority, userId};
    }

    void edit(int taskId, int newPriority) {
        auto [currentPriority, userId] = TaskMap[taskId];
        PriorityMap[currentPriority].erase({taskId, userId});
        if (PriorityMap[currentPriority].empty()) {
            PriorityMap.erase(currentPriority);
        }
        PriorityMap[newPriority].emplace(taskId, userId);
        TaskMap[taskId] = {newPriority, userId};
    }

    void rmv(int taskId) {
        auto [currentPriority, userId] = TaskMap[taskId];
        PriorityMap[currentPriority].erase({taskId, userId});
        if (PriorityMap[currentPriority].empty()) {
            PriorityMap.erase(currentPriority);
        }
        TaskMap.erase(taskId);
    }

    int execTop() {
        if (PriorityMap.empty()) {
            return -1;
        }

        auto it = PriorityMap.begin();
        auto [taskId, userId] = *it->second.rbegin();
        it->second.erase({taskId, userId});
        if (it->second.empty()) {
            PriorityMap.erase(it);
        }
        TaskMap.erase(taskId);
        return userId;
    }
};
```
['Ordered Map', 'Ordered Set', 'C++']