Blue vertex distance sums on a tree

Process paint and distance-sum queries on a weighted tree, reporting for each query 2 the total distance from x to all blue vertices.

Hard8TreePrefix sumDFSDynamic programmingNo attempts yetTime limit5sMemory limit512 MB

Problem

There is a tree with NN vertices, numbered 00 through N1N-1. Every edge has a length. The distance between two vertices is the sum of the edge lengths on the unique path that joins them.

At the start every vertex is white. Process the following two kinds of queries in the given order.

  • Query 1: paint vertex xx blue. If vertex xx is already blue it stays blue, and it is still counted once.
  • Query 2: compute the sum of the distances between vertex xx and every blue vertex. If vertex xx is itself blue, the distance 00 is part of the sum. If no vertex is blue, the sum is 00.

Write a program that answers every query 2.

Input

The first line has the number of vertices NN (2N100,0002 \le N \le 100{,}000) and the number of queries QQ (1Q100,0001 \le Q \le 100{,}000).

Each of the next N1N-1 lines has an edge as uu, vv, ww: vertices uu and vv are joined by an edge of length ww. (0u,vN10 \le u, v \le N-1, uvu \ne v, 0w1,000,0030 \le w \le 1{,}000{,}003)

Each of the next QQ lines has one query. The first integer is the kind of the query, 11 or 22, and the second integer is xx (0xN10 \le x \le N-1).

Output

For each query 2, print its answer on its own line, in the order the queries are given.