結果

問題 No.1030 だんしんぐぱーりない
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2022-01-04 07:40:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,213 ms / 2,000 ms
コード長 6,991 bytes
コンパイル時間 233 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 131,764 KB
最終ジャッジ日時 2024-10-13 21:38:18
合計ジャッジ時間 33,621 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
53,632 KB
testcase_01 AC 45 ms
53,504 KB
testcase_02 AC 46 ms
53,632 KB
testcase_03 AC 45 ms
53,632 KB
testcase_04 AC 45 ms
53,504 KB
testcase_05 AC 925 ms
116,720 KB
testcase_06 AC 811 ms
111,744 KB
testcase_07 AC 715 ms
97,664 KB
testcase_08 AC 694 ms
101,760 KB
testcase_09 AC 825 ms
118,912 KB
testcase_10 AC 638 ms
89,344 KB
testcase_11 AC 926 ms
107,776 KB
testcase_12 AC 865 ms
109,812 KB
testcase_13 AC 856 ms
110,208 KB
testcase_14 AC 966 ms
112,640 KB
testcase_15 AC 638 ms
91,648 KB
testcase_16 AC 876 ms
103,808 KB
testcase_17 AC 905 ms
115,720 KB
testcase_18 AC 1,056 ms
115,840 KB
testcase_19 AC 762 ms
93,440 KB
testcase_20 AC 803 ms
100,352 KB
testcase_21 AC 801 ms
107,392 KB
testcase_22 AC 772 ms
99,968 KB
testcase_23 AC 879 ms
102,400 KB
testcase_24 AC 800 ms
96,384 KB
testcase_25 AC 860 ms
105,984 KB
testcase_26 AC 703 ms
87,936 KB
testcase_27 AC 704 ms
90,368 KB
testcase_28 AC 955 ms
107,776 KB
testcase_29 AC 795 ms
109,312 KB
testcase_30 AC 771 ms
99,712 KB
testcase_31 AC 780 ms
100,480 KB
testcase_32 AC 892 ms
112,128 KB
testcase_33 AC 988 ms
114,560 KB
testcase_34 AC 611 ms
91,264 KB
testcase_35 AC 1,178 ms
130,616 KB
testcase_36 AC 1,188 ms
131,224 KB
testcase_37 AC 1,194 ms
131,764 KB
testcase_38 AC 1,213 ms
131,372 KB
testcase_39 AC 1,199 ms
131,132 KB
testcase_40 AC 46 ms
53,632 KB
testcase_41 AC 44 ms
53,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
class LCA_wp():
    
    def __init__(self,e,w=None,f=None,ide_ele=None ,root=0):
        
        if w is None:
            
            self.N = len(e)
            self.e = e
            self.root = root
            self.IN = [-1]*N
            self.OUT = [-1]*N
            self.DIST = [-1]*N
            self.time = 0
            self.DIST[root] = 0
            self.parents = [-1]*N
            self.dfs(root)
            self.depth = [(self.DIST[i],i) for i in range(N)]
            self.init_val = [-1]*(2*N)
            for i in range(N):
                x = self.IN[i]
                y = self.OUT[i]
                self.init_val[x] = self.depth[i]
                self.init_val[y] = self.depth[self.parents[i]]
            
            self.T = self.SegTree(self.init_val)
            return
        self.N = len(e)
        self.e = e
        self.root = root
        self.IN = [-1]*N
        self.OUT = [-1]*N
        self.DIST = [-1]*N
        self.time = 0
        self.DIST[root] = 0
        self.parents = [-1]*N
        self.init_val_w = [ide_ele]*(2*N)
        self.dfs(root)
        self.depth = [(self.DIST[i],i) for i in range(N)]
        self.init_val = [-1]*(2*N)
        for i in range(N):
            x = self.IN[i]
            y = self.OUT[i]
            self.init_val[x] = self.depth[i]
            self.init_val[y] = self.depth[self.parents[i]]
            
        for i in range(N):
            self.init_val_w[self.IN[i]] = w[i]
            
        self.W = self.SegTree(self.init_val_w,f,ide_ele)
        
        self.T = self.SegTree(self.init_val)
        return
    
    def weight_update(self,x,y):
        self.W.update(x,y)
        return
    
    
    def tree_query(self,x):
        
        return self.W.query(self.IN[x],self.OUT[x])
    
    
        
    def dfs(self,x):
        
        self.IN[x] = self.time
        self.time += 1
        for ix,c in e[x]:
            if self.DIST[ix] != -1:
                continue
            self.parents[ix] = x
            self.DIST[ix] = self.DIST[x] + c
            self.dfs(ix)
            
        self.OUT[x] = self.time
        self.time += 1
        
    def lca(self,l,r):
        L = self.IN[l]
        R = self.IN[r]
        if R<L:
            L,R = R,L
        R += 1
        return self.T.query(L,R)[1]
        
        
    def distance_between(self,l,r):
        
        p = self.lca(l,r)
        return self.DIST[l]+ self.DIST[r] - 2*self.DIST[p]
        
    def distance_from_root(self,x):
        return self.DIST[x]

    class SegTree:
        """
        Segment Tree
        """
    
        def __init__(self, init_val, segfunc = None, ide_ele= None):
            """
            初期化
            
            init_val: 配列の初期値
            """
            if segfunc is None:
                def segfunc(x,y):
                    return min(x,y)
                ide_ele = (float('inf'),-1)
                
            
            n = len(init_val)
            self.segfunc = segfunc
            self.ide_ele = ide_ele
            self.num = 1 << (n - 1).bit_length()
            self.tree = [ide_ele] * 2 * self.num
            # 配列の値を葉にセット
            for i in range(n):
                self.tree[self.num + i] = init_val[i]
            # 構築していく
            for i in range(self.num - 1, 0, -1):
                self.tree[i] = segfunc(self.tree[2 * i], self.tree[2 * i + 1])
    
        def update(self, k, x):
            """
            k番目の値をxに更新
    
            k: index(0-index)
            x: update value
            """
            k += self.num
            self.tree[k] = x
            while k > 1:
                self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
                k >>= 1
    
        def query(self, l, r):
            """
            [l, r)のsegfuncしたものを得る
    
            l: index(0-index)
            r: index(0-index)
            """
            res = self.ide_ele
    
            l += self.num
            r += self.num
            while l < r:
                if l & 1:
                    res = self.segfunc(res, self.tree[l])
                    l += 1
                if r & 1:
                    res = self.segfunc(res, self.tree[r - 1])
                l >>= 1
                r >>= 1
            return res        
 


