/
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 533 点
問題文
N 個の都市があり、初めはどの都市間にも両替所は存在しない。
これから Q 個の操作が順に与えられる。任意の時点で両替所の配置は森(閉路を持たないグラフ)を成すことが保証される。したがって、同じ連結成分に属する任意の 2 都市間の経路はちょうど 1 つである。
各両替所は 2 つの都市を結ぶ辺であり、通過する方向によって異なるアフィン変換(t \mapsto at + b)を持つ。すべての演算は \bmod{998244353} 上の整数として行う。
各両替所には、開設時に定められた 方向 がある。操作 L p q a b c d で開設された両替所の方向は「p 側 \to q 側」であり、この方向は両替所が閉鎖されるまで変わらない。
各操作は次のいずれかである。
L u v a b c d(開設):都市 u と都市 v の間に新しい両替所を開設する。この時点で u と v は異なる連結成分に属することが保証される。この両替所の方向は「u 側 \to v 側」と定められ、u 側から v 側への両替は所持金 t を at + b \pmod{998244353} に、v 側から u 側への両替は所持金 t を ct + d \pmod{998244353} に変換する。
C u v(閉鎖):都市 u と都市 v を直接結ぶ両替所を閉鎖する。この時点でその両替所が存在することが保証される。引数 u, v の順序は開設時と異なっていてもよい。閉鎖により u と v は異なる連結成分に分かれる。
U u v a b c d(更新):都市 u と都市 v を直接結ぶ両替所の為替レートを更新する。この時点でその両替所が存在することが保証される。引数 u, v の順序は開設時と異なっていてもよい。
この両替所が操作 L p q ... によって開設されたものなら、更新後は p 側から q 側への両替が t \mapsto at + b \pmod{998244353}、q 側から p 側への両替が t \mapsto ct + d \pmod{998244353} となる。
Q x y s(照会):都市 x と都市 y が同じ連結成分に属する場合、x から y への唯一の経路 x = v_0, v_1, \ldots, v_k = y に沿って、辺 v_0v_1, v_1v_2, \ldots, v_{k-1}v_k に対応する変換を初期所持金 s に順に適用した結果を \bmod{998244353} で出力せよ。
x = y の場合は k = 0 であり、変換は適用されないため s をそのまま出力する。
x と y が同じ連結成分に属さない場合は -1 を出力せよ。
制約
- 1 \leq N \leq 5 \times 10^4
- 1 \leq Q \leq 5 \times 10^4
- 操作
L u v a b c dにおいて、1 \leq u, v \leq N、u \neq v、0 \leq a, b, c, d \leq 998244352 - 操作
C u vにおいて、1 \leq u, v \leq N、u \neq v - 操作
U u v a b c dにおいて、1 \leq u, v \leq N、u \neq v、0 \leq a, b, c, d \leq 998244352 - 操作
Q x y sにおいて、1 \leq x, y \leq N、0 \leq s \leq 998244352 - 任意の時点で、両替所の配置は森を成す
- 操作はすべて正当である(上記の各操作の保証条件を満たす)
- 入力はすべて整数である
入力
N Q
\text{op}_1
\text{op}_2
:
\text{op}_Q
1 行目には、都市の数 N と操作の数 Q がスペース区切りで与えられる。続く Q 行に各操作が 1 行ずつ与えられる。各操作は以下のいずれかの形式である。
L u v a b c d C u v U u v a b c d Q x y s
出力
各 Q 操作に対して 1 行ずつ、求める値を出力せよ。x と y が同じ連結成分に属さない場合は -1 を出力せよ。
入力例 1
4 8 L 1 2 2 3 5 7 Q 1 2 10 L 2 3 4 1 6 0 Q 1 3 10 U 2 3 3 3 2 2 Q 3 1 5 C 1 2 Q 1 3 10
出力例 1
23 93 67 -1
入力例 2
5 8 Q 1 2 0 L 1 2 1 5 1 6 Q 2 1 10 L 3 4 2 0 3 0 Q 1 4 7 C 2 1 Q 1 2 123 Q 3 4 8
出力例 2
-1 16 -1 -1 16
入力例 3
8 20 L 1 2 2 1 3 4 L 2 3 5 0 7 2 L 4 5 1 10 1 20 Q 1 3 6 Q 3 1 100 L 3 4 4 4 2 2 Q 1 5 9 U 3 2 6 6 8 8 Q 1 5 9 C 3 4 Q 1 5 9 L 6 7 9 1 10 2 L 7 8 11 3 12 4 Q 6 8 5 L 5 6 13 5 14 6 Q 4 8 7 C 7 8 Q 6 8 5 L 3 8 15 7 16 8 Q 1 8 9
出力例 3
65 2110 394 494 -1 509 22388 -1 1807
入力例 4
12 35 L 1 2 2 3 4 5 L 2 3 3 4 5 6 L 3 4 4 5 6 7 L 5 6 5 6 7 8 L 6 7 6 7 8 9 L 8 9 7 8 9 10 Q 1 4 11 Q 4 1 22 L 4 5 8 9 10 11 Q 1 7 33 U 4 5 9 10 11 12 Q 7 1 44 C 2 3 Q 1 7 55 L 2 8 10 11 12 13 Q 1 9 66 L 9 10 11 12 13 14 L 10 11 12 13 14 15 Q 11 1 77 C 5 6 Q 3 7 88 L 5 12 13 14 15 16 Q 3 12 99 L 7 11 14 15 16 17 Q 6 1 111 U 1 2 15 16 17 18 U 10 11 16 17 18 19 Q 6 9 222 C 4 5 L 4 12 17 18 19 20 Q 3 5 333 C 8 9 Q 1 6 444 L 8 3 18 19 20 21 Q 1 5 555
出力例 4
321 2809 204073 3347809 -1 9535 6144873 -1 47061 742065513 4390335 341221 -1 533386148
入力例 5
1 5 Q 1 1 0 Q 1 1 1 Q 1 1 998244352 Q 1 1 123456789 Q 1 1 500000000
出力例 5
0 1 998244352 123456789 500000000
Score : 533 pts
Problem Statement
There are N cities, and initially, there are no exchange offices between any cities.
We are given Q operations in order. It is guaranteed that at any point in time, the configuration of the exchange offices forms a forest (a graph with no cycles). Therefore, there is exactly one path between any two cities belonging to the same connected component.
Each exchange office is an edge connecting two cities and has a different affine transformation (t \mapsto at + b) depending on the direction of traversal. All calculations are performed as integers modulo 998244353.
Each exchange office has a direction determined when it is opened. The direction of the exchange office opened by the operation L p q a b c d is "p-side \to q-side", and this direction does not change until the exchange office is closed.
Each operation is one of the following:
L u v a b c d(Open): Open a new exchange office between city u and city v. It is guaranteed that at this moment, u and v belong to different connected components. The direction of this exchange office is defined as "u-side \to v-side". Exchanging money from the u-side to the v-side converts the holding money t into at + b \pmod{998244353}, and exchanging from the v-side to the u-side converts t into ct + d \pmod{998244353}.
C u v(Close): Close the exchange office directly connecting city u and city v. It is guaranteed that this exchange office exists at this moment. The order of the arguments u, v may differ from when it was opened. Closing it splits u and v into different connected components.
U u v a b c d(Update): Update the exchange rates of the exchange office directly connecting city u and city v. It is guaranteed that this exchange office exists at this moment. The order of the arguments u, v may differ from when it was opened.
If this exchange office was opened by an operation L p q ..., after the update, the exchange from the p-side to the q-side becomes t \mapsto at + b \pmod{998244353}, and the exchange from the q-side to the p-side becomes t \mapsto ct + d \pmod{998244353}.
Q x y s(Query): If city x and city y belong to the same connected component, output the result of applying the transformations corresponding to the edges v_0v_1, v_1v_2, \ldots, v_{k-1}v_k in order to the initial holding money s, modulo 998244353, along the unique path x = v_0, v_1, \ldots, v_k = y from x to y.
If x = y, then k = 0, and no transformations are applied, so output s as is.
If x and y do not belong to the same connected component, output -1.
Constraints
- 1 \leq N \leq 5 \times 10^4
- 1 \leq Q \leq 5 \times 10^4
- In the operation
L u v a b c d, 1 \leq u, v \leq N, u \neq v, 0 \leq a, b, c, d \leq 998244352 - In the operation
C u v, 1 \leq u, v \leq N, u \neq v - In the operation
U u v a b c d, 1 \leq u, v \leq N, u \neq v, 0 \leq a, b, c, d \leq 998244352 - In the operation
Q x y s, 1 \leq x, y \leq N, 0 \leq s \leq 998244352 - At any point in time, the configuration of the exchange offices forms a forest.
- All operations are valid (satisfying the guaranteed conditions of each operation described above).
- All input values are integers.
Input
N Q
\text{op}_1
\text{op}_2
:
\text{op}_Q
The first line contains the number of cities N and the number of operations Q, separated by a space. The subsequent Q lines each contain an operation. Each operation is in one of the following formats:
L u v a b c d C u v U u v a b c d Q x y s
Output
For each Q operation, output the required value on a single line. If x and y do not belong to the same connected component, output -1.
Sample Input 1
4 8 L 1 2 2 3 5 7 Q 1 2 10 L 2 3 4 1 6 0 Q 1 3 10 U 2 3 3 3 2 2 Q 3 1 5 C 1 2 Q 1 3 10
Sample Output 1
23 93 67 -1
Sample Input 2
5 8 Q 1 2 0 L 1 2 1 5 1 6 Q 2 1 10 L 3 4 2 0 3 0 Q 1 4 7 C 2 1 Q 1 2 123 Q 3 4 8
Sample Output 2
-1 16 -1 -1 16
Sample Input 3
8 20 L 1 2 2 1 3 4 L 2 3 5 0 7 2 L 4 5 1 10 1 20 Q 1 3 6 Q 3 1 100 L 3 4 4 4 2 2 Q 1 5 9 U 3 2 6 6 8 8 Q 1 5 9 C 3 4 Q 1 5 9 L 6 7 9 1 10 2 L 7 8 11 3 12 4 Q 6 8 5 L 5 6 13 5 14 6 Q 4 8 7 C 7 8 Q 6 8 5 L 3 8 15 7 16 8 Q 1 8 9
Sample Output 3
65 2110 394 494 -1 509 22388 -1 1807
Sample Input 4
12 35 L 1 2 2 3 4 5 L 2 3 3 4 5 6 L 3 4 4 5 6 7 L 5 6 5 6 7 8 L 6 7 6 7 8 9 L 8 9 7 8 9 10 Q 1 4 11 Q 4 1 22 L 4 5 8 9 10 11 Q 1 7 33 U 4 5 9 10 11 12 Q 7 1 44 C 2 3 Q 1 7 55 L 2 8 10 11 12 13 Q 1 9 66 L 9 10 11 12 13 14 L 10 11 12 13 14 15 Q 11 1 77 C 5 6 Q 3 7 88 L 5 12 13 14 15 16 Q 3 12 99 L 7 11 14 15 16 17 Q 6 1 111 U 1 2 15 16 17 18 U 10 11 16 17 18 19 Q 6 9 222 C 4 5 L 4 12 17 18 19 20 Q 3 5 333 C 8 9 Q 1 6 444 L 8 3 18 19 20 21 Q 1 5 555
Sample Output 4
321 2809 204073 3347809 -1 9535 6144873 -1 47061 742065513 4390335 341221 -1 533386148
Sample Input 5
1 5 Q 1 1 0 Q 1 1 1 Q 1 1 998244352 Q 1 1 123456789 Q 1 1 500000000
Sample Output 5
0 1 998244352 123456789 500000000