結果

問題 No.1030 だんしんぐぱーりない
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2022-01-04 07:40:00
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 6,991 bytes
コンパイル時間 178 ms
コンパイル使用メモリ 13,440 KB
実行使用メモリ 85,432 KB
最終ジャッジ日時 2024-10-13 21:37:33
合計ジャッジ時間 7,002 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
16,768 KB
testcase_01 AC 36 ms
11,520 KB
testcase_02 AC 34 ms
11,520 KB
testcase_03 AC 33 ms
11,392 KB
testcase_04 AC 33 ms
11,392 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
権限があれば一括ダウンロードができます

ソースコード

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