結果
問題 | No.900 aδδitivee |
ユーザー | tktk_snsn |
提出日時 | 2023-02-01 20:12:11 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 769 ms / 2,000 ms |
コード長 | 11,221 bytes |
コンパイル時間 | 193 ms |
コンパイル使用メモリ | 81,792 KB |
実行使用メモリ | 122,680 KB |
最終ジャッジ日時 | 2024-07-01 13:48:54 |
合計ジャッジ時間 | 19,228 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 79 ms
74,368 KB |
testcase_01 | AC | 77 ms
74,368 KB |
testcase_02 | AC | 101 ms
79,104 KB |
testcase_03 | AC | 98 ms
79,168 KB |
testcase_04 | AC | 100 ms
79,324 KB |
testcase_05 | AC | 104 ms
78,976 KB |
testcase_06 | AC | 106 ms
78,848 KB |
testcase_07 | AC | 753 ms
119,076 KB |
testcase_08 | AC | 733 ms
110,848 KB |
testcase_09 | AC | 741 ms
118,656 KB |
testcase_10 | AC | 769 ms
119,168 KB |
testcase_11 | AC | 730 ms
118,268 KB |
testcase_12 | AC | 766 ms
118,784 KB |
testcase_13 | AC | 707 ms
119,296 KB |
testcase_14 | AC | 694 ms
111,104 KB |
testcase_15 | AC | 711 ms
110,936 KB |
testcase_16 | AC | 743 ms
118,528 KB |
testcase_17 | AC | 706 ms
118,528 KB |
testcase_18 | AC | 720 ms
118,528 KB |
testcase_19 | AC | 739 ms
119,040 KB |
testcase_20 | AC | 719 ms
119,028 KB |
testcase_21 | AC | 728 ms
118,644 KB |
testcase_22 | AC | 633 ms
122,680 KB |
testcase_23 | AC | 630 ms
122,240 KB |
testcase_24 | AC | 647 ms
122,584 KB |
testcase_25 | AC | 603 ms
122,240 KB |
testcase_26 | AC | 627 ms
122,064 KB |
testcase_27 | AC | 618 ms
122,492 KB |
testcase_28 | AC | 636 ms
122,112 KB |
ソースコード
import sys import os import inspect input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) if os.getenv("TKTKLOCAL", False): def debug(*arg, sep=" ", end="\n"): print(*arg, sep=sep, end=end, file=sys.stderr) def debug_indent(*arg, sep=" ", end="\n", indent=" "): frame = inspect.currentframe().f_back par_func = inspect.getframeinfo(frame).function if par_func == "<module>": debug(*arg, sep=sep, end=end) return frame_stack = inspect.stack() if len(frame_stack) > 30: return depth = sum(f.function == par_func for f in frame_stack) debug(indent * (depth - 1), end="") debug(*arg, sep=sep, end=end) else: def debug(*arg, **kwarg): pass def debug_indent(*arg, **kwarg): pass class SegTree(object): def __init__(self, N, op_data, u_data): self._n = N self.log = (N-1).bit_length() self.size = 1 << self.log self.op = op_data self.e = u_data self.data = [u_data] * (2 * self.size) self.len = [1] * (2 * self.size) def __repr__(self): res = [self.get(i) for i in range(self._n)] return " ".join(map(str, res)) def _update(self, i): self.data[i] = self.op(self.data[i << 1], self.data[i << 1 | 1]) def initialize(self, arr=None): """ segtreeをarrで初期化する。len(arr) == Nにすること """ if arr: for i, a in enumerate(arr, self.size): self.data[i] = a for i in reversed(range(1, self.size)): self._update(i) self.len[i] = self.len[i << 1] + self.len[i << 1 | 1] def update(self, p, x): """ data[p] = x とする (0-indexed)""" p += self.size self.data[p] = x for i in range(1, self.log + 1): self._update(p >> i) def get(self, p): """ data[p]を返す """ return self.data[p + self.size] def prod(self, l, r): """ op_data(data[l], data[l+1], ..., data[r-1])を返す (0-indexed) """ sml = self.e smr = self.e l += self.size r += self.size 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): """ op(data[0], data[1], ... data[N-1])を返す """ return self.data[1] def max_right(self, l, func): """ func(l, l+1, ..., r-1) = True, func(l, l+1, ..., r-1, r) = Falseとなる r を返す """ if l == self._n: return self._n l += self.size sm = self.e while True: while l % 2 == 0: l >>= 1 if not func(self.op(sm, self.data[l])): while l < self.size: l <<= 1 if func(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, func): """ func( l, l+1, ..., r-1) = True, func(l-1, l, l+1, ..., r-1) = Falseとなる l を返す """ if r == 0: return 0 r += self.size sm = self.e while True: r -= 1 while r > 1 and r & 1: r >>= 1 if not func(self.op(self.data[r], sm)): while r < self.size: r = r << 1 | 1 if func(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 LazySegTree(SegTree): def __init__(self, N, op_data, u_data, op_lazy, u_lazy, op_merge): super().__init__(N, op_data, u_data) self.composition = op_lazy self.mapping = op_merge self.id = u_lazy self.lazy = [u_lazy] * self.size def _all_apply(self, i, F): self.data[i] = self.mapping(F, self.data[i], self.len[i]) # self.data[i] = self.mapping(F, self.data[i]) if i < self.size: self.lazy[i] = self.composition(F, self.lazy[i]) def _push(self, i): self._all_apply(i << 1, self.lazy[i]) self._all_apply(i << 1 | 1, self.lazy[i]) self.lazy[i] = self.id def update(self, p, x): """ data[p] = x とする (0-indexed)""" p += self.size for i in reversed(range(1, self.log + 1)): self._push(p >> i) self.data[p] = x for i in range(1, self.log + 1): self._update(p >> i) def apply(self, p, F): """ data[p]にFを作用させる(data[p] = op_merge(F, data[p])とする, 0-indexed) """ p += self.size for i in reversed(range(1, self.log + 1)): self._push(p >> i) self.data[p] = self.mapping(F, self.data[p], self.len[p]) # self.data[p] = self.mapping(F, self.data[p]) for i in range(1, self.log + 1): self._update(p >> i) def range_apply(self, l, r, F): """ i = l, l+1, ..., r-1 について、Fを作用させる(op_merge(F, data[i]), 0-indexed) """ if l == r: return l += self.size r += self.size for i in reversed(range(1, self.log + 1)): # too->down 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 get(self, p): """ data[p]を返す """ p += self.size for i in reversed(range(1, self.log + 1)): self._push(p >> i) return self.data[p] def prod(self, l, r): """ op_data(data[l], data[l+1], ..., data[r-1])を返す (0-indexed) l == rの時は単位元u_dataを返す """ if l == r: return self.e l += self.size r += self.size for i in reversed(range(1, self.log + 1)): if ((l >> i) << i) != l: self._push(l >> i) if ((r >> i) << i) != r: self._push(r >> i) sml = self.e smr = 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 max_right(self, l, func): """ func(l, l+1, ..., r-1) = True, func(l, l+1, ..., r-1, r) = Falseとなる r を返す """ if l == self._n: return self._n l += self.size for i in reversed(range(1, self.log + 1)): self._push(l >> i) sm = self.e while True: while l % 2 == 0: l >>= 1 if not func(self.op(sm, self.data[[l]])): while l < self.size: self._push(l) l <<= 1 if func(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, func): """ func( l, l+1, ..., r-1) = True, func(l-1, l, l+1, ..., r-1) = Falseとなる l を返す """ if r == 0: return 0 r += self.size for i in reversed(range(1, self.log + 1)): self._push((r - 1) >> i) sm = self.e while True: r -= 1 while r > 1 and r & 1: r >>= 1 if not func(self.op(self.data[r], sm)): while r < self.size: self._push(r) r = r << 1 | 1 if func(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 """ 遅延セグ木(ac-library移植) op_data(d_L, d_R) : d_Lとd_Rの二項演算, dataを返す op_lazy(lz_new, lz_orig) : lz_origにlz_newを作用させる, lazyを返す op_merge(lz, d) : dにlzを作用させる, dataを返す """ u_data = 0 u_lazy = 0 def op(L, R): return L + R def composition(nw, od): return nw + od def mapping(lz, d, sz): return lz * sz + d def main(): N = int(input()) G = [[] for _ in range(N)] par = [-1] * N dist = [0] * N for _ in range(N - 1): a, b, w = map(int, input().split()) G[a].append((b, w)) par[b] = a dist[b] = w iter = [0] * N go_from = [-1] * N go_to = [-1] * N back_from = [-1] * N back_to = [-1] * N memo_go = [] memo_back = [] go_from[0] = 0 back_from[0] = 0 que = [0] while que: s = que[-1] if iter[s] < len(G[s]): t, d = G[s][iter[s]] iter[s] += 1 que.append(t) memo_go.append(d) go_from[t] = len(memo_go) back_from[t] = len(memo_back) else: que.pop() go_to[s] = len(memo_go) back_to[s] = len(memo_back) memo_back.append(dist[s]) debug() debug(memo_go) debug(go_from) debug(go_to) debug() debug(memo_back) debug(back_from) debug(back_to) seg_go = LazySegTree(N, op, u_data, composition, u_lazy, mapping) seg_back = LazySegTree(N, op, u_data, composition, u_lazy, mapping) seg_go.initialize(memo_go) seg_back.initialize(memo_back) Q = int(input()) for _ in range(Q): flag, *arg = map(int, input().split()) if flag == 1: a, x = arg seg_go.range_apply(go_from[a], go_to[a], x) seg_back.range_apply(back_from[a], back_to[a], x) else: a, = arg answer = seg_go.prod(0, go_from[a]) answer -= seg_back.prod(0, back_from[a]) print(answer) main()