結果
問題 | No.2294 Union Path Query (Easy) |
ユーザー | t98slider |
提出日時 | 2023-03-16 19:41:08 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,124 ms / 4,000 ms |
コード長 | 1,798 bytes |
コンパイル時間 | 367 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 191,488 KB |
最終ジャッジ日時 | 2024-05-02 03:21:16 |
合計ジャッジ時間 | 40,285 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
52,352 KB |
testcase_01 | AC | 37 ms
52,352 KB |
testcase_02 | AC | 37 ms
52,096 KB |
testcase_03 | AC | 1,081 ms
178,040 KB |
testcase_04 | AC | 765 ms
139,264 KB |
testcase_05 | AC | 683 ms
128,384 KB |
testcase_06 | AC | 1,109 ms
188,032 KB |
testcase_07 | AC | 1,124 ms
177,408 KB |
testcase_08 | AC | 896 ms
191,488 KB |
testcase_09 | AC | 885 ms
184,320 KB |
testcase_10 | AC | 1,111 ms
187,392 KB |
testcase_11 | AC | 1,043 ms
176,768 KB |
testcase_12 | AC | 1,064 ms
179,952 KB |
testcase_13 | AC | 989 ms
189,996 KB |
testcase_14 | AC | 465 ms
176,384 KB |
testcase_15 | AC | 540 ms
176,256 KB |
testcase_16 | AC | 486 ms
176,988 KB |
testcase_17 | AC | 994 ms
175,060 KB |
testcase_18 | AC | 1,093 ms
178,656 KB |
testcase_19 | AC | 952 ms
178,268 KB |
testcase_20 | AC | 721 ms
176,104 KB |
testcase_21 | AC | 808 ms
177,760 KB |
testcase_22 | AC | 848 ms
177,164 KB |
testcase_23 | AC | 804 ms
176,656 KB |
testcase_24 | AC | 818 ms
176,024 KB |
testcase_25 | AC | 37 ms
52,480 KB |
testcase_26 | AC | 36 ms
52,352 KB |
testcase_27 | AC | 36 ms
52,352 KB |
testcase_28 | AC | 285 ms
83,200 KB |
testcase_29 | AC | 347 ms
87,952 KB |
testcase_30 | AC | 400 ms
93,012 KB |
testcase_31 | AC | 469 ms
98,812 KB |
testcase_32 | AC | 491 ms
102,964 KB |
testcase_33 | AC | 532 ms
108,352 KB |
testcase_34 | AC | 579 ms
113,268 KB |
testcase_35 | AC | 612 ms
118,160 KB |
testcase_36 | AC | 663 ms
122,848 KB |
testcase_37 | AC | 708 ms
128,524 KB |
testcase_38 | AC | 755 ms
133,688 KB |
testcase_39 | AC | 766 ms
138,500 KB |
testcase_40 | AC | 813 ms
143,132 KB |
testcase_41 | AC | 840 ms
147,800 KB |
testcase_42 | AC | 862 ms
152,312 KB |
testcase_43 | AC | 902 ms
157,368 KB |
testcase_44 | AC | 912 ms
163,052 KB |
testcase_45 | AC | 983 ms
167,744 KB |
testcase_46 | AC | 968 ms
173,020 KB |
testcase_47 | AC | 1,002 ms
177,524 KB |
testcase_48 | AC | 531 ms
176,416 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 * ((w >> i) & 1) + i][rx] += self.cnt[i][ry] self.cnt[30 * (1 ^ ((w >> i) & 1)) + 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