結果

問題 No.900 aδδitivee
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2024-01-07 13:45:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,645 ms / 2,000 ms
コード長 8,546 bytes
コンパイル時間 313 ms
コンパイル使用メモリ 82,568 KB
実行使用メモリ 157,700 KB
最終ジャッジ日時 2024-09-27 19:23:19
合計ジャッジ時間 33,848 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
58,184 KB
testcase_01 AC 50 ms
58,364 KB
testcase_02 AC 61 ms
69,448 KB
testcase_03 AC 65 ms
69,680 KB
testcase_04 AC 63 ms
69,092 KB
testcase_05 AC 63 ms
68,700 KB
testcase_06 AC 64 ms
69,900 KB
testcase_07 AC 1,632 ms
157,272 KB
testcase_08 AC 1,573 ms
155,300 KB
testcase_09 AC 1,556 ms
155,076 KB
testcase_10 AC 1,591 ms
156,532 KB
testcase_11 AC 1,611 ms
156,232 KB
testcase_12 AC 1,588 ms
155,164 KB
testcase_13 AC 1,622 ms
155,452 KB
testcase_14 AC 1,583 ms
154,952 KB
testcase_15 AC 1,581 ms
155,068 KB
testcase_16 AC 1,620 ms
156,640 KB
testcase_17 AC 1,598 ms
155,760 KB
testcase_18 AC 1,600 ms
156,392 KB
testcase_19 AC 1,610 ms
156,396 KB
testcase_20 AC 1,571 ms
155,304 KB
testcase_21 AC 1,645 ms
157,700 KB
testcase_22 AC 853 ms
116,032 KB
testcase_23 AC 849 ms
115,484 KB
testcase_24 AC 850 ms
115,732 KB
testcase_25 AC 850 ms
115,700 KB
testcase_26 AC 856 ms
116,500 KB
testcase_27 AC 874 ms
115,576 KB
testcase_28 AC 857 ms
115,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
from itertools import *
from functools import *
from heapq import *
import sys,math
input = sys.stdin.readline

#区間加算区間最小値        
ope = lambda x,y : (x[0]+y[0],x[1]+y[1])
ide_ele = (0,0)
mapping = lambda f,x : (x[0]+f*x[1],x[1])
composition = lambda f,g : f+g #g→fの順に作用
id_ = 0


