Sorted Event Processing (O(n log n))
IntuitionThe problem revolves around tracking user mentions based on event types and user statuses (online/offline). Initially, all users are online. We need to
# Intuition
The problem revolves around tracking user mentions based on event types and user statuses (online/offline). Initially, all users are online. We need to update their status dynamically as offline events occur and count the mentions for each user from message events. Offline users will automatically be brought back online after 60 time units, and message events that happen at the same timestamp should consider the current online status of users. The challenge is to correctly account for both user status and the multiple types of mentions (all, here, specific users) while processing the events in chronological order.

# Approach
`Data Structures:`
1. Use a set<int> called online to store currently online users.
1. Use a set<pair<int, int>> called offline to track users who are offline and their respective offline times.
3. Maintain a vector ans to store the mention count for each user.


`Sorting Events:`
1. The events are sorted by their timestamps to ensure that events are processed in chronological order. A custom comparison function handles this sorting, ensuring "OFFLINE" events are processed first when timestamps are the same.


`Event Processing:`
1. For each event:
    1. Offline Events: If a user goes offline, we record their offline time and mark them as offline by removing them from the online set.
    2. Message Events: If a message mentions users, we:
        1. For "ALL" mentions, increment the mention count for all users.
        2. For "HERE" mentions, only increment the mention count for currently online users.
        3. For specific user mentions (e.g., id12), extract the IDs and update the mention count for the mentioned users.


`Handling Offline Users:`
1. As events are processed, if the current timestamp is greater than the offline time of any user in the offline set, the user is brought back online (removed from offline and added back to online).

By sorting the events and processing them one by one, we ensure that all users' mentions and statuses are accurately tracked. The final result is a vector of mention counts for each user.

# Complexity
- Time complexity:
O(nlogn+n⋅m).

- Space complexity:
O(n).

# Code
```cpp []
class Solution {
public:
    vector<int> countMentions(int n, vector<vector<string>>& events) {
        set<int> online;
        //{id,off-time}
        set<pair<int,int>> offline;
        vector<int>ans(n,0);
        for(int i=0;i<n;i++)
        online.insert(i);
        sort(events.begin(),events.end(),comp);
        int currTime=0;
        for(auto event :events)
        {
            currTime = stoi(event[1]);
            for (auto it = offline.begin(); it != offline.end(); ) {
                int off_id = it->first;
                int off_time = it->second;
                if (off_time + 60 <= currTime) {
                    online.insert(off_id);
                    it = offline.erase(it);  // Erase and get next iterator
                } else {
                    ++it;
                }
            }
            int eventType = (event[0]=="MESSAGE")?1:0;//0
            int mentionType = (event[2]=="ALL")?1:(event[2]=="HERE")?2:3;//3
            int eventTime = currTime;//1
            if(eventType==0)
            {
                int offlineTime = currTime;
                int offlineId = stoi(event[2]);
                offline.insert({offlineId,offlineTime});//[{0,1},{}]
                online.erase(offlineId);//[1,2]
            }
            else
            {
                if(mentionType==1)
                {
                    for(int i=0;i<n;i++)
                    ans[i]++;
                }
                else if(mentionType==2)
                {
                    for(auto on:online)
                    ans[on]++;
                }
                else
                {
                    int mentionLen = event[2].size();
                    for(int k=0;k<mentionLen;k++)
                    {
                        if(event[2][k]=='i' && k+2<mentionLen)
                        {
                            int id = stoi(event[2].substr(k + 2));
                            ans[id]++;
                        }
                    }
                }
            }
        }
        return ans;
    }
    static bool comp(const vector<string>& a, const vector<string>& b)
    {
        int at = stoi(a[1]);
        int bt = stoi(b[1]);
        //off-0,message-1
        int atype = (a[0]=="MESSAGE")?1:0;
        int btype = (b[0]=="MESSAGE")?1:0;
        if(at==bt)
        return atype<btype;
        return at<bt;
    }
};
```
['C++']