結果

問題 No.2296 Union Path Query (Hard)
ユーザー t98slidert98slider
提出日時 2023-03-16 06:00:04
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,823 bytes
コンパイル時間 710 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 186,488 KB
最終ジャッジ日時 2024-05-02 14:14:33
合計ジャッジ時間 58,081 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
54,528 KB
testcase_01 AC 91 ms
112,768 KB
testcase_02 AC 94 ms
112,768 KB
testcase_03 AC 90 ms
112,768 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 508 ms
186,488 KB
testcase_24 AC 392 ms
131,528 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 1,707 ms
154,652 KB
testcase_32 AC 1,610 ms
153,860 KB
testcase_33 AC 1,388 ms
150,684 KB
testcase_34 AC 1,171 ms
149,828 KB
testcase_35 AC 1,059 ms
148,428 KB
testcase_36 AC 870 ms
142,632 KB
testcase_37 AC 1,488 ms
133,008 KB
testcase_38 AC 1,519 ms
133,644 KB
testcase_39 AC 1,537 ms
133,616 KB
testcase_40 AC 1,506 ms
133,260 KB
testcase_41 AC 42 ms
54,272 KB
testcase_42 AC 40 ms
54,656 KB
testcase_43 AC 40 ms
54,656 KB
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin
from collections import deque

class UnionFind():
    def __init__(self, n):
        self.parent_or_size = [-1] * n
        self.G = [[] for i in range(n)]
        self.E = [[] for i in range(n)]
        self.depth = [0] * n
        self.dp = [0] * n
        self.log_table = [0] * n
        self.diameter = [0] * n
        self.diameter_vertex = [0] * (2 * n)
        self.parent = [[-1] * n for i in range(18)]
        self.que = deque()

        for i in range(n):
            self.diameter_vertex[i << 1] = i
            self.diameter_vertex[(i << 1) + 1] = i
        for i in range(2, n):
            self.log_table[i] = self.log_table[i >> 1] + 1


    def leader(self, x):
        return x if self.parent_or_size[x] < 0 else self.parent_or_size[x]

    def merge(self, u, v, w):
        x, y = self.leader(u), self.leader(v)
        if x == y: return
        if -self.parent_or_size[x] < -self.parent_or_size[y]: 
            x, y = y, x
            u, v = v, u
        
        self.parent_or_size[x] += self.parent_or_size[y]
        self.G[u].append(v)
        self.G[v].append(u)
        self.E[v].append(w)
        self.E[u].append(w)
        self.update_parent(v, u, w)

        self.que.append(v)
        while len(self.que):
            v = self.que.popleft()
            self.parent_or_size[v] = x
            for i in range(len(self.G[v])):
                if self.G[v][i] == self.parent[0][v]: continue
                self.update_parent(self.G[v][i], v, self.E[v][i])
                self.que.append(self.G[v][i])

        self.update_diameter(x, y)
        

    def update_parent(self, v, par, w):
        logv = self.log_table[max(self.depth[v], self.depth[par] + 1)]
        self.depth[v] = self.depth[par] + 1
        self.dp[v] = self.dp[par] + w
        self.parent[0][v] = par
        for i in range(logv):
            self.parent[i + 1][v] = -1 if self.parent[i][v] == -1 else self.parent[i][self.parent[i][v]]

    def update_diameter(self, x, y):
        mx, v, u = 0, 0, 0
        v1, v2 = self.diameter_vertex[2 * x], self.diameter_vertex[2 * x + 1]
        v3, v4 = self.diameter_vertex[2 * y], self.diameter_vertex[2 * y + 1]
        if self.diameter[x] >= self.diameter[y]:
            mx, u, v = self.diameter[x], v1, v2
        else:
            mx, u, v = self.diameter[y], v3, v4
        
        d = self.dist(v1, v3)
        if d > mx: mx, u, v = d, v1, v3
        d = self.dist(v1, v4)
        if d > mx: mx, u, v = d, v1, v4
        d = self.dist(v2, v3)
        if d > mx: mx, u, v = d, v2, v3
        d = self.dist(v2, v4)
        if d > mx: mx, u, v = d, v2, v4

        self.diameter[x], self.diameter_vertex[2 * x], self.diameter_vertex[2 * x + 1] = mx, u, v
    
    def dist(self, u, v):
        if self.leader(u) != self.leader(v): return -1
        if self.depth[u] > self.depth[v]: u, v = v, u
        res = self.dp[u] + self.dp[v]
        d = self.depth[v] - self.depth[u]

        while d:
            v = self.parent[self.log_table[d & -d]][v]
            d -= d & -d
        
        if u == v: return res - 2 * self.dp[v]

        for i in range(self.log_table[d], -1,-1):
            if self.parent[i][v] != self.parent[i][u]:
                u = self.parent[i][u]
                v = self.parent[i][v]
        
        return res - 2 * self.dp[self.parent[0][v]]
        

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.diameter[uf.leader(query[1])])
    else:
        X += query[1]
        if X >= N: X -= N
0