結果
問題 | No.2294 Union Path Query (Easy) |
ユーザー | t98slider |
提出日時 | 2023-03-16 04:41:51 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,608 ms / 4,000 ms |
コード長 | 1,823 bytes |
コンパイル時間 | 1,602 ms |
コンパイル使用メモリ | 81,792 KB |
実行使用メモリ | 191,076 KB |
最終ジャッジ日時 | 2024-05-02 03:17:55 |
合計ジャッジ時間 | 53,582 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
52,096 KB |
testcase_01 | AC | 41 ms
52,352 KB |
testcase_02 | AC | 42 ms
52,352 KB |
testcase_03 | AC | 1,578 ms
177,788 KB |
testcase_04 | AC | 1,074 ms
139,648 KB |
testcase_05 | AC | 962 ms
129,024 KB |
testcase_06 | AC | 1,543 ms
188,032 KB |
testcase_07 | AC | 1,608 ms
177,060 KB |
testcase_08 | AC | 1,305 ms
191,076 KB |
testcase_09 | AC | 1,301 ms
184,576 KB |
testcase_10 | AC | 1,579 ms
187,648 KB |
testcase_11 | AC | 1,535 ms
176,876 KB |
testcase_12 | AC | 1,548 ms
179,916 KB |
testcase_13 | AC | 1,413 ms
189,696 KB |
testcase_14 | AC | 588 ms
176,236 KB |
testcase_15 | AC | 710 ms
176,348 KB |
testcase_16 | AC | 599 ms
176,764 KB |
testcase_17 | AC | 1,306 ms
174,964 KB |
testcase_18 | AC | 1,425 ms
178,168 KB |
testcase_19 | AC | 1,245 ms
179,448 KB |
testcase_20 | AC | 957 ms
176,128 KB |
testcase_21 | AC | 1,001 ms
177,280 KB |
testcase_22 | AC | 1,110 ms
176,928 KB |
testcase_23 | AC | 1,029 ms
175,912 KB |
testcase_24 | AC | 1,047 ms
176,172 KB |
testcase_25 | AC | 41 ms
52,480 KB |
testcase_26 | AC | 42 ms
52,224 KB |
testcase_27 | AC | 42 ms
52,352 KB |
testcase_28 | AC | 365 ms
83,892 KB |
testcase_29 | AC | 445 ms
87,724 KB |
testcase_30 | AC | 507 ms
92,668 KB |
testcase_31 | AC | 594 ms
98,972 KB |
testcase_32 | AC | 633 ms
103,004 KB |
testcase_33 | AC | 702 ms
108,768 KB |
testcase_34 | AC | 772 ms
113,040 KB |
testcase_35 | AC | 803 ms
118,512 KB |
testcase_36 | AC | 860 ms
123,020 KB |
testcase_37 | AC | 936 ms
129,064 KB |
testcase_38 | AC | 973 ms
133,324 KB |
testcase_39 | AC | 1,011 ms
138,504 KB |
testcase_40 | AC | 1,061 ms
142,780 KB |
testcase_41 | AC | 1,111 ms
148,616 KB |
testcase_42 | AC | 1,140 ms
152,908 KB |
testcase_43 | AC | 1,178 ms
157,528 KB |
testcase_44 | AC | 1,250 ms
162,700 KB |
testcase_45 | AC | 1,329 ms
168,048 KB |
testcase_46 | AC | 1,318 ms
172,664 KB |
testcase_47 | AC | 1,360 ms
177,880 KB |
testcase_48 | AC | 704 ms
176,604 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): 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[((w >> i) & 1)][i][rx] += self.cnt[0][i][ry] self.cnt[1 ^ ((w >> i) & 1)][i][rx] += self.cnt[1][i][ry] self.ans[rx] += ((self.cnt[0][i][rx] * self.cnt[1][i][rx]) % 998244353) << 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