Beats 100% with optimization: keep track of offline users using queue and set
IntuitionWe keep track of offline users in a queue with a set.
For every event, we process the queue to ensure that users go back online correctly according to
# Intuition
We keep track of offline users in a queue with a set.
For every event, we process the queue to ensure that users go back online correctly according to the time.

One optimization: we use `cnt` to count the mentions to add to all users, and only update the mentions only once in the end `[v + cnt for v in ans]`.

Since we track the set of offline users, when a "HERE" message arrives, we can increase `cnt` by 1 and decrease offline users' count by 1.

The code below should be pretty self-explanatory. Note that the events should be sorted. The complexity is $$O(m \log(m))$$ (sorting) + $$O(n)$$.

# Code
```python3 []
class Solution:
    def countMentions(self, n: int, events: List[List[str]]) -> List[int]:
        q = deque()
        offs = set()
        ans = [0] * n
        cnt = 0

        events.sort(key = lambda e: (int(e[1]), int(e[0] == 'MESSAGE')))
        for m, t, s in events:
            t = int(t)
            while q and q[0][0] <= t:
                _, u = q.popleft()
                offs.remove(u)
            if m == "OFFLINE":
                q.append((t + 60, int(s)))
                offs.add(int(s))
            else:
                if s == 'ALL':
                    cnt += 1
                elif s == 'HERE':
                    cnt += 1
                    for i in offs:
                        ans[i] -= 1
                else:
                    ids = [int(idu[2:]) for idu in s.split()]
                    for i in ids:
                        ans[i] += 1

        return [v + cnt for v in ans]
```
['Queue', 'Python3']