Easy to Understand - C++ | O(nlogn) ✅ ✅ ✅ ✅ ✅
IntuitionThe problem involves processing a series of events where users either go offline, send messages, or mention other users. We need to track mentions for
# Intuition
The problem involves processing a series of events where users either go offline, send messages, or mention other users. We need to track mentions for each user in a list. The key challenge is to handle offline users correctly and efficiently process the messages in each event. Sorting events by timestamp ensures that we process them in chronological order.

# Approach
**1. Sorting Events**: First, the events are sorted by timestamp to ensure we process them in the correct order. The sorting is done using a custom comparator that sorts by timestamp and, in the case of equal timestamps, handles the order between events based on their type.

**2. Tracking Online Status**: We maintain a boolean vector online to track whether a user is online or offline. When a user goes offline, they will be marked as offline and will return online after a certain amount of time (1 minute in this case). The offStat vector stores the time at which a user will be considered online again.

**3. Processing Messages**: The event types could be:

- **OFFLINE**: Mark the user as offline and update their offline status in offStat.
- **MESSAGE**:  
    - **"ALL"**: All users are mentioned, so increment the mention count for every user.
    - **"HERE"**: Only increment the mention count for users who are currently online.
    - **User Mentions**: Parse the message to extract user IDs and increment their mention count.

**4. Efficiency Considerations**: By processing events in order and using efficient data structures like vectors and unordered sets, we ensure that the solution performs well even with larger input sizes.

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

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

# Code
```cpp []
class Solution {
public:
    static bool comparator(vector<string> &a,vector<string> &b){
        return a[1]==b[1] ? a[0][0]>b[0][0] :stoi(a[1])<stoi(b[1]);
    }
    vector<int> countMentions(int numberOfUsers, vector<vector<string>>& events) {
        std::ios_base::sync_with_stdio(false);
        std::cin.tie(NULL);

        vector<int> mentions(numberOfUsers, 0);
        vector<bool> online(numberOfUsers, true);
        vector<long long> offStat(numberOfUsers, 0);
        
        sort(events.begin(), events.end(),comparator);

            
        for (const auto& event : events) {
            long long timestamp = stoll(event[1]);
            
            for (int i = 0; i < numberOfUsers; ++i) {
                if (offStat[i] > 0 && timestamp >= offStat[i]) {
                    online[i] = true;
                    offStat[i] = 0;
                }
            }
            
            if (event[0] == "OFFLINE") {
                int userId = stoi(event[2]);
                online[userId] = false;
                offStat[userId] = timestamp + 60;
            }
            
            if (event[0] == "MESSAGE") {
                unordered_set<int> mentionedUsers;
                istringstream iss(event[2]);
                string token;
                
                if (event[2] == "ALL") {
                    for (int i = 0; i < numberOfUsers; ++i) {
                        mentions[i]++;
                    }
                }
                else if (event[2] == "HERE") {
                    for (int i = 0; i < numberOfUsers; ++i) {
                        if (online[i]) {
                            mentions[i]++;
                        }
                    }
                }
                else {
                    while (iss >> token) {
                        if (token.substr(0, 2) == "id") {
                            int userId = stoi(token.substr(2));
                            mentions[userId]++;
                        }
                    }
                }
            }
        }
        
        return mentions;
    }
};
```
['C++']