#P4004. [清华集训 2017] Hello world!

[清华集训 2017] Hello world!

Description

Xiao V has nn problems. His problems are very tricky, so the caring contestant ufozgg plans to weaken them. To avoid being weakened, Xiao V hides all his tricky problems in a tree with nn nodes (nodes are numbered from 11 to nn). Every node on the tree corresponds one-to-one with one of Xiao V’s problems. Each problem has a “trickiness value.” The problem corresponding to node ii (meaning the node labeled ii in the tree, same below) has trickiness value aia_i.

To protect his problems, the magician Xiao V cast a spell on the tree. From then on, anyone who wants to explore the tree must perform jump operations on it. Each jump operation includes a start ss, an end tt, and a step size kk. This means the jumper will start from ss and reach tt by jumping along the simple path on the tree. In each jump, if the shortest path distance from the current node to tt is no more than kk, the jumper jumps directly to tt; otherwise, the jumper advances exactly kk edges along the shortest path toward tt.

Since Xiao V hid the problems in the tree, ufozgg cannot weaken them directly. He must jump on the tree and weaken problems along the way. Every time ufozgg passes through a node (including the start ss, even when s=ts=t), he will take the square root of that node’s problem’s trickiness value and round it down: that is, if he passes node ii, he sets ai=aia_i = \lfloor \sqrt{a_i} \rfloor. We call this a weaken operation.

From time to time, ufozgg also wants to know how much he has weakened the problems. Therefore, in some jump operations he gives up weakening and instead computes the sum of trickiness values of the nodes visited in that jump. We call this a query operation.

The onlooker Lülü is very interested in Xiao V’s tricky problems and ufozgg’s weakening plan. He now wants to know the result ufozgg gets for each query operation. Can you help him?

Input Format

The first line contains a positive integer nn, the number of nodes in the tree.

The next line contains nn space-separated positive integers a1,a2,,ana_1, a_2, \ldots, a_n, describing the trickiness value on each node.

The next n1n-1 lines describe the tree. Each line contains two positive integers u,vu, v, describing an edge (u,v)(u, v). It is guaranteed that 1u,vn1 \le u, v \le n, and that these n1n-1 edges form a tree.

The next line contains a positive integer QQ, the total number of operations performed by ufozgg.

The next QQ lines describe each operation in the order they are performed. Each line contains four space-separated integers op,s,t,kop, s, t, k, meaning the jump starts at ss, ends at tt, with step size kk. If op=0op = 0, it is a weaken operation; if op=1op = 1, it is a query operation.

Output Format

For each query operation, output a single line with one integer, the sum of the trickiness values of all problems counted in that operation.

5
1 2 3 4 5
1 2
2 3
3 4
2 5
5
1 1 4 1
1 1 4 2
0 1 5 2
1 2 4 5
1 1 5 1
10
8 6 5

Hint

For 100%100\% of the testdata, n50000n \le 50000, Q400000Q \le 400000, 1ai10131 \le a_i \le 10^{13}.

For all operations, it is guaranteed that 0op10 \le op \le 1 and 1s,t,kn1 \le s, t, k \le n.

Translated by ChatGPT 5