結果
問題 | No.2294 Union Path Query (Easy) |
ユーザー | t98slider |
提出日時 | 2023-03-16 19:43:52 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,445 ms / 4,000 ms |
コード長 | 1,810 bytes |
コンパイル時間 | 339 ms |
コンパイル使用メモリ | 82,428 KB |
実行使用メモリ | 190,720 KB |
最終ジャッジ日時 | 2024-11-22 13:34:12 |
合計ジャッジ時間 | 48,623 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
52,224 KB |
testcase_01 | AC | 38 ms
52,096 KB |
testcase_02 | AC | 38 ms
52,352 KB |
testcase_03 | AC | 1,431 ms
178,480 KB |
testcase_04 | AC | 981 ms
139,264 KB |
testcase_05 | AC | 908 ms
129,360 KB |
testcase_06 | AC | 1,401 ms
187,164 KB |
testcase_07 | AC | 1,445 ms
177,980 KB |
testcase_08 | AC | 1,116 ms
190,720 KB |
testcase_09 | AC | 1,105 ms
184,356 KB |
testcase_10 | AC | 1,409 ms
187,008 KB |
testcase_11 | AC | 1,341 ms
177,812 KB |
testcase_12 | AC | 1,370 ms
179,860 KB |
testcase_13 | AC | 1,249 ms
186,088 KB |
testcase_14 | AC | 554 ms
176,276 KB |
testcase_15 | AC | 653 ms
176,216 KB |
testcase_16 | AC | 600 ms
177,284 KB |
testcase_17 | AC | 1,180 ms
175,436 KB |
testcase_18 | AC | 1,319 ms
178,644 KB |
testcase_19 | AC | 1,172 ms
179,528 KB |
testcase_20 | AC | 921 ms
176,896 KB |
testcase_21 | AC | 962 ms
177,536 KB |
testcase_22 | AC | 1,018 ms
177,536 KB |
testcase_23 | AC | 970 ms
176,336 KB |
testcase_24 | AC | 962 ms
177,084 KB |
testcase_25 | AC | 39 ms
52,480 KB |
testcase_26 | AC | 40 ms
52,608 KB |
testcase_27 | AC | 40 ms
52,480 KB |
testcase_28 | AC | 346 ms
83,892 KB |
testcase_29 | AC | 455 ms
88,240 KB |
testcase_30 | AC | 519 ms
93,948 KB |
testcase_31 | AC | 633 ms
98,972 KB |
testcase_32 | AC | 651 ms
104,092 KB |
testcase_33 | AC | 706 ms
109,208 KB |
testcase_34 | AC | 841 ms
113,732 KB |
testcase_35 | AC | 793 ms
119,244 KB |
testcase_36 | AC | 837 ms
123,476 KB |
testcase_37 | AC | 904 ms
129,404 KB |
testcase_38 | AC | 968 ms
133,712 KB |
testcase_39 | AC | 958 ms
138,760 KB |
testcase_40 | AC | 1,018 ms
143,408 KB |
testcase_41 | AC | 1,075 ms
149,324 KB |
testcase_42 | AC | 1,090 ms
153,924 KB |
testcase_43 | AC | 1,155 ms
158,424 KB |
testcase_44 | AC | 1,188 ms
163,412 KB |
testcase_45 | AC | 1,255 ms
169,228 KB |
testcase_46 | AC | 1,282 ms
173,792 KB |
testcase_47 | AC | 1,297 ms
178,168 KB |
testcase_48 | AC | 580 ms
176,560 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(60)] for i in range(30): for j in range(n): self.cnt[i][j] = 1 def leader(self, x): if self.parent_or_size[x] < 0: return x root = self.leader(self.parent_or_size[x]) self.weight[x] ^= self.weight[self.parent_or_size[x]] self.parent_or_size[x] = root return root def merge(self, x, y, w): rx, ry = self.leader(x), self.leader(y) w ^= self.weight[x] ^ self.weight[y] if self.parent_or_size[rx] > self.parent_or_size[ry]: rx, ry = ry, rx self.parent_or_size[rx] += self.parent_or_size[ry] self.parent_or_size[ry] = rx self.weight[ry] = w self.ans[rx] = 0 for i in range(30): self.cnt[(30 if w >> i & 1 else 0) + i][rx] += self.cnt[i][ry] self.cnt[(30 if 1 ^ (w >> i & 1) else 0) + i][rx] += self.cnt[30 + i][ry] self.ans[rx] += (self.cnt[i][rx] * self.cnt[30 + i][rx]) << i self.ans[rx] %= 998244353 def dist(self, x, y): if self.leader(x) != self.leader(y): return -1 return self.weight[x] ^ self.weight[y] 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