QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 512 MB Puntuación total: 100

#3784. Boxes in a Line

Estadísticas

You have a row of boxes, numbered $1, 2, 3, \dots, n$ from left to right. You can perform four types of instructions:

  • 1 X Y: Move box $X$ to the left of box $Y$ (if $X$ is already to the left of $Y$, ignore this instruction).
  • 2 X Y: Move box $X$ to the right of box $Y$ (if $X$ is already to the right of $Y$, ignore this instruction).
  • 3 X Y: Swap the positions of box $X$ and box $Y$.
  • 4: Reverse the entire chain.

The instructions are guaranteed to be valid, meaning $X \neq Y$. For example, when $n=6$, starting from the initial state and executing 1 1 4, the sequence of boxes becomes $2, 3, 1, 4, 5, 6$. Next, executing 2 3 5, the sequence becomes $2, 1, 4, 5, 3, 6$. Then, executing 3 1 6 results in $2, 6, 4, 5, 3, 1$. Finally, executing 4 results in $1, 3, 5, 4, 6, 2$.

Input

The input contains no more than 10 test cases. Each test case starts with a line containing the number of boxes $n$ and the number of instructions $m$ ($1 \le n, m \le 100,000$), followed by $m$ lines, each containing one instruction.

Output

For each test case, output one line containing the sum of the box numbers at all odd positions. Positions are numbered $1$ to $n$ from left to right.

Examples

Input 1

6 4
1 1 4
2 3 5
3 1 6
4
6 3
1 1 4
2 3 5
3 1 6
100000 1
4

Output 1

Case 1: 12
Case 2: 9
Case 3: 2500050000

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.