結果

問題 No.235 めぐるはめぐる (5)
ユーザー shotoyooshotoyoo
提出日時 2021-07-26 18:32:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 8,776 ms / 10,000 ms
コード長 9,645 bytes
コンパイル時間 1,603 ms
コンパイル使用メモリ 81,588 KB
実行使用メモリ 278,016 KB
最終ジャッジ日時 2024-07-22 09:33:44
合計ジャッジ時間 32,957 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8,622 ms
278,016 KB
testcase_01 AC 5,323 ms
269,560 KB
testcase_02 AC 8,776 ms
276,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")
writef = lambda x: print("{:.12f}".format(x))


class LSG:
    def __init__(self,n, a=None):
        self._n = n
        self._ninf = ninf
        x = 0
        while (1 << x) < self._n:
            x += 1
        self._log = x
        self._size = 1 << self._log
        self._d = [ninf] * (2 * self._size)
        self._lz = [f0] * self._size
        if a is not None:
            for i in range(self._n):
                self._d[self._size + i] = a[i]
            for i in range(self._size - 1, 0, -1):
                self._update(i)
    def check(self):
        return [self.query_point(p) for p in range(self._n)]
    def update_point(self, p, x):
        p += self._size
        for i in range(self._log, 0, -1):
            self._push(p >> i)
        self._d[p] = x
        for i in range(1, self._log + 1):
            self._update(p >> i)
    def query_point(self, p):
        p += self._size
        for i in range(self._log, 0, -1):
            self._push(p >> i)
        return self._d[p]
    def query(self, left, right):
        if left == right:
            return ninf
        left += self._size
        right += self._size
        for i in range(self._log, 0, -1):
            if ((left >> i) << i) != left:
                self._push(left >> i)
            if ((right >> i) << i) != right:
                self._push(right >> i)
        sml = ninf
        smr = ninf
        while left < right:
            if left & 1:
                sml = op(sml, self._d[left])
                left += 1
            if right & 1:
                right -= 1
                smr = op(self._d[right], smr)
            left >>= 1
            right >>= 1
        return op(sml, smr)
    def query_all(self):
        return self._d[1]
    def update(self, left, right, f):
        if right is None:
            p = left
            p += self._size
            for i in range(self._log, 0, -1):
                self._push(p >> i)
            self._d[p] = mapping(f, self._d[p])
            for i in range(1, self._log + 1):
                self._update(p >> i)
        else:
            if left == right:
                return
            left += self._size
            right += self._size
            for i in range(self._log, 0, -1):
                if ((left >> i) << i) != left:
                    self._push(left >> i)
                if ((right >> i) << i) != right:
                    self._push((right - 1) >> i)
            l2 = left
            r2 = right
            while left < right:
                if left & 1:
                    self._all_apply(left, f)
                    left += 1
                if right & 1:
                    right -= 1
                    self._all_apply(right, f)
                left >>= 1
                right >>= 1
            left = l2
            right = r2
            for i in range(1, self._log + 1):
                if ((left >> i) << i) != left:
                    self._update(left >> i)
                if ((right >> i) << i) != right:
                    self._update((right - 1) >> i)
    def _update(self, k):
        self._d[k] = op(self._d[2 * k], self._d[2 * k + 1])
    def _all_apply(self, k, f) -> None:
        self._d[k] = mapping(f, self._d[k])
        if k < self._size:
            self._lz[k] = 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] = f0
    def loc(self, l, r):
        return self._lz[self._size+l : self._size+r]
    
