結果

問題 No.1030 だんしんぐぱーりない
ユーザー tktk_snsntktk_snsn
提出日時 2021-06-03 22:54:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 709 ms / 2,000 ms
コード長 4,924 bytes
コンパイル時間 391 ms
コンパイル使用メモリ 82,332 KB
実行使用メモリ 115,368 KB
最終ジャッジ日時 2024-04-28 17:58:14
合計ジャッジ時間 25,037 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,692 KB
testcase_01 AC 41 ms
53,416 KB
testcase_02 AC 41 ms
55,036 KB
testcase_03 AC 43 ms
53,816 KB
testcase_04 AC 42 ms
54,804 KB
testcase_05 AC 599 ms
110,088 KB
testcase_06 AC 531 ms
105,804 KB
testcase_07 AC 487 ms
90,292 KB
testcase_08 AC 482 ms
96,120 KB
testcase_09 AC 570 ms
112,872 KB
testcase_10 AC 436 ms
86,064 KB
testcase_11 AC 584 ms
99,444 KB
testcase_12 AC 568 ms
106,364 KB
testcase_13 AC 532 ms
105,844 KB
testcase_14 AC 616 ms
105,888 KB
testcase_15 AC 428 ms
86,636 KB
testcase_16 AC 575 ms
98,004 KB
testcase_17 AC 546 ms
111,812 KB
testcase_18 AC 633 ms
108,204 KB
testcase_19 AC 504 ms
88,864 KB
testcase_20 AC 519 ms
95,672 KB
testcase_21 AC 567 ms
102,040 KB
testcase_22 AC 583 ms
95,828 KB
testcase_23 AC 579 ms
95,740 KB
testcase_24 AC 541 ms
89,772 KB
testcase_25 AC 585 ms
97,240 KB
testcase_26 AC 505 ms
84,524 KB
testcase_27 AC 479 ms
84,812 KB
testcase_28 AC 619 ms
100,388 KB
testcase_29 AC 512 ms
105,652 KB
testcase_30 AC 494 ms
96,804 KB
testcase_31 AC 505 ms
94,780 KB
testcase_32 AC 554 ms
107,356 KB
testcase_33 AC 552 ms
108,940 KB
testcase_34 AC 415 ms
86,808 KB
testcase_35 AC 682 ms
115,096 KB
testcase_36 AC 709 ms
115,368 KB
testcase_37 AC 706 ms
114,772 KB
testcase_38 AC 690 ms
115,304 KB
testcase_39 AC 683 ms
115,132 KB
testcase_40 AC 40 ms
53,004 KB
testcase_41 AC 39 ms
53,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 9


class SegTree(object):
    def __init__(self, N, op_data, u_data):
        self._n = N
        self.log = (N-1).bit_length()
        self.size = 1 << self.log

        self.op = op_data
        self.e = u_data

        self.data = [u_data] * (2 * self.size)
        # self.len = [1] * (2 * self.size)

    def _update(self, i):
        self.data[i] = self.op(self.data[i << 1], self.data[i << 1 | 1])

    def initialize(self, arr=None):
        """ segtreeをarrで初期化する。len(arr) == Nにすること """
        if arr:
            for i, a in enumerate(arr, self.size):
                self.data[i] = a
        for i in reversed(range(1, self.size)):
            self._update(i)
            # self.len[i] = self.len[i << 1] + self.len[i << 1 | 1]

    def update(self, p, x):
        """ data[p] = x とする (0-indexed)"""
        p += self.size
        self.data[p] = x
        for i in range(1, self.log + 1):
            self._update(p >> i)

    def get(self, p):
        """ data[p]を返す """
        return self.data[p + self.size]

    def prod(self, l, r):
        """
        op_data(data[l], data[l+1], ..., data[r-1])を返す (0-indexed)
        """
        sml = self.e
        smr = self.e
        l += self.size
        r += self.size

        while l < r:
            if l & 1:
                sml = self.op(sml, self.data[l])
                l += 1
            if r & 1:
                r -= 1
                smr = self.op(self.data[r], smr)
            l >>= 1
            r >>= 1
        return self.op(sml, smr)

    def all_prod(self):
        """ op(data[0], data[1], ... data[N-1])を返す """
        return self.data[1]

    def max_right(self, l, func):
        """
        func(l, l+1, ..., r-1) = True,
        func(l, l+1, ..., r-1, r) = Falseとなる r を返す
        """
        if l == self._n:
            return self._n
        l += self.size
        sm = self.e
        while True:
            while l % 2 == 0:
                l >>= 1
            if not func(self.op(sm, self.data[l])):
                while l < self.size:
                    l <<= 1
                    if func(self.op(sm, self.data[l])):
                        sm = self.op(sm, self.data[l])
                        l += 1
                return l - self.size
            sm = self.op(sm, self.data[l])
            l += 1
            if (l & -l) == l:
                break
        return self._n

    def min_left(self, r, func):
        """
        func(     l, l+1, ..., r-1) = True,
        func(l-1, l, l+1, ..., r-1) = Falseとなる l を返す
        """
        if r == 0:
            return 0
        r += self.size
        sm = self.e
        while True:
            r -= 1
            while r > 1 and r & 1:
                r >>= 1
            if not func(self.op(self.data[r], sm)):
                while r < self.size:
                    r = r << 1 | 1
                    if func(self.op(self.data[r], sm)):
                        sm = self.op(self.data[r], sm)
                        r -= 1
                return r + 1 - self.size
            sm = self.op(self.data[r], sm)
            if (r & -r) == r:
                break
        return 0


N, K, Q = map(int, input().split())
D = N.bit_length()
C = list(map(int, input().split()))
A = list(map(lambda x: int(x)-1, input().split()))
G = [[] for _ in range(N)]
parent = [[0] * N for _ in range(D)]
for _ in range(N-1):
    e, f = map(int, input().split())
    e -= 1
    f -= 1
    G[f].append(e)
    parent[0][e] = f

for i in range(D-1):
    for j in range(N):
        parent[i+1][j] = parent[i][parent[i][j]]


def lowest_ancestor(x, h):
    for d in reversed(range(D)):
        if h >= (1 << d):
            x = parent[d][x]
            h -= (1 << d)
    return x


timer = 0
d = -1
depth = [0] * N
tour = [0] * (2 * N)
t_in = [0] * N
que = [~0, 0]
while que:
    s = que.pop()
    if s >= 0:
        d += 1
        t_in[s] = timer
        tour[timer] = d
        timer += 1
        for t in G[s]:
            que.append(~t)
            que.append(t)
            C[t] = max(C[t], C[s])
            depth[t] = depth[s] + 1
    else:
        s = ~s
        d -= 1
        tour[timer] = d
        timer += 1


atoi = [-1] * K
for i in range(K):
    atoi[i] = t_in[A[i]]

left = SegTree(K, min, inf)
right = SegTree(K, max, -inf)
seg = SegTree(2*N, min, inf)
left.initialize(atoi)
right.initialize(atoi)
seg.initialize(tour)

for _ in range(Q):
    flag, x, y = map(int, input().split())
    x -= 1
    y -= 1
    if flag == 1:
        # A[x] -> y
        A[x] = y
        atoi[x] = t_in[y]
        left.update(x, t_in[y])
        right.update(x, t_in[y])
    else:
        L = left.prod(x, y+1)
        R = right.prod(x, y+1)
        H = seg.prod(L, R+1)
        m = lowest_ancestor(A[x], depth[A[x]] - H)
        print(C[m])
0