Multiset | Sorting
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

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

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
private:
    static bool comp(const vector<string> &a, const vector<string> &b){
        if(stoi(a[1]) == stoi(b[1])){
            return a[0] > b[0];
        }
        return stoi(a[1]) < stoi(b[1]);
    }
public:
    vector<int> countMentions(int num, vector<vector<string>>& events) {
        int n = events.size();
        vector<int> ans(num);

        sort(events.begin(), events.end(), comp);
        multiset<pair<int, int>> mp; // wait time for not allowed users

        int cnt = 0; // inc all by 1

        for(auto it : events){
            string event = it[0];
            string time = it[1];
            string users = it[2];

            if(event=="MESSAGE"){
                // 3 cases : id, ALL, HERE

                if(users=="ALL"){
                    cnt++;
                }
                else if(users=="HERE"){
                    int currt = stoi(time);
                    for(auto it : mp){
                        if(it.first > currt){
                            ans[it.second]--;
                        }
                    }
                    cnt++;
                }
                else{
                    // id
                    int i=0;
                    while(i<users.size()){
                        if(users[i]==' '){
                            i++;
                        }
                        else{
                            if(users[i]=='i'){
                                i+=2;
                                string s = "";
                                while( i<users.size() && users[i]!=' '){
                                    s += users[i];
                                    i++;
                                }
                                int dig = stoi(s);
                                ans[dig]++;
                            }
                        }
                    }
                }
            }
            else{
                // OFFLINE
                int u = stoi(users);
                int t = stoi(time);
                mp.insert({t+60, u});
            }
        }
        for(int i=0; i<num; i++){
            ans[i]+= cnt;
        }
        return ans;
    }
};
```
['C++']