結果

問題 No.1038 TreeAddQuery
ユーザー 👑 rin204rin204
提出日時 2023-07-08 14:07:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,543 ms / 4,000 ms
コード長 7,333 bytes
コンパイル時間 342 ms
コンパイル使用メモリ 87,260 KB
実行使用メモリ 201,588 KB
最終ジャッジ日時 2023-09-29 15:26:48
合計ジャッジ時間 42,824 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
71,436 KB
testcase_01 AC 60 ms
71,304 KB
testcase_02 AC 65 ms
71,240 KB
testcase_03 AC 250 ms
81,652 KB
testcase_04 AC 216 ms
81,780 KB
testcase_05 AC 207 ms
83,944 KB
testcase_06 AC 195 ms
81,488 KB
testcase_07 AC 201 ms
82,212 KB
testcase_08 AC 1,911 ms
186,804 KB
testcase_09 AC 2,126 ms
193,148 KB
testcase_10 AC 2,163 ms
195,632 KB
testcase_11 AC 2,237 ms
193,968 KB
testcase_12 AC 2,254 ms
194,336 KB
testcase_13 AC 3,194 ms
201,264 KB
testcase_14 AC 2,943 ms
199,480 KB
testcase_15 AC 3,543 ms
200,960 KB
testcase_16 AC 2,653 ms
200,628 KB
testcase_17 AC 2,596 ms
198,788 KB
testcase_18 AC 559 ms
136,500 KB
testcase_19 AC 647 ms
126,620 KB
testcase_20 AC 657 ms
126,072 KB
testcase_21 AC 749 ms
130,436 KB
testcase_22 AC 1,081 ms
143,308 KB
testcase_23 AC 1,139 ms
150,160 KB
testcase_24 AC 1,818 ms
176,220 KB
testcase_25 AC 3,135 ms
201,588 KB
testcase_26 AC 1,843 ms
197,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)


def input():
    return sys.stdin.readline().rstrip("\r\n")


class CentroidDecomposition:
    def __init__(self, n, edges=None):
        self.n = n
        self.par = [-1] * n  # 重心分解木の親
        self.depth = [-1] * n  # 重心分解木の深さ
        self.size = [-1] * n  # 重心分解木の部分木のサイズ
        self.childcnt = [0] * n  # 重心分解木の子の数
        if edges is None:
            self.edges = [[] for _ in range(n)]
        else:
            self.edges = edges
            # コピーしてないので注意

        self.pars = [[] for _ in range(n)]  # pars[i][j] := 頂点 i の先祖の内,深さが j であるもの
        self.centroids = []  # centroids[i] := 深さが i の重心のリスト
        self.treeind = []  # treeind[i][j] := 頂点 j が深さ i の重心の何番目の部分木か
        self.cent_dist = []  # cent_dist[i][j] := 頂点 j が深さ i の重心からの距離

    def add_edge(self, u, v):
        self.edges[u].append(v)
        self.edges[v].append(u)

    def read_edges(self, indexed=1):
        for _ in range(self.n - 1):
            u, v = map(int, input().split())
            u -= indexed
            v -= indexed
            self.add_edge(u, v)

    def build(self):
        stack = [(0, -1, 0, -1)]
        while stack:
            pos, bpos, d, c = stack.pop()
            st = [pos]
            route = []
            sz = 0

            if len(self.treeind) <= d:
                self.treeind.append([-1] * self.n)
                self.cent_dist.append([-1] * self.n)
            if d >= 1:
                self.cent_dist[d - 1][pos] = 1

            while st:
                pos = st.pop()
                if bpos != -1:
                    self.pars[pos].append(bpos)
                self.depth[pos] = -2
                route.append(pos)
                sz += 1
                if d >= 1:
                    self.treeind[d - 1][pos] = c

                for npos in self.edges[pos]:
                    if self.depth[npos] == -1:
                        st.append(npos)
                        if d >= 1:
                            self.cent_dist[d - 1][npos] = self.cent_dist[d - 1][pos] + 1

            g = -1
            while route:
                pos = route.pop()
                self.size[pos] = 1
                self.depth[pos] = -1
                isg = True
                for npos in self.edges[pos]:
                    if self.depth[npos] == -1:
                        self.size[pos] += self.size[npos]
                        if self.size[npos] * 2 > sz:
                            isg = False
                            break

                if isg and 2 * self.size[pos] >= sz:
                    g = pos

            if len(self.centroids) == d:
                self.centroids.append([])
            self.centroids[d].append(g)

            self.size[g] = sz
            self.par[g] = bpos
            self.depth[g] = d
            self.cent_dist[d][g] = 0

            if sz != 1:
                c = 0
                for npos in self.edges[g]:
                    if self.depth[npos] == -1:
                        stack.append((npos, g, d + 1, c))
                        c += 1
                self.childcnt[g] = c

    def cent_ind_dist(self, u):
        """
        u + u の各先祖の {頂点番号,何番目の部分木か,距離} を返す
        """

        ret = [(u, -1, 0)]
        for d in range(len(self.pars[u]) - 1, -1, -1):
            ret.append((self.pars[u][d], self.treeind[d][u], self.cent_dist[d][u]))

        return ret


class BIT:
    def __init__(self, n):
        self.n = n
        self.data = [0] * (n + 1)
        if n == 0:
            self.n0 = 0
        else:
            self.n0 = 1 << (n.bit_length() - 1)

    def sum_(self, i):
        s = 0
        while i > 0:
            s += self.data[i]
            i -= i & -i
        return s

    def sum(self, l, r=-1):
        if r == -1:
            return self.sum_(l)
        else:
            return self.sum_(r) - self.sum_(l)

    def get(self, i):
        return self.sum(i, i + 1)

    def add(self, i, x):
        i += 1
        while i <= self.n:
            self.data[i] += x
            i += i & -i

    def lower_bound(self, x):
        if x <= 0:
            return 0
        i = 0
        k = self.n0
        while k > 0:
            if i + k <= self.n and self.data[i + k] < x:
                x -= self.data[i + k]
                i += k
            k //= 2
        return i + 1


n, Q = map(int, input().split())
G = CentroidDecomposition(n)
G.read_edges()
G.build()
logn = len(G.centroids)
bit = [BIT(n) for _ in range(logn)]
subbit = [BIT(2 * n) for _ in range(logn)]
L = [0] * n
subL = [0] * n

for d in range(logn):
    c = 0
    c2 = 0
    for g in G.centroids[d]:
        L[g] = c
        if d != 0:
            subL[g] = c2
        c += G.size[g]
        c2 += G.size[g] + 1


def add(x, y, z):
    bg = -1
    for g, j, d in G.cent_ind_dist(x):
        dd = y - d
        if dd >= 0:
            bit[G.depth[g]].add(L[g], z)
            bit[G.depth[g]].add(L[g] + min(dd + 1, G.size[g]), -z)
            if j != -1:
                subbit[G.depth[g]].add(subL[bg] + 1, z)
                subbit[G.depth[g]].add(subL[bg] + min(dd + 1, G.size[bg] + 1), -z)
        bg = g


def get(x):
    bg = -1
    ret = 0
    for g, j, d in G.cent_ind_dist(x):
        ret += bit[G.depth[g]].sum(L[g] + d + 1)
        if j != -1:
            ret -= subbit[G.depth[g]].sum(subL[bg] + d + 1)
        bg = g

    return ret


for _ in range(Q):
    x, y, z = map(int, input().split())
    x -= 1
    print(get(x))
    add(x, y, z)
0