#P15081. [ICPC 2024 Chengdu R] Grand Prix of Ballance

[ICPC 2024 Chengdu R] Grand Prix of Ballance

题目描述

Ballance is a classic game where players use the keyboard to control a ball through complex structures high above the ground, avoiding falls while solving puzzles to reach the end of each level. Recently, the player community has developed online mods and hosts regular online competitions, such as the Grand Prix of Ballance.

The Grand Prix consists of nn levels, numbered from 11 to nn, with mm participants, numbered from 11 to mm. The competition uses a point system: players earn points based on their ranking in each level, and the sum of their points across all levels determines the final standings. Each level has a designated start time, and participants must complete the level as quickly as possible. As a staff member, you receive a server log during the match containing three types of messages (it is guaranteed that 1xn1\le x\le n and 1idm1\le id\le m):

  • 1 x\tt{1\ x} --- Type 1: the match on Level xx has started.
  • 2 id x\tt{2\ id\ x} --- Type 2: participant indexed idid has completed Level xx.
  • 3 id x\tt{3\ id\ x} --- Type 3: participant indexed idid voluntarily gives up completing Level xx.

A Type 1 message indicates the start of Level xx until a new Type 1 message appears for a different level. Only messages that correspond to the currently active level are considered valid; all messages for other levels should be ignored. Messages before the first Type 1 message are also ignored. Each level appears at most once in a Type 1 message.

Each player may yield multiple Type 2 and Type 3 messages per level, but only the first valid message counts. Specifically:

  • Messages are ignored if they do not match the active level indicated by the Type 1 message.
  • If a player's first valid message for a level is Type 2, they are considered to have completed the level successfully at that moment, and the player's any subsequent messages for that level are ignored.
  • If a player's first valid message for a level is Type 3, they are considered to have given up completing the level at that moment, and the player's any subsequent messages for that level are ignored.
  • If a player yields no messages for a level, they are considered not to have completed the level.

Points are awarded to participants who complete the level as follows: the first player to complete the level receives mm points, the second receives m1m-1 points, and so on. Participants who do not complete the level, including those who give up, receive no points.

Your task is to output the current total points of each participant in descending order based on the log. If multiple participants have the same points, they should be listed in ascending order by their index.

输入格式

The first line contains an integer TT (1T1041 \leq T \leq 10^4), indicating the number of test cases.

For each test case, the first line contains three integers nn, mm, and qq (1n1051 \leq n \leq 10^5, 2m1052 \leq m \leq 10^5, 1q21051 \leq q \leq 2 \cdot 10^5), indicating the number of levels, participants, and log messages, respectively.

The following qq lines contain the log messages as specified above. Of course, the messages are presented in chronological order. The log may not contain all levels, as you may receive it midway through the competition. You only need to process the current results.

It is guaranteed that the sum of nn, the sum of mm, and the sum of qq over all test cases do not exceed 51055 \cdot 10^5, respectively.

输出格式

For each test case, output mm lines, each containing two integers: idid and xx, where idid is the participant's index and xx is their total points, sorted in descending order of points. If multiple participants have the same points, list them in ascending order by their index.

3
3 4 6
1 2
2 1 1
2 2 2
3 3 2
2 3 2
2 1 2
3 4 8
1 2
2 1 1
2 2 2
3 3 2
2 3 2
2 1 2
1 1
2 1 1
3 4 7
1 2
2 1 1
2 2 2
3 3 2
2 3 2
2 1 2
1 1
2 4
1 3
3 0
4 0
1 7
2 4
3 0
4 0
2 4
1 3
3 0
4 0