/
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 266 点
問題文
高橋君は \(N\) 個のサイコロを一直線に並んだマス目の上で転がす実験を行っています。
マス目は、\(1\) から \(M\) までの番号が付いた \(M\) 個のマスが一列に並んだものです。マス番号は一方の端から順に \(1,2,\dots,M\) と振られています。
各サイコロ \(i\)(\(1 \leq i \leq N\))は、初期位置としてマス \(S_i\) に置かれています。複数のサイコロが同じマスに置かれることもありますが、サイコロ同士は互いに影響を及ぼさず、それぞれ独立に扱われます。
各サイコロは一般的なサイコロであり、\(6\) つの面にそれぞれ \(1\) から \(6\) の数が \(1\) つずつ書かれています。向かい合う \(2\) 面の数の和はすべて \(7\) です。
この問題では、サイコロの姿勢を上面の数と前面の数の組で管理します。前面とは、マス番号が増える方向を向いている面のことです。
サイコロ \(i\) の初期状態において、上面の数は \(U_i\)、前面の数は \(F_i\) です。上面と前面は同一の面でも向かい合う面でもないため、\(U_i \neq F_i\) かつ \(U_i+F_i \neq 7\) が保証されます。
高橋君は \(Q\) 回の操作を順番に行います。\(j\) 回目(\(1 \leq j \leq Q\))の操作では、サイコロ番号 \(d_j\) のサイコロを方向 \(c_j\)(+ または -)に \(1\) マスだけ転がします。
転がす直前のそのサイコロの上面の数を \(u\)、前面の数を \(f\) としたとき、位置と姿勢は以下の規則に従って変化します。
+方向(マス番号が増える方向)に転がす場合:- 位置が \(1\) 増える。
- 新しい上面の数は \(7-f\)、新しい前面の数は \(u\) となる。
- これは、転がす前に前面だった面が下面、背面だった面が上面、上面だった面が前面になる回転です。
-方向(マス番号が減る方向)に転がす場合:- 位置が \(1\) 減る。
- 新しい上面の数は \(f\)、新しい前面の数は \(7-u\) となる。
- これは、
+方向と逆の回転であり、転がす前に背面だった面が下面、前面だった面が上面、上面だった面が背面になる回転です。
すべての操作を通じて、各サイコロの位置は常に \(1\) 以上 \(M\) 以下の範囲内に留まることが保証されます。
すべての \(Q\) 回の操作を終えた後、各サイコロの最終的な位置と上面の数を求めてください。前面の数を出力する必要はありません。
制約
- \(1 \leq N \leq 2\times 10^5\)
- \(1 \leq M \leq 10^9\)
- \(1 \leq Q \leq 2\times 10^5\)
- \(N+Q \leq 2\times 10^5\)
- \(1 \leq S_i \leq M\)(\(1 \leq i \leq N\))
- \(1 \leq U_i \leq 6\)(\(1 \leq i \leq N\))
- \(1 \leq F_i \leq 6\)(\(1 \leq i \leq N\))
- \(U_i \neq F_i\) かつ \(U_i+F_i \neq 7\)(\(1 \leq i \leq N\))
- \(1 \leq d_j \leq N\)(\(1 \leq j \leq Q\))
- \(c_j\) は
+または-(\(1 \leq j \leq Q\)) - 操作の結果、サイコロの位置が \(1\) 未満または \(M\) を超えることはない
- \(c_j\) を除き、入力はすべて整数で与えられる
入力
入力は以下の形式で標準入力から与えられる。
\(N\) \(M\) \(Q\) \(S_1\) \(U_1\) \(F_1\) \(S_2\) \(U_2\) \(F_2\) \(\vdots\) \(S_N\) \(U_N\) \(F_N\) \(d_1\) \(c_1\) \(d_2\) \(c_2\) \(\vdots\) \(d_Q\) \(c_Q\)
- \(1\) 行目には、サイコロの個数 \(N\)、マス目の数 \(M\)、操作の回数 \(Q\) がスペース区切りで与えられる。
- 続く \(N\) 行のうち \(i\) 行目(\(1 \leq i \leq N\))には、サイコロ \(i\) の初期位置 \(S_i\)、初期の上面の数 \(U_i\)、初期の前面の数 \(F_i\) がスペース区切りで与えられる。
- 続く \(Q\) 行のうち \(j\) 行目(\(1 \leq j \leq Q\))には、転がすサイコロの番号 \(d_j\) と転がす方向 \(c_j\)(
+または-)がスペース区切りで与えられる。
出力
\(N\) 行出力せよ。
\(i\) 行目(\(1 \leq i \leq N\))には、サイコロ \(i\) の最終的な位置と上面の数をスペース区切りで出力せよ。
入力例 1
1 5 3 2 1 2 1 + 1 + 1 -
出力例 1
3 5
入力例 2
3 6 7 1 1 2 6 6 5 3 3 1 1 + 2 - 3 - 3 + 1 + 2 + 1 -
出力例 2
2 5 6 6 3 3
入力例 3
5 20 15 1 1 3 20 2 4 10 3 2 5 4 1 15 5 3 1 + 2 - 3 + 4 - 5 + 1 + 3 + 2 - 5 - 4 + 1 - 3 - 2 + 5 + 4 -
出力例 3
2 4 19 4 11 5 4 1 16 4
入力例 4
10 1000000000 36 1 1 2 1000000000 6 4 500000000 2 3 999999999 3 1 123456789 4 5 987654321 5 1 2 1 3 999999998 2 4 400000000 3 6 600000000 6 2 1 + 2 - 7 - 8 + 4 + 3 + 5 - 6 + 9 + 10 - 1 - 2 + 7 + 8 + 4 - 3 + 5 + 6 - 9 - 10 + 1 + 2 - 7 - 8 - 4 + 3 - 5 - 6 + 9 + 10 - 1 + 2 - 7 + 8 + 4 - 3 +
出力例 4
3 6 999999998 1 500000002 5 999999999 3 123456788 5 987654322 6 2 1 1000000000 5 400000001 1 599999999 2
入力例 5
1 2 15 1 1 2 1 + 1 - 1 + 1 - 1 + 1 - 1 + 1 - 1 + 1 - 1 + 1 - 1 + 1 - 1 +
出力例 5
2 5
Score : 266 pts
Problem Statement
Takahashi is conducting an experiment by rolling \(N\) dice on a line of squares.
The squares are arranged in a row, numbered \(1\) through \(M\). The square numbers are assigned sequentially from one end as \(1,2,\dots,M\).
Each die \(i\) (\(1 \leq i \leq N\)) is initially placed on square \(S_i\). Multiple dice may be placed on the same square, but the dice do not affect each other and are treated independently.
Each die is a standard six-sided die, with numbers \(1\) through \(6\) written on its six faces, one per face. The sum of the numbers on opposite faces is always \(7\).
In this problem, the orientation of a die is represented by a pair of the top face number and the front face number. The front face is the face facing in the direction of increasing square numbers.
In the initial state of die \(i\), the top face number is \(U_i\) and the front face number is \(F_i\). Since the top and front faces are neither the same face nor opposite faces, it is guaranteed that \(U_i \neq F_i\) and \(U_i+F_i \neq 7\).
Takahashi performs \(Q\) operations in order. In the \(j\)-th operation (\(1 \leq j \leq Q\)), he rolls die \(d_j\) by \(1\) square in direction \(c_j\) (+ or -).
When rolling a die, let \(u\) be the top face number and \(f\) be the front face number immediately before the roll. The position and orientation change according to the following rules:
- Rolling in the
+direction (direction of increasing square numbers): - The position increases by \(1\).
- The new top face number becomes \(7-f\), and the new front face number becomes \(u\).
- This corresponds to a rotation where the face that was the front becomes the bottom, the face that was the back becomes the top, and the face that was the top becomes the front.
- Rolling in the
-direction (direction of decreasing square numbers): - The position decreases by \(1\).
- The new top face number becomes \(f\), and the new front face number becomes \(7-u\).
- This corresponds to the reverse rotation of the
+direction: the face that was the back becomes the bottom, the face that was the front becomes the top, and the face that was the top becomes the back.
It is guaranteed that throughout all operations, the position of each die remains within the range of \(1\) to \(M\), inclusive.
After completing all \(Q\) operations, find the final position and top face number for each die. You do not need to output the front face number.
Constraints
- \(1 \leq N \leq 2\times 10^5\)
- \(1 \leq M \leq 10^9\)
- \(1 \leq Q \leq 2\times 10^5\)
- \(N+Q \leq 2\times 10^5\)
- \(1 \leq S_i \leq M\) (\(1 \leq i \leq N\))
- \(1 \leq U_i \leq 6\) (\(1 \leq i \leq N\))
- \(1 \leq F_i \leq 6\) (\(1 \leq i \leq N\))
- \(U_i \neq F_i\) and \(U_i+F_i \neq 7\) (\(1 \leq i \leq N\))
- \(1 \leq d_j \leq N\) (\(1 \leq j \leq Q\))
- \(c_j\) is
+or-(\(1 \leq j \leq Q\)) - As a result of operations, the position of any die will never be less than \(1\) or exceed \(M\).
- All input values except \(c_j\) are integers.
Input
Input is given from Standard Input in the following format:
\(N\) \(M\) \(Q\) \(S_1\) \(U_1\) \(F_1\) \(S_2\) \(U_2\) \(F_2\) \(\vdots\) \(S_N\) \(U_N\) \(F_N\) \(d_1\) \(c_1\) \(d_2\) \(c_2\) \(\vdots\) \(d_Q\) \(c_Q\)
- The 1st line contains the number of dice \(N\), the number of squares \(M\), and the number of operations \(Q\), separated by spaces.
- The following \(N\) lines contain the initial configuration of die \(i\) (\(1 \leq i \leq N\)): the initial position \(S_i\), the initial top face number \(U_i\), and the initial front face number \(F_i\), separated by spaces.
- The following \(Q\) lines contain the operations: the \(j\)-th line (\(1 \leq j \leq Q\)) contains the die index \(d_j\) to roll and the direction \(c_j\) (
+or-), separated by spaces.
Output
Print \(N\) lines.
In the \(i\)-th line (\(1 \leq i \leq N\)), print the final position and top face number of die \(i\), separated by a space.
Sample Input 1
1 5 3 2 1 2 1 + 1 + 1 -
Sample Output 1
3 5
Sample Input 2
3 6 7 1 1 2 6 6 5 3 3 1 1 + 2 - 3 - 3 + 1 + 2 + 1 -
Sample Output 2
2 5 6 6 3 3
Sample Input 3
5 20 15 1 1 3 20 2 4 10 3 2 5 4 1 15 5 3 1 + 2 - 3 + 4 - 5 + 1 + 3 + 2 - 5 - 4 + 1 - 3 - 2 + 5 + 4 -
Sample Output 3
2 4 19 4 11 5 4 1 16 4
Sample Input 4
10 1000000000 36 1 1 2 1000000000 6 4 500000000 2 3 999999999 3 1 123456789 4 5 987654321 5 1 2 1 3 999999998 2 4 400000000 3 6 600000000 6 2 1 + 2 - 7 - 8 + 4 + 3 + 5 - 6 + 9 + 10 - 1 - 2 + 7 + 8 + 4 - 3 + 5 + 6 - 9 - 10 + 1 + 2 - 7 - 8 - 4 + 3 - 5 - 6 + 9 + 10 - 1 + 2 - 7 + 8 + 4 - 3 +
Sample Output 4
3 6 999999998 1 500000002 5 999999999 3 123456788 5 987654322 6 2 1 1000000000 5 400000001 1 599999999 2
Sample Input 5
1 2 15 1 1 2 1 + 1 - 1 + 1 - 1 + 1 - 1 + 1 - 1 + 1 - 1 + 1 - 1 + 1 - 1 +
Sample Output 5
2 5