結果
問題 | No.2294 Union Path Query (Easy) |
ユーザー | t98slider |
提出日時 | 2023-03-16 16:36:29 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,533 ms / 4,000 ms |
コード長 | 1,989 bytes |
コンパイル時間 | 540 ms |
コンパイル使用メモリ | 82,816 KB |
実行使用メモリ | 189,056 KB |
最終ジャッジ日時 | 2024-05-02 03:18:47 |
合計ジャッジ時間 | 50,146 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
52,224 KB |
testcase_01 | AC | 41 ms
52,224 KB |
testcase_02 | AC | 41 ms
52,224 KB |
testcase_03 | AC | 1,499 ms
178,172 KB |
testcase_04 | AC | 1,065 ms
138,496 KB |
testcase_05 | AC | 941 ms
128,392 KB |
testcase_06 | AC | 1,433 ms
186,112 KB |
testcase_07 | AC | 1,533 ms
177,664 KB |
testcase_08 | AC | 1,208 ms
189,056 KB |
testcase_09 | AC | 1,183 ms
183,424 KB |
testcase_10 | AC | 1,414 ms
185,600 KB |
testcase_11 | AC | 1,373 ms
177,152 KB |
testcase_12 | AC | 1,377 ms
179,712 KB |
testcase_13 | AC | 1,300 ms
188,160 KB |
testcase_14 | AC | 573 ms
176,640 KB |
testcase_15 | AC | 672 ms
176,256 KB |
testcase_16 | AC | 609 ms
176,616 KB |
testcase_17 | AC | 1,225 ms
175,104 KB |
testcase_18 | AC | 1,183 ms
175,980 KB |
testcase_19 | AC | 1,028 ms
175,972 KB |
testcase_20 | AC | 910 ms
175,976 KB |
testcase_21 | AC | 901 ms
175,848 KB |
testcase_22 | AC | 984 ms
176,808 KB |
testcase_23 | AC | 1,002 ms
176,316 KB |
testcase_24 | AC | 997 ms
176,492 KB |
testcase_25 | AC | 42 ms
52,352 KB |
testcase_26 | AC | 42 ms
52,096 KB |
testcase_27 | AC | 41 ms
52,352 KB |
testcase_28 | AC | 363 ms
83,328 KB |
testcase_29 | AC | 451 ms
88,220 KB |
testcase_30 | AC | 517 ms
93,020 KB |
testcase_31 | AC | 591 ms
98,076 KB |
testcase_32 | AC | 642 ms
103,088 KB |
testcase_33 | AC | 721 ms
108,396 KB |
testcase_34 | AC | 777 ms
113,508 KB |
testcase_35 | AC | 826 ms
118,340 KB |
testcase_36 | AC | 903 ms
123,116 KB |
testcase_37 | AC | 965 ms
128,260 KB |
testcase_38 | AC | 1,004 ms
132,988 KB |
testcase_39 | AC | 1,027 ms
138,248 KB |
testcase_40 | AC | 1,052 ms
142,512 KB |
testcase_41 | AC | 1,091 ms
147,656 KB |
testcase_42 | AC | 1,114 ms
152,428 KB |
testcase_43 | AC | 1,155 ms
157,384 KB |
testcase_44 | AC | 1,181 ms
163,264 KB |
testcase_45 | AC | 1,249 ms
167,648 KB |
testcase_46 | AC | 1,251 ms
172,712 KB |
testcase_47 | AC | 1,284 ms
177,392 KB |
testcase_48 | AC | 660 ms
176,384 KB |
ソースコード
from sys import stdin class UnionFind(): def __init__(self, n): self.parent_or_size = [-1] * n self.weight = [0] * n self.ans = [0] * n self.cnt = [[[0] * n for i in range(30)] for j in range(2)] for i in range(30): for j in range(n): self.cnt[0][i][j] = 1 def leader(self, x): while self.parent_or_size[x] >= 0: x = self.parent_or_size[x] return x def merge(self, x, y, w): while self.parent_or_size[x] >= 0: w ^= self.weight[x] x = self.parent_or_size[x] while self.parent_or_size[y] >= 0: w ^= self.weight[y] y = self.parent_or_size[y] if self.parent_or_size[x] > self.parent_or_size[y]: x, y = y, x self.parent_or_size[x] += self.parent_or_size[y] self.parent_or_size[y] = x self.weight[y] = w self.ans[x] = 0 for i in range(30): self.cnt[((w >> i) & 1)][i][x] += self.cnt[0][i][y] self.cnt[1 ^ ((w >> i) & 1)][i][x] += self.cnt[1][i][y] self.ans[x] += ((self.cnt[0][i][x] * self.cnt[1][i][x]) % 998244353) << i self.ans[x] %= 998244353 def dist(self, x, y): res = 0 while self.parent_or_size[x] >= 0: res ^= self.weight[x] x = self.parent_or_size[x] while self.parent_or_size[y] >= 0: res ^= self.weight[y] y = self.parent_or_size[y] return -1 if x != y else res N, X, Q = map(int, stdin.readline().split()) uf = UnionFind(N) for _ in range(Q): query = list(map(int, stdin.readline().split())) if query[0] == 1: uf.merge(X, query[1], query[2]) elif query[0] == 2: d = uf.dist(query[1], query[2]) print(d) if d != -1: X += d X %= N elif query[0] == 3: print(uf.ans[uf.leader(query[1])]) else: X += query[1] if X >= N: X -= N