Tree Hull

정점을 넣고 빼는 질의마다 현재 집합을 모두 포함하는 최소 부분트리의 간선 가중치 합을 출력한다.

어려움8트리DFS수학누적 합아직 제출이 없습니다시간 제한3초메모리 제한256 MB

문제

You are given an edge-weighted tree.

Consider a set AA which is a subset of vertices of the tree. Initially, AA is empty, and we have to process queries which ask to add a vertex to AA or remove a vertex from AA.

After each query, find the weight of the minimum subtree containing all vertices from AA. We define the weight of the subtree as the sum of weights of its edges.

입력

The first line of input contains an integer nn: the size of the tree (1n31051 \le n \le 3 \cdot 10^{5}).

The next n1n-1 lines describe edges of the tree. Each edge is described as "uu vv ww": its endpoints and weight (1u,vn1 \le u, v \le n, uvu \ne v, 0w1090 \le w \le 10^{9}). It is guaranteed that the given edges form a tree.

The following line contains an integer qq: the number of queries (1q31051 \le q \le 3 \cdot 10^{5}).

The next qq lines contain queries. Each query is given as "tt vv", where tt is either "+" (add vertex to AA) or "-" (remove vertex from AA), and vv is the number of the vertex (1vn1 \le v \le n). It is guaranteed that you are never asked to add a vertex which is already in AA, or to remove a vertex which is not currently in AA.

출력

Print qq numbers: the weight of the smallest subtree containing all vertices from AA after each query. In case AA is empty, print a 00.