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

#LCA0000075. Jamie và cây (Jamie and Tree)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Jamie và cây (Jamie and Tree)

Jamie and Tree

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A tree has nn vertices with integer values ava_v and initially root 11. Process three commands in order: 1 r changes the current root to rr; 2 u v x finds w=LCA⁡(u,v)w=\operatorname{LCA}(u,v) under the current root, then adds xx to every vertex in the subtree of ww under that root; 3 v prints the sum of values in the subtree of vv under the current root. Changing the root does not change stored values, but it changes subsequent subtree interpretations.

Input

First line: n qn\ q. Second line: nn values a1,…,ana_1,\ldots,a_n. Then n−1n-1 edges u vu\ v and qq commands in the forms described above.

Output

For each type-3 command print one integer: the subtree sum.

Subtasks

General constraints: 1≤n,q≤1051\le n,q\le10^5; 1≤r,u,v≤n1\le r,u,v\le n; ∣ai∣,∣x∣≤109|a_i|,|x|\le10^9; các tổng có thể cần số nguyên 64 bit.

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤q≤501\le q\le 50.

  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤q≤10001\le q\le 1000.

  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤q≤1000001\le q\le 100000.

Examples

Input

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

Output

15
21
31

Explanation

The first query returns the whole-tree sum 1515. The subsequent update changes values in the queried subtree. After rerooting to 44, a subtree can become the complement of an old subtree.