結果
問題 | No.2294 Union Path Query (Easy) |
ユーザー | t98slider |
提出日時 | 2023-03-16 19:47:59 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,663 ms / 4,000 ms |
コード長 | 1,803 bytes |
コンパイル時間 | 548 ms |
コンパイル使用メモリ | 82,476 KB |
実行使用メモリ | 191,360 KB |
最終ジャッジ日時 | 2024-05-02 03:20:30 |
合計ジャッジ時間 | 50,590 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
52,096 KB |
testcase_01 | AC | 40 ms
52,096 KB |
testcase_02 | AC | 41 ms
52,352 KB |
testcase_03 | AC | 1,556 ms
177,800 KB |
testcase_04 | AC | 1,048 ms
139,520 KB |
testcase_05 | AC | 912 ms
128,384 KB |
testcase_06 | AC | 1,480 ms
187,776 KB |
testcase_07 | AC | 1,663 ms
177,252 KB |
testcase_08 | AC | 1,200 ms
191,360 KB |
testcase_09 | AC | 1,184 ms
184,320 KB |
testcase_10 | AC | 1,472 ms
187,648 KB |
testcase_11 | AC | 1,422 ms
177,248 KB |
testcase_12 | AC | 1,433 ms
180,232 KB |
testcase_13 | AC | 1,328 ms
189,696 KB |
testcase_14 | AC | 570 ms
175,836 KB |
testcase_15 | AC | 676 ms
176,476 KB |
testcase_16 | AC | 585 ms
177,144 KB |
testcase_17 | AC | 1,244 ms
175,476 KB |
testcase_18 | AC | 1,349 ms
178,424 KB |
testcase_19 | AC | 1,186 ms
178,804 KB |
testcase_20 | AC | 943 ms
176,636 KB |
testcase_21 | AC | 1,029 ms
177,260 KB |
testcase_22 | AC | 1,091 ms
177,312 KB |
testcase_23 | AC | 1,038 ms
175,908 KB |
testcase_24 | AC | 1,033 ms
175,904 KB |
testcase_25 | AC | 44 ms
52,224 KB |
testcase_26 | AC | 43 ms
52,224 KB |
testcase_27 | AC | 43 ms
52,224 KB |
testcase_28 | AC | 361 ms
83,144 KB |
testcase_29 | AC | 474 ms
88,108 KB |
testcase_30 | AC | 534 ms
93,036 KB |
testcase_31 | AC | 619 ms
98,588 KB |
testcase_32 | AC | 640 ms
103,248 KB |
testcase_33 | AC | 735 ms
108,888 KB |
testcase_34 | AC | 818 ms
113,164 KB |
testcase_35 | AC | 840 ms
118,084 KB |
testcase_36 | AC | 869 ms
122,740 KB |
testcase_37 | AC | 928 ms
128,424 KB |
testcase_38 | AC | 988 ms
133,452 KB |
testcase_39 | AC | 998 ms
138,124 KB |
testcase_40 | AC | 1,042 ms
142,404 KB |
testcase_41 | AC | 1,103 ms
148,580 KB |
testcase_42 | AC | 1,110 ms
152,580 KB |
testcase_43 | AC | 1,160 ms
157,456 KB |
testcase_44 | AC | 1,256 ms
163,080 KB |
testcase_45 | AC | 1,314 ms
168,372 KB |
testcase_46 | AC | 1,248 ms
172,720 KB |
testcase_47 | AC | 1,289 ms
177,564 KB |
testcase_48 | AC | 663 ms
176,216 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]) << 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