Easy C++ solution using sorting and event handling.
IntuitionWe can solve this problem using sorting and event handling.ApproachMy solution follows basic approach as follow:

Sort the events based on time stamp.
# Intuition
 We can solve this problem using sorting and event handling.

# Approach
My solution follows basic approach as follow:
1. Sort the events based on time stamp.

    NOTE: When timestamp is same for two events then first handle the events that may get online at that time before procceding for anothet MESSAGE event at that time stamp.

2. When event is having type = "OFFLINE" then update online array and offlineUntill array.

3. When event is having type = "MESSAGE" then we have to handle three cases of mention string that are "ALL", "HERE" and string with id<number>.

4. At last return mention array.


# Complexity
- Time complexity: O(nlogn)


- Space complexity: O(n)


# Code
```cpp []
class Solution {
private:
    // Sort events by timestamp; if timestamps are equal, sort by type ("OFFLINE" > "MESSAGE")
    void sortEvents(vector<vector<string>>& events) {
        sort(events.begin(), events.end(),
             [](const vector<string>& a, const vector<string>& b) {
                 int timeA = stoi(a[1]);
                 int timeB = stoi(b[1]);
                 if (timeA == timeB) {
                     return a[0] > b[0];
                 }
                 return timeA < timeB;
             });
    }

public:
    vector<int> countMentions(int n, vector<vector<string>>& events) {
        sortEvents(events);

        vector<int> mentions(n, 0);
        vector<int> offlineUntil(n, 0);
        unordered_set<int> onlineUsers;

        // Initialize all users as online initially
        for (int i = 0; i < n; i++) {
            onlineUsers.insert(i);
        }

        for (const auto& event : events) {
            string type = event[0];
            int timestamp = stoi(event[1]);

            if (type == "OFFLINE") {
                int userId = stoi(event[2]);
                offlineUntil[userId] = timestamp + 60; // User is offline for 60 seconds
                onlineUsers.erase(userId);
            } else if (type == "MESSAGE") {
                string mentionsString = event[2];

                // Reactivate users who are no longer offline
                for (int i = 0; i < n; i++) {
                    if (offlineUntil[i] <= timestamp && onlineUsers.find(i) == onlineUsers.end()) {
                        onlineUsers.insert(i);
                    }
                }

                if (mentionsString == "ALL") {
                    // Increment mentions for all users
                    for (int i = 0; i < n; i++) {
                        mentions[i]++;
                    }
                } else if (mentionsString == "HERE") {
                    // Increment mentions only for online users
                    for (int userId : onlineUsers) {
                        mentions[userId]++;
                    }
                } else {
                    // Parse specific mentions in the format "idX"
                    stringstream ss(mentionsString);
                    string token;
                    while (ss >> token) {
                        if (token.size() > 2 && token.substr(0, 2) == "id") {
                            int userId = stoi(token.substr(2));
                            mentions[userId]++;
                        }
                    }
                }
            }
        }

        return mentions;
    }
};

```
['C++']