class lazy_segtree():
    def __init__(self, lst, ope, e, mapping, composition, id_):
        self.n = len(lst)
        self.log = (self.n - 1).bit_length()
        self.size = 1 << self.log
        self.data = [e for _ in range(2 * self.size)]
        self.lz = [id_ for _ in range(self.size)]
        self.e = e
        self.op = ope
        self.mapping = mapping
        self.composition = composition
        self.identity = id_
        for i in range(self.n):
            self.data[self.size + i] = lst[i]
        for i in range(self.size - 1, 0, -1):
            self.update(i)
        
    def update(self, k):
        self.data[k] = self.op(self.data[2 * k], self.data[2 * k + 1])
        
    def all_apply(self, k, f):
        self.data[k] = self.mapping(f, self.data[k])
        if k < self.size:
            self.lz[k] = self.composition(f, self.lz[k])

    def push(self, k):
        self.all_apply(2 * k, self.lz[k])
        self.all_apply(2 * k + 1, self.lz[k])
        self.lz[k] = self.identity

    def set(self, p, x):
        p += self.size
        for i in range(self.log, 0, -1):
            self.push(p >> i)
        self.data[p] = x
        for i in range(1, self.log + 1):
            self.update(p >> i)

    def get(self, p):
        p += self.size
        for i in range(self.log, 0, -1):
            self.push(p >> i)
        return self.data[p]

    def prod(self, l, r):
        if l == r: return self.e
        l += self.size
        r += self.size
        for i in range(self.log, 0, -1):
            if (l >> i) << i != l:
                self.push(l >> i)
            if (r >> i) << i != r:
                self.push(r >> i)
        sml, smr = self.e, self.e
        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):
        return self.data[1]

    def apply_point(self, p, f):
        p += self.size
        for i in range(self.log, 0, -1):
            self.push(p >> i)
        self.data[p] = self.mapping(f, self.data[p])
        for i in range(1, self.log + 1):
            self.update(p >> i)

    def apply(self, l, r, f):
        if l == r: return
        l += self.size
        r += self.size
        for i in range(self.log, 0, -1):
            if (l >> i) << i != l:
                self.push(l >> i)
            if (r >> i) << i != r:
                self.push((r - 1) >> i)
        l2, r2 = l, r
        while l < r:
            if l & 1:
                self.all_apply(l, f)
                l += 1
            if r & 1:
                r -= 1
                self.all_apply(r, f)
            l >>= 1
            r >>= 1
        l, r = l2, r2
        for i in range(1, self.log + 1):
            if (l >> i) << i != l:
                self.update(l >> i)
            if (r >> i) << i != r:
                self.update((r - 1) >> i)

    def max_right(self, l, g):
        if l == self.n: return self.n
        l += self.size
        for i in range(self.log, 0, -1):
            self.push(l >> i)
        sm = self.e
        while 1:
            while i % 2 == 0:
                l >>= 1
            if not g(self.op(sm, self.data[l])):
                while l < self.size:
                    self.push(l)
                    l *= 2
                    if g(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, g):
        if r == 0:
            return 0
        r += self.size
        for i in range(self.log, 0, -1):
            self.push((r - 1) >> i)
        sm = self.e
        while 1:
            r -= 1
            while r > 1 and r % 2 == 1:
                r >>= 1
            if not g(self.op(self.data[r], sm)):
                while r < self.size:
                    self.push(r)
                    r = 2 * r + 1
                    if g(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


class HLD():
    
    ### HL分解をしてIDを振りなおしたものに対して、パスに含まれる区間を返す
    ### SegTreeにのせる配列はIDを並び替えたもの
    
    
    def __init__(self,e,root=0):
        
        
        self.N = len(e)
        self.e = e
        par = [-1]*N
        sub = [-1]*N
        self.root = root
        dist = [-1]*N
        v = deque()
        dist[root]=0
        v.append(root)
        while v:
            x = v.popleft()
            for ix in e[x]:
                if dist[ix] !=-1:
                    continue
                dist[ix] = dist[x] + 1
                v.append(ix)
        
        H = [(-dist[i],i) for i in range(N)]
        H.sort()
        for h,i in H:
            tmp = 1
            for ix in e[i]:
                if sub[ix] == -1:
                    par[i]= ix
                else:
                    tmp += sub[ix]
            sub[i] = tmp
        
        
        self.ID = [-1]*N
        self.ID[self.root]=0
        self.HEAD = [-1]*N
        head = [-1]*N
        self.PAR = [-1]*N
        visited = [False]*N
        self.HEAD[0]=0
        head[self.root]=0
        depth = [-1]*N
        depth[self.root]=0
        self.DEPTH = [-1]*N
        self.DEPTH[0]=0
        cnt = 0
        v = deque([self.root])
        self.SUB = [0]*N
        self.SUB[0] = N
        while v:
            x = v.popleft()
            visited[x]=True
            self.ID[x]=cnt
            cnt += 1
            n = len(self.e[x])
            tmp = [(sub[ix],ix) for ix in self.e[x]]
            tmp.sort()
            flg = 0
            if x==self.root:
                flg -= 1
            for _,ix in tmp:
                flg += 1
                if visited[ix]:
                    continue
                v.appendleft(ix)
                if flg==n-1:
                    head[ix] = head[x]
                    depth[ix] = depth[x]
                else:
                    head[ix] = ix
                    depth[ix] = depth[x]+1
        
        for i in range(self.N):
            self.PAR[self.ID[i]] = self.ID[par[i]]
            self.HEAD[self.ID[i]] = self.ID[head[i]]
            self.DEPTH[self.ID[i]] = depth[i]
            self.SUB[self.ID[i]] = sub[i]
        
    def path_query(self,l,r):
        L = self.ID[l]
        R = self.ID[r]
        res = []
        if self.DEPTH[L]<self.DEPTH[R]:
            L,R = R,L
        
        while self.DEPTH[L] != self.DEPTH[R]:
            tmp = (self.HEAD[L],L+1)
            res.append(tmp)
            L = self.PAR[self.HEAD[L]]
        
        while self.HEAD[L] != self.HEAD[R]:
            tmp = (self.HEAD[L],L+1)
            res.append(tmp)
            L = self.PAR[self.HEAD[L]]            
            tmp = (self.HEAD[R],R+1)
            res.append(tmp)
            R = self.PAR[self.HEAD[R]]        
        
        if L>R:
            L,R = R,L
            
        tmp = (L,R+1)
        res.append(tmp)
        
        return res
        
    def sub_query(self,k):
        
        K = self.ID[k]
        
        return (K,K+self.SUB[K])

N = int(input())
e = [[] for _ in range(N)]
W = [0]*N
for _ in range(N-1):
    u,v,w = map(int,input().split())
    e[u].append(v)
    e[v].append(u)
    W[v] = w

Q = int(input())
hld = HLD(e)
B = [(0,1)]*N
ID = hld.ID[:]
for i,idx in enumerate(ID):
    B[idx] = (W[i],1)

T = lazy_segtree(B,ope,ide_ele,mapping,composition,id_)

for _ in range(Q):
    query = tuple(map(int,input().split()))
    if query[0]==1:
        a,x = query[1:]
        l,r = hld.sub_query(a)
        T.apply(l+1,r,x)
    else:
        b = query[1]
        tmp = 0
        for l,r in hld.path_query(0,b):
            tmp += T.prod(l,r)[0]
        print(tmp)
0