結果

問題 No.1030 だんしんぐぱーりない
ユーザー 👑 tamatotamato
提出日時 2020-04-17 22:46:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 855 ms / 2,000 ms
コード長 4,207 bytes
コンパイル時間 427 ms
コンパイル使用メモリ 82,624 KB
実行使用メモリ 156,800 KB
最終ジャッジ日時 2024-04-14 15:02:44
合計ジャッジ時間 23,973 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,456 KB
testcase_01 AC 38 ms
53,916 KB
testcase_02 AC 37 ms
53,916 KB
testcase_03 AC 38 ms
53,644 KB
testcase_04 AC 38 ms
54,444 KB
testcase_05 AC 734 ms
141,556 KB
testcase_06 AC 594 ms
132,896 KB
testcase_07 AC 466 ms
102,764 KB
testcase_08 AC 459 ms
114,800 KB
testcase_09 AC 672 ms
147,440 KB
testcase_10 AC 389 ms
89,424 KB
testcase_11 AC 590 ms
120,384 KB
testcase_12 AC 688 ms
132,388 KB
testcase_13 AC 599 ms
130,128 KB
testcase_14 AC 655 ms
134,236 KB
testcase_15 AC 400 ms
95,980 KB
testcase_16 AC 616 ms
119,848 KB
testcase_17 AC 713 ms
144,124 KB
testcase_18 AC 680 ms
138,024 KB
testcase_19 AC 448 ms
96,196 KB
testcase_20 AC 523 ms
110,364 KB
testcase_21 AC 587 ms
125,056 KB
testcase_22 AC 548 ms
111,780 KB
testcase_23 AC 564 ms
109,872 KB
testcase_24 AC 463 ms
102,860 KB
testcase_25 AC 552 ms
117,888 KB
testcase_26 AC 409 ms
85,068 KB
testcase_27 AC 427 ms
88,728 KB
testcase_28 AC 634 ms
123,484 KB
testcase_29 AC 590 ms
131,168 KB
testcase_30 AC 548 ms
112,560 KB
testcase_31 AC 511 ms
110,160 KB
testcase_32 AC 616 ms
133,456 KB
testcase_33 AC 673 ms
138,408 KB
testcase_34 AC 375 ms
94,976 KB
testcase_35 AC 855 ms
156,408 KB
testcase_36 AC 848 ms
156,436 KB
testcase_37 AC 793 ms
156,800 KB
testcase_38 AC 845 ms
156,360 KB
testcase_39 AC 831 ms
156,284 KB
testcase_40 AC 38 ms
53,984 KB
testcase_41 AC 37 ms
53,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.buffer.readline

    class SegmentTree:
        def __init__(self, A, initialize=True, segfunc=min, ident=2000000000):
            self.N = len(A)
            self.LV = (self.N - 1).bit_length()
            self.N0 = 1 << self.LV
            self.segfunc = segfunc
            self.ident = ident
            if initialize:
                self.data = [self.ident] * self.N0 + A + [self.ident] * (self.N0 - self.N)
                for i in range(self.N0 - 1, 0, -1):
                    self.data[i] = segfunc(self.data[i * 2], self.data[i * 2 + 1])
            else:
                self.data = [self.ident] * (self.N0 * 2)

        def update(self, i, x):
            i += self.N0 - 1
            self.data[i] = x
            for _ in range(self.LV):
                i >>= 1
                self.data[i] = self.segfunc(self.data[i * 2], self.data[i * 2 + 1])

        # open interval [l, r)
        def query(self, l, r):
            l += self.N0 - 1
            r += self.N0 - 1
            ret = self.ident
            while l < r:
                if l & 1:
                    ret = self.segfunc(ret, self.data[l])
                    l += 1
                if r & 1:
                    ret = self.segfunc(self.data[r - 1], ret)
                    r -= 1
                l >>= 1
                r >>= 1
            return ret

    # min
    def STfunc(a, b):
        if a < b:
            return a
        else:
            return b

    # クエリは0-indexedで[l, r)
    class SparseTable():
        def __init__(self, A):
            # A: 処理したい数列
            self.N = len(A)
            self.K = self.N.bit_length() - 1
            self.table = [[0] * (self.K + 1) for _ in range(self.N)]
            for i, a in enumerate(A):
                self.table[i][0] = a
            for k in range(1, self.K + 1):
                for i in range(self.N):
                    j = i + (1 << (k - 1))
                    if j <= self.N - 1:
                        self.table[i][k] = STfunc(self.table[i][k - 1], self.table[j][k - 1])
                    else:
                        self.table[i][k] = self.table[i][k - 1]

        def query(self, l, r):
            # [l, r)の最小値を求める
            k = (r - l).bit_length() - 1
            return STfunc(self.table[l][k], self.table[r - (1 << k)][k])

    # adj[0] must be empty list
    def EulerTour(adj, root):
        st = [root]
        ret = []
        seen = [0] * len(adj)
        par = [0] * len(adj)
        depth = [0] * len(adj)
        while st:
            v = st.pop()
            if seen[v]:
                ret.append(v)
                continue
            ret.append(v)
            seen[v] = 1
            if par[v] != 0:
                st.append(par[v])
                C[v-1] = max(C[v-1], C[par[v]-1])
            for u in adj[v]:
                if seen[u] == 0:
                    st.append(u)
                    par[u] = v
                    depth[u] = depth[v] + 1

        return ret, depth

    N, K, Q = map(int, input().split())
    C = list(map(int, input().split()))
    A = list(map(int, input().split()))
    adj = [[] for _ in range(N + 1)]
    for _ in range(N - 1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)

    et, depth = EulerTour(adj, 1)
    depth_list = [0] * len(et)
    left = [-1] * (N + 1)
    right = [-1] * (N + 1)
    for i, v in enumerate(et):
        depth_list[i] = (depth[v] << 20) + v
        if left[v] < 0:
            left[v] = i
        right[v] = i
    ST = SparseTable(depth_list)

    L_list = [0] * K
    R_list = [0] * K
    for i, a in enumerate(A):
        L_list[i] = left[a]
        R_list[i] = right[a]

    L = SegmentTree(L_list)
    R = SegmentTree(R_list, segfunc=max, ident=-2000000000)

    for _ in range(Q):
        flg, x, y = map(int, input().split())
        if flg == 1:
            L.update(x, left[y])
            R.update(x, right[y])
        else:
            ll = L.query(x, y+1)
            rr = R.query(x, y+1)
            print(C[ST.query(ll, rr+1) % (1<<20) - 1])


if __name__ == '__main__':
    main()
0