結果

問題 No.2294 Union Path Query (Easy)
ユーザー t98slidert98slider
提出日時 2023-03-16 19:43:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,245 ms / 4,000 ms
コード長 1,810 bytes
コンパイル時間 427 ms
コンパイル使用メモリ 82,468 KB
実行使用メモリ 190,464 KB
最終ジャッジ日時 2024-05-02 03:19:35
合計ジャッジ時間 42,686 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
52,352 KB
testcase_01 AC 35 ms
52,480 KB
testcase_02 AC 35 ms
52,480 KB
testcase_03 AC 1,205 ms
178,408 KB
testcase_04 AC 839 ms
139,008 KB
testcase_05 AC 757 ms
129,388 KB
testcase_06 AC 1,205 ms
187,008 KB
testcase_07 AC 1,245 ms
177,684 KB
testcase_08 AC 977 ms
190,464 KB
testcase_09 AC 948 ms
184,192 KB
testcase_10 AC 1,190 ms
187,008 KB
testcase_11 AC 1,161 ms
177,932 KB
testcase_12 AC 1,180 ms
179,980 KB
testcase_13 AC 1,112 ms
186,112 KB
testcase_14 AC 482 ms
176,400 KB
testcase_15 AC 597 ms
176,276 KB
testcase_16 AC 522 ms
177,164 KB
testcase_17 AC 1,052 ms
175,500 KB
testcase_18 AC 1,161 ms
178,692 KB
testcase_19 AC 1,023 ms
179,340 KB
testcase_20 AC 769 ms
176,648 KB
testcase_21 AC 808 ms
177,420 KB
testcase_22 AC 912 ms
177,800 KB
testcase_23 AC 871 ms
176,720 KB
testcase_24 AC 883 ms
176,580 KB
testcase_25 AC 39 ms
52,736 KB
testcase_26 AC 36 ms
52,096 KB
testcase_27 AC 35 ms
52,480 KB
testcase_28 AC 294 ms
83,384 KB
testcase_29 AC 371 ms
88,232 KB
testcase_30 AC 426 ms
93,820 KB
testcase_31 AC 516 ms
99,220 KB
testcase_32 AC 515 ms
103,692 KB
testcase_33 AC 600 ms
109,384 KB
testcase_34 AC 650 ms
113,672 KB
testcase_35 AC 664 ms
119,620 KB
testcase_36 AC 712 ms
123,476 KB
testcase_37 AC 796 ms
129,712 KB
testcase_38 AC 823 ms
133,712 KB
testcase_39 AC 830 ms
138,880 KB
testcase_40 AC 924 ms
143,412 KB
testcase_41 AC 961 ms
149,072 KB
testcase_42 AC 949 ms
153,988 KB
testcase_43 AC 960 ms
158,804 KB
testcase_44 AC 1,018 ms
163,548 KB
testcase_45 AC 1,062 ms
169,216 KB
testcase_46 AC 1,056 ms
173,544 KB
testcase_47 AC 1,104 ms
178,176 KB
testcase_48 AC 512 ms
176,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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
0