Under construction

Still being built. Things may change

Trees and Queries 10

Time limit2sMemory limit512 MB

Summary
Given a tree with vertex weights, answer path maximum-subarray-sum queries and path range-assign-weight updates.
Level

Hard9 of 10

Topics
Segment tree, Tree, DFS, Divide and conquer
Solved
No attempts yet

Problem

You are given a tree with NN vertices. A tree is a connected undirected graph with no cycle. The vertices are numbered from 1 to NN, and each vertex carries one integer weight.

Write a program that processes the following two kinds of queries.

  • 1 u v: list the vertices on the path from uu to vv in the order they are visited, then report the largest sum of a contiguous block of that list. The empty block is allowed, so the answer is always at least 0.
  • 2 u v w: change the weight of every vertex on the path from uu to vv to ww.

Exactly one path joins two vertices of a tree, so the path of each query is unique. If uu equals vv, the path is that single vertex.

Input

The first line contains the number of vertices NN (2≤N≤100,0002 \le N \le 100{,}000).

The second line contains the weights of vertices 1 through NN in order. Each weight is an integer whose absolute value is at most 10,000.

Each of the next N−1N-1 lines contains two vertex numbers uu and vv joined by an edge. The given edges form a tree.

The next line contains the number of queries MM (1≤M≤100,0001 \le M \le 100{,}000).

Each of the next MM lines contains one query in the form 1 u v or 2 u v w, where 1≤u,v≤N1 \le u, v \le N and ww is an integer whose absolute value is at most 10,000.

Output

For each query of the first kind, print its answer on its own line, in the order the queries are given. A query of the second kind prints nothing.

Examples2

  1. Example 1

    Input
    5
    -3 -2 1 2 3
    1 2
    2 3
    1 4
    4 5
    3
    1 2 5
    2 3 4 2
    1 2 5
    
    Expected output
    5
    9
    
  2. Example 2

    Input
    2
    -5 -7
    1 2
    4
    1 1 2
    1 2 1
    1 1 1
    1 2 2
    
    Expected output
    0
    0
    0
    0