結果

問題 No.900 aδδitivee
ユーザー 双六
提出日時 2020-07-29 21:12:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 775 ms / 2,000 ms
コード長 3,976 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 82,564 KB
実行使用メモリ 207,736 KB
最終ジャッジ日時 2024-06-30 04:47:36
合計ジャッジ時間 19,743 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict, deque
INF = float("inf")

def getlist():
	return list(map(int, input().split()))

class lazySegTree(object):
	# N:処理する区間の長さ
	def __init__(self, N, sign):
		self.N = N
		self.LV = (N - 1).bit_length()
		self.N0 = 2 ** self.LV
		self.initVal = 0
		self.data = [0] * (2 * self.N0)
		self.lazy = [0] * (2 * self.N0)
		self.sign = sign

	# 区間クエリの種類
	def calc(self, a, b):
		return a + b

	def initialize(self, A):
		for i in range(self.N):
			self.data[self.N0 - 1 + i] = A[i]
		for i in range(self.N0 - 2, -1, -1):
			self.data[i] = self.calc(self.data[2 * i + 1], self.data[2 * i + 2])

	# 遅延伝播を行うindexを生成
	def gindex(self, l, r):
		L = (l + self.N0) >> 1; R = (r + self.N0) >> 1
		lc = 0 if l & 1 else (L & -L).bit_length()
		rc = 0 if r & 1 else (R & -R).bit_length()
		for i in range(self.LV):
			if rc <= i:
				yield R
			if L < R and lc <= i:
				yield L
			L >>= 1; R >>= 1

	# 遅延伝搬処理
	def propagates(self, *ids):
		for i in reversed(ids):
			v = self.lazy[i - 1]
			if not v:
				continue
			self.lazy[2 * i - 1] += v; self.lazy[2 * i] += v
			self.data[2 * i - 1] += v * self.sign[2 * i - 1]; 
			self.data[2 * i] += v * self.sign[2 * i]
			self.lazy[i - 1] = 0

	# 区間[l, r]にxを加算
	def update(self, l, r, x):
		*ids, = self.gindex(l, r + 1)
		self.propagates(*ids)

		L = self.N0 + l; R = self.N0 + r + 1
		while L < R:
			if R & 1:
				R -= 1
				self.lazy[R - 1] += x; self.data[R - 1] += x * self.sign[R - 1]
			if L & 1:
				self.lazy[L - 1] += x; self.data[L - 1] += x * self.sign[L - 1]
				L += 1
			L >>= 1; R >>= 1
		for i in ids:
			self.data[i - 1] = self.calc(self.data[2 * i - 1], self.data[2 * i])

	# 区間[l, r]内の和
	def query(self, l, r):
		self.propagates(*self.gindex(l, r + 1))
		L = self.N0 + l; R = self.N0 + r + 1

		s = self.initVal
		while L < R:
			if R & 1:
				R -= 1
				s = self.calc(s, self.data[R - 1])
			if L & 1:
				s = self.calc(s, self.data[L - 1])
				L += 1
			L >>= 1; R >>= 1
		return s


class SegmentTree(object):
	#N:処理する区間の長さ
	def __init__(self, N):
		self.N = N
		self.N0 = 2 ** (N - 1).bit_length()
		self.initVal = 0
		self.data = [self.initVal] * (2 * self.N0)

	# 区間クエリの種類
	def calc(self, a, b):
		return a + b

	# セグメント木の中身をリストAで初期化
	def initialize(self, A):
		for i in range(self.N):
			self.data[self.N0 - 1 + i] = A[i]
		for i in range(self.N0 - 2, -1, -1):
			self.data[i] = self.calc(self.data[2 * i + 1], self.data[2 * i + 2])


class Graph(object):
	def __init__(self):
		self.graph = defaultdict(list)

	def __len__(self):
		return len(self.graph)

	def add_edge(self, a, b):
		self.graph[a].append(b)

def Euler_tour(G, node, euler, sign):
	euler.append(node)
	sign.append(1)
	for i in G.graph[node]:
		Euler_tour(G, i, euler, sign)
	euler.append(node)
	sign.append(-1)

#処理内容
def main():
	N = int(input())
	edgeD = defaultdict(int)
	# edge = []
	G = Graph()
	for i in range(N - 1):
		u, v, w = getlist()
		G.add_edge(u, v)
		edgeD[v] = w
		# edge.append((v, w))

	euler = []
	sign = []
	Euler_tour(G, 0, euler, sign)

	# print(euler)
	# print(sign)

	Seg = SegmentTree(2 * N)
	Seg.initialize(sign)

	Din = defaultdict(lambda:-1)
	Dout = defaultdict(int)

	A = [0] * (2 * N)

	#DinおよびDoutの構築
	for i in range(2 * N):
		var = euler[i]
		if Din[var] == -1:
			Din[var] = i
			A[i] = edgeD[var]
		else:
			Dout[var] = i
			A[i] = -edgeD[var]

	# print(A)

	# signによる重みをもたせる
	lSeg = lazySegTree(2 * N, Seg.data)
	lSeg.initialize(A)

	Q = int(input())
	for i in range(Q):
		query = getlist()
		if query[0] == 1:
			a, x = query[1], query[2]
			lSeg.update(Din[a] + 1, Dout[a] - 1, x)

		else:
			b = query[1]
			ans = lSeg.query(0, Din[b])
			# print(0, Din[b])
			print(ans)


if __name__ == '__main__':
	main()
0