PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000114. Truy vấn trên cây (Queries on the Tree)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Truy vấn trên cây (Queries on the Tree)

Queries on the Tree

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a directed tree with NN vertices numbered 1,2,…,N1,2,\ldots,N, rooted at vertex 11. Every edge points from a parent to a child, and all vertices are reachable from the root. Initially, every vertex contains 00 coins.

The depth of a vertex is its distance from the root, measured in edges; the root has depth 00. The subtree of XX contains XX and all its descendants.

Process MM operations in order:

  • 1 L Y: add YY coins to every vertex at depth exactly LL across the entire tree.
  • 2 X: output the total number of coins in the subtree rooted at XX at the current time.

Answers may be large; use 64-bit integer arithmetic.

Input

  • The first line contains integers N,MN,M.
  • Each of the next N−1N-1 lines contains u v, a directed edge from parent uu to child vv.
  • The final MM lines contain 1 L Y or 2 X.

Vertices are 1-indexed, the root is vertex 1, and edges can appear in any order.

Output

For every operation 2 X, print the total number of coins in the subtree of XX on its own line.

Subtasks

  • Subtask 1 (15 points): 1≤N≤1001\le N\le100, 1≤M≤1001\le M\le100; rooted directed tree at 1; 0≤L≤H0\le L\le H, 0≤Y≤1090\le Y\le10^9, 1≤X,u,v≤N1\le X,u,v\le N, where HH is the maximum depth.

  • Subtask 2 (20 points): 1≤N≤2 0001\le N\le2\,000, 1≤M≤1 0001\le M\le1\,000; rooted directed tree at 1; 0≤L≤H0\le L\le H, 0≤Y≤1090\le Y\le10^9, 1≤X,u,v≤N1\le X,u,v\le N.

  • Subtask 3 (25 points): 1≤N≤20 0001\le N\le20\,000, 1≤M≤5 0001\le M\le5\,000; rooted directed tree at 1; 0≤L≤H0\le L\le H, 0≤Y≤1090\le Y\le10^9, 1≤X,u,v≤N1\le X,u,v\le N.

  • Subtask 4 (40 points): 1≤N≤1051\le N\le10^5, 1≤M≤1041\le M\le10^4; rooted directed tree at 1; 0≤L≤H0\le L\le H, 0≤Y≤1090\le Y\le10^9, 1≤X,u,v≤N1\le X,u,v\le N.

Examples

Example 1

Input

5 4
1 2
1 3
3 4
3 5
1 1 2
1 2 3
2 3
2 1

Output

8
10

Explanation

The first update adds 2 to vertices 2 and 3; the second adds 3 to vertices 4 and 5. Subtree 3 totals 2+3+3=82+3+3=8; the whole tree totals 2+2+3+3=102+2+3+3=10.