class SegTree:
    """
    Segment Tree
    """

    def __init__(self, init_val, segfunc, ide_ele):
        """
        初期化

        init_val: 配列の初期値
        """
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新

        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る

        l: index(0-index)
        r: index(0-index)
        """
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res
           


N,K,Q = map(int,input().split())
C = list(map(int,input().split()))
A = list(map(int,input().split()))
A = [i-1 for i in A]
e = [[] for _ in range(N)]

for _ in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    e[a].append((b,1))
    e[b].append((a,1))
    

L = LCA_wp(e)    

ide_ele_0 = (10**10,-1)
segfunc_0 = lambda x,y:min(x,y)
init_0 = [ide_ele_0 for _ in range(K)]
for i in range(K):
    init_0[i] = (L.IN[A[i]],A[i])
    
mT = SegTree(init_0,segfunc_0,ide_ele_0)


ide_ele_1 = (-1,-1)
segfunc_1 = lambda x,y:max(x,y)
init_1 = [ide_ele_1 for _ in range(K)]
for i in range(K):
    init_1[i] = (L.IN[A[i]],A[i])
    
MT = SegTree(init_1,segfunc_1,ide_ele_1)

W = [-1]*N
W[0] = C[0]
def dfs(x):
    
    for ix,c in e[x]:
        if W[ix] != -1:
            continue
        W[ix] = max(C[ix],W[x])
        dfs(ix)
dfs(0)


for _ in range(Q):
    t,x,y = map(int,input().split())
    x -= 1
    y -= 1
    if t == 1:
        mT.update(x,(L.IN[y],y))
        MT.update(x,(L.IN[y],y))
    else:
        l = mT.query(x,y+1)[1]
        r = MT.query(x,y+1)[1]
        p = L.lca(l,r)
        print(W[p])
0