結果

問題 No.1030 だんしんぐぱーりない
ユーザー maspymaspy
提出日時 2020-04-18 01:07:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 599 ms / 2,000 ms
コード長 3,040 bytes
コンパイル時間 191 ms
コンパイル使用メモリ 82,384 KB
実行使用メモリ 130,908 KB
最終ジャッジ日時 2024-10-03 19:52:59
合計ジャッジ時間 20,037 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,988 KB
testcase_01 AC 39 ms
53,976 KB
testcase_02 AC 40 ms
53,648 KB
testcase_03 AC 40 ms
53,724 KB
testcase_04 AC 40 ms
52,748 KB
testcase_05 AC 549 ms
124,852 KB
testcase_06 AC 449 ms
115,336 KB
testcase_07 AC 405 ms
98,208 KB
testcase_08 AC 392 ms
102,176 KB
testcase_09 AC 464 ms
123,604 KB
testcase_10 AC 338 ms
86,700 KB
testcase_11 AC 479 ms
107,676 KB
testcase_12 AC 511 ms
120,064 KB
testcase_13 AC 444 ms
114,992 KB
testcase_14 AC 533 ms
117,804 KB
testcase_15 AC 376 ms
92,648 KB
testcase_16 AC 490 ms
112,684 KB
testcase_17 AC 482 ms
124,072 KB
testcase_18 AC 553 ms
120,928 KB
testcase_19 AC 421 ms
97,868 KB
testcase_20 AC 436 ms
108,288 KB
testcase_21 AC 421 ms
112,804 KB
testcase_22 AC 444 ms
103,236 KB
testcase_23 AC 470 ms
104,796 KB
testcase_24 AC 437 ms
96,940 KB
testcase_25 AC 439 ms
105,864 KB
testcase_26 AC 407 ms
88,796 KB
testcase_27 AC 402 ms
92,400 KB
testcase_28 AC 503 ms
114,868 KB
testcase_29 AC 436 ms
116,412 KB
testcase_30 AC 415 ms
103,136 KB
testcase_31 AC 434 ms
106,116 KB
testcase_32 AC 487 ms
116,340 KB
testcase_33 AC 492 ms
121,120 KB
testcase_34 AC 347 ms
87,188 KB
testcase_35 AC 599 ms
130,908 KB
testcase_36 AC 596 ms
119,448 KB
testcase_37 AC 594 ms
129,448 KB
testcase_38 AC 597 ms
119,504 KB
testcase_39 AC 587 ms
119,220 KB
testcase_40 AC 39 ms
52,904 KB
testcase_41 AC 39 ms
52,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


class SegTree:
    """ segment tree with point modification and range product access. """
    data_unit = 1 << 30
    data_f = min

    def __init__(self, N):
        self.N = N
        self.data = [self.data_unit] * (N + N)

    def build(self, raw_data):
        data = self.data
        f = self.data_f
        N = self.N
        data[N:] = raw_data[:]
        for i in range(N - 1, 0, -1):
            data[i] = f(data[i << 1], data[i << 1 | 1])

    def set_val(self, i, x):
        data = self.data
        f = self.data_f
        i += self.N
        data[i] = x
        while i > 1:
            i >>= 1
            data[i] = f(data[i << 1], data[i << 1 | 1])

    def fold(self, L, R):
        """ compute for [L, R) """
        vL = vR = self.data_unit
        data = self.data
        f = self.data_f
        L += self.N
        R += self.N
        while L < R:
            if L & 1:
                vL = f(vL, data[L])
                L += 1
            if R & 1:
                R -= 1
                vR = f(data[R], vR)
            L >>= 1
            R >>= 1
        return f(vL, vR)


def create_euler_tour(graph, root=1):
    """1-indexed graph"""
    V = len(graph)
    par = [0] * V
    depth = [0] * V
    depth[root] = 0
    tour = [root]
    st = [root]
    while st:
        x = st[-1]
        if not graph[x]:
            st.pop()
            tour.append(par[x])
            continue
        y = graph[x].pop()
        if y == par[x]:
            continue
        par[y] = x
        depth[y] = depth[x] + 1
        st.append(y)
        tour.append(y)
    return tour, depth, par


N, K, Q = map(int, readline().split())
C = tuple(map(int, readline().split()))
A = list(map(int, readline().split()))

graph = [[] for _ in range(N + 1)]
lines = readlines()
for line in lines[: N - 1]:
    e, f = map(int, line.split())
    graph[f].append(e)

tour, depth, parent = create_euler_tour(graph)
ind = [0] * (N + 1)
for i, x in enumerate(tour):
    ind[x] = i
tour_dep = [depth[v] for v in tour]
seg_min = SegTree(K)

seg_max = SegTree(K)
seg_max.data_f = max
seg_max.data_unit = 0

seg_lca = SegTree(N + N - 1)
seg_lca.data_unit = None


def f(x, y):
    if x is None:
        return y
    if y is None:
        return x
    if depth[x] < depth[y]:
        return x
    else:
        return y


seg_lca.data_f = f
seg_lca.build(tour)


def lca(x, y):
    return seg_lca.fold(x, y + 1)


seg_min.build([ind[x] for x in A])
seg_max.build([ind[x] for x in A])
LCA = []
for query in lines[N - 1:]:
    t, x, y = map(int, query.split())
    if t == 2:
        L = seg_min.fold(x - 1, y)
        R = seg_max.fold(x - 1, y)
        v = lca(L, R)
        LCA.append(v)
    else:
        x -= 1
        i = A[x]
        A[x] = y
        seg_min.set_val(x, ind[y])
        seg_max.set_val(x, ind[y])

cost = [0] * (N + 1)
for v in tour:
    cost[v] = max(C[v - 1], cost[parent[v]])

for v in LCA:
    print(cost[v])
0