結果
問題 | No.2294 Union Path Query (Easy) |
ユーザー | t98slider |
提出日時 | 2023-03-16 19:49:22 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,538 ms / 4,000 ms |
コード長 | 1,815 bytes |
コンパイル時間 | 528 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 191,488 KB |
最終ジャッジ日時 | 2024-11-22 13:37:10 |
合計ジャッジ時間 | 51,908 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 50 ms
52,096 KB |
testcase_01 | AC | 43 ms
52,352 KB |
testcase_02 | AC | 44 ms
52,352 KB |
testcase_03 | AC | 1,526 ms
178,316 KB |
testcase_04 | AC | 1,041 ms
139,648 KB |
testcase_05 | AC | 937 ms
128,768 KB |
testcase_06 | AC | 1,483 ms
188,160 KB |
testcase_07 | AC | 1,538 ms
177,008 KB |
testcase_08 | AC | 1,310 ms
191,488 KB |
testcase_09 | AC | 1,218 ms
184,960 KB |
testcase_10 | AC | 1,469 ms
187,648 KB |
testcase_11 | AC | 1,432 ms
177,264 KB |
testcase_12 | AC | 1,436 ms
180,040 KB |
testcase_13 | AC | 1,352 ms
190,080 KB |
testcase_14 | AC | 551 ms
176,480 KB |
testcase_15 | AC | 687 ms
176,616 KB |
testcase_16 | AC | 583 ms
177,020 KB |
testcase_17 | AC | 1,283 ms
175,224 KB |
testcase_18 | AC | 1,373 ms
178,432 KB |
testcase_19 | AC | 1,200 ms
178,936 KB |
testcase_20 | AC | 934 ms
176,372 KB |
testcase_21 | AC | 972 ms
177,268 KB |
testcase_22 | AC | 1,055 ms
177,308 KB |
testcase_23 | AC | 1,297 ms
176,040 KB |
testcase_24 | AC | 1,033 ms
176,172 KB |
testcase_25 | AC | 43 ms
52,224 KB |
testcase_26 | AC | 43 ms
52,608 KB |
testcase_27 | AC | 43 ms
52,480 KB |
testcase_28 | AC | 356 ms
83,892 KB |
testcase_29 | AC | 456 ms
87,984 KB |
testcase_30 | AC | 507 ms
93,040 KB |
testcase_31 | AC | 648 ms
98,716 KB |
testcase_32 | AC | 639 ms
103,128 KB |
testcase_33 | AC | 723 ms
108,512 KB |
testcase_34 | AC | 779 ms
113,104 KB |
testcase_35 | AC | 861 ms
118,592 KB |
testcase_36 | AC | 868 ms
123,144 KB |
testcase_37 | AC | 1,020 ms
128,548 KB |
testcase_38 | AC | 1,074 ms
133,312 KB |
testcase_39 | AC | 1,076 ms
138,596 KB |
testcase_40 | AC | 1,085 ms
143,176 KB |
testcase_41 | AC | 1,160 ms
148,760 KB |
testcase_42 | AC | 1,141 ms
153,036 KB |
testcase_43 | AC | 1,195 ms
157,404 KB |
testcase_44 | AC | 1,257 ms
163,076 KB |
testcase_45 | AC | 1,301 ms
168,240 KB |
testcase_46 | AC | 1,343 ms
173,108 KB |
testcase_47 | AC | 1,360 ms
177,696 KB |
testcase_48 | AC | 703 ms
176,616 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