from itertools import chain
class HLD:
    def __init__(self, g, root=0, a=None):
        """g: 隣接行列
        root : 木の根
        a: 遅延セグ木に与える配列
        """
        self.g = g
        self.n = len(g)
        self.parent = [-1]*self.n
        self.size = [1]*self.n
        self.head = [0]*self.n
        self.preorder = [0]*self.n
        self.k = 0
        self.depth = [0]*self.n
        
        for v in chain(range(root, self.n), range(0, root)):
            if self.parent[v] == -1:
                self.dfs_pre(v)
                self.dfs_hld(v)
        if a is not None:
            na = [0]*self.n
            for i in range(self.n):
                na[self.preorder[i]] = a[i]
            self.sg = LSG(self.n, na)        

    def dfs_pre(self, v):
        g = self.g
        stack = [v]
        order = [v]
        while stack:
            v = stack.pop()
            for u in g[v]:
                if self.parent[v] == u:
                    continue
                self.parent[u] = v
                self.depth[u] = self.depth[v]+1
                stack.append(u)
                order.append(u)

        # 隣接リストの左端: heavyな頂点への辺
        # 隣接リストの右端: 親への辺
        while order:
            v = order.pop()
            child_v = g[v]
            if len(child_v) and child_v[0] == self.parent[v]:
                child_v[0], child_v[-1] = child_v[-1], child_v[0]
            for i, u in enumerate(child_v):
                if u == self.parent[v]:
                    continue
                self.size[v] += self.size[u]
                if self.size[u] > self.size[child_v[0]]:
                    child_v[i], child_v[0] = child_v[0], child_v[i]

    def dfs_hld(self, v):
        stack = [v]
        while stack:
            v = stack.pop()
            self.preorder[v] = self.k
            self.k += 1
            top = self.g[v][0]
            # 隣接リストを逆順に見ていく(親 > lightな頂点への辺 > heavyな頂点 (top))
            # 連結成分が連続するようにならべる
            for u in reversed(self.g[v]):
                if u == self.parent[v]:
                    continue
                if u == top:
                    self.head[u] = self.head[v]
                else:
                    self.head[u] = u
                stack.append(u)

    def for_each(self, u, v):
        # [u, v]上の頂点集合の区間を列挙
        while True:
            if self.preorder[u] > self.preorder[v]:
                u, v = v, u
            l = max(self.preorder[self.head[v]], self.preorder[u])
            r = self.preorder[v]
            yield l, r # [l, r]
            if self.head[u] != self.head[v]:
                v = self.parent[self.head[v]]
            else:
                return

    def for_each_edge(self, u, v):
        # [u, v]上の辺集合の区間列挙
        # 辺の情報は子の頂点に
        while True:
            if self.preorder[u] > self.preorder[v]:
                u, v = v, u
            if self.head[u] != self.head[v]:
                yield self.preorder[self.head[v]], self.preorder[v]
                v = self.parent[self.head[v]]
            else:
                if u != v:
                    yield self.preorder[u]+1, self.preorder[v]
                break

    def subtree(self, v):
        # 頂点vの部分木の頂点集合の区間 [l, r)
        l = self.preorder[v]
        r = self.preorder[v]+self.size[v]
        return l, r

    def lca(self, u, v):
        # 頂点u, vのLCA
        while True:
            if self.preorder[u] > self.preorder[v]:
                u, v = v, u
            if self.head[u] == self.head[v]:
                return u
            v = self.parent[self.head[v]]
            
    def update(self, u, v, val):
        for l,r in self.for_each(u,v):
            self.sg.update(l,r+1,val)
    def query(self, u, v):
        res = ninf
        for l,r in self.for_each(u,v):
            res = op(res, self.sg.query(l,r+1))
        return res
    def update_edge(self, u, v, val):
        for l,r in self.for_each_edge(u,v):
            self.sg.update(l,r+1,val)
    def query_edge(self, u, v):
        res = ninf
        for l,r in self.for_each_edge(u,v):
            res = op(res, self.sg.query(l,r+1))
        return res

def t2i(*vs):
    n = len(vs)+1
    nvs = [1]
    val = 1
    for v in vs[::-1]:
        val *= v
        nvs.append(val)
    vs = nvs[::-1]
    tos = "def to(%s):\n" % (",".join([f"v{i}" for i in range(n)]))
    tos += "    val = 0\n"
    for i,v in enumerate(vs):
        tos += f"    val += {v} * v{i}\n"
    tos += "    return val"
    frms = f"def frm(val):\n"
    for i in range(n-1):
        frms += f"    v{i},val = divmod(val, {vs[i]})\n"
    frms += "    return %s" % ((", ".join([f"v{i}" for i in range(n-1)]) + ", val"))
    return tos, frms
tos, frms = t2i(1,2)
exec(tos)
exec(frms)

# 区間和・区間加算の例
M = 10**9+7
ninf = (0,0)
f0 = 0
def op(x,y):
    xx = x[0]+y[0]
    if xx>M:
        xx -= M
    yy = x[1]+y[1]
    if yy>M:
        yy -= M
    return (xx,yy)
def mapping(f,x):
    return ((f*x[1] + x[0])%M, x[1])
# composition: あとからきたクエリがf1に入る
def composition(f1,f2):
    val = (f1+f2)
    if val>M:
        val -= M
    return val

n = int(input())
s = list(map(int, input().split()))
c = list(map(int, input().split()))
ns = [[] for _ in range(n)]
for _ in range(n-1):
    u,v = map(int, input().split())
    u -= 1
    v -= 1
    ns[u].append(v)
    ns[v].append(u)
hld = HLD(ns, a=list(zip(s,c)))
q = int(input())
ans = []
for i in range(q):
    t = list(map(int, input().split()))
    if t[0]==0:
        _,x,y,z = t
        x -= 1
        y -= 1
        hld.update(x,y,z)
    else:
        _,x,y = t
        x -= 1
        y -= 1
        val = hld.query(x,y)
        ans.append(val[0]%M)
#     break
write("\n".join(map(str, ans)))
0