#K95022. Active Sessions in Chat Logs

    ID: 38771 Type: Default 1000ms 256MiB

Active Sessions in Chat Logs

Active Sessions in Chat Logs

You are given a chat log consisting of n messages. Each message is represented as a record containing a user ID, a timestamp (in minutes), and a message string.

An active session for a user is defined as a contiguous sequence of messages where the difference between any two consecutive messages is at most \(k\) minutes. If the gap between two messages exceeds \(k\) minutes, then a new session is counted for that user.

Your task is to process the chat log and for each user output the number of active sessions they have. The output should list the users in increasing order of their user IDs. Each output line should display the user ID followed by the corresponding active session count.

Example: For the following log with n = 5 and k = 10:

1 10 Hello
2 15 Hi
1 20 How_are_you?
1 50 Goodbye
2 60 See_you

The active session count would be 2 for user 1 and 2 for user 2, because the gap between messages for each user that exceeds 10 minutes starts a new session.

inputFormat

The first line of input contains two integers n and k, separated by a space, where n is the number of messages and k is the maximum allowed minute difference within a session.

The next n lines each contain a message record with three parts:

  • An integer user_id.
  • An integer timestamp (in minutes).
  • A string message (without spaces or with underscores instead of spaces).

You can assume that each record is well-formed.

outputFormat

For each user that appears in the logs, output a line in the format:

user_id active_session_count

Users should be listed in increasing order of their user IDs.

## sample
2 10
1 10 Hello
1 15 Hi
1 1