Problem:
Do use segment tree

Given a tree with n (1 ≤ n ≤ 200,000) nodes and a list of q (1 ≤ q ≤ 100,000) queries,
process the queries in order and output a value for each output query.
The given tree is connected and each node on the tree has a weight wi (-10,000 ≤ wi ≤ 10,000).

Each query consists of a number ti (ti = 1, 2), which indicates the type of the query , and three numbers ai, bi and ci (1 ≤ ai, bi ≤ n, -10,000 ≤ ci ≤ 10,000).
Depending on the query type, process one of the followings:

(ti = 1: modification query)
Change the weights of all nodes on the shortest path between ai and bi (both inclusive) to ci.

(ti = 2: output query)
First, create a list of weights on the shortest path between ai and bi (both inclusive) in order. After that, output the maximum sum of a non-empty continuous subsequence of the weights on the list. ci is ignored for output queries.

Input

The first line contains two integers n and q.
On the second line, there are n integers which indicate w1, w2, ... , wn.

Each of the following n - 1 lines consists of two integers si and ei (1 ≤ si, ei ≤ n),
which means that there is an edge between si and ei.

Finally the following q lines give the list of queries, each of which contains four integers in the format described above.
Queries must be processed one by one from top to bottom.

Output

For each output query, output the maximum sum in one line.

Sample Input 1

3 4
1 2 3
1 2
2 3
2 1 3 0
1 2 2 -4
2 1 3 0
2 2 2 0

Output for the Sample Input 1

6
3
-4

Sample Input 2

7 5
-8 5 5 5 5 5 5
1 2
2 3
1 4
4 5
1 6
6 7
2 3 7 0
2 5 2 0
2 4 3 0
1 1 1 -1
2 3 7 0

Output for the Sample Input 2

12
10
10
19

Sample Input 3

21 30
10 0 -10 -8 5 -5 -4 -3 1 -2 8 -1 -7 2 7 6 -9 -6 3 4 9
10 3
3 2
3 12
12 4
4 13
4 9
10 21
21 1
1 11
11 14
1 15
10 6
6 17
6 16
6 5
5 18
5 19
10 7
10 8
8 20
1 1 21 -10
1 3 19 10
2 1 13 0
1 4 18 8
1 5 17 -5
2 16 7 0
1 6 16 5
1 7 15 4
2 4 20 0
1 8 14 3
1 9 13 -1
2 9 18 0
1 10 12 2
1 11 11 -8
2 21 15 0
1 12 10 1
1 13 9 7
2 6 14 0
1 14 8 -2
1 15 7 -7
2 10 2 0
1 16 6 -6
1 17 5 9
2 12 17 0
1 18 4 6
1 19 3 -3
2 11 8 0
1 20 2 -4
1 21 1 -9
2 5 19 0

Output for the Sample Input 3

20
9
29
27
10
12
1
18
-2
-3

Solution in Ruby (judge verdict: Accepted)

require 'set'

class LazySegmentTree
  def initialize(arr, l = 0, r = arr.size - 1)
    @l, @r = l, r
    @lazy = nil
    if l == r
      @sum = @left_seq = @seq = @right_seq = arr[l]
    else
      c = (l + r) / 2
      @left_child = self.class.new(arr, l, c)
      @right_child = self.class.new(arr, c + 1, r)
      aggregate
    end
  end

  attr_reader :l, :r, :sum, :left_seq, :seq, :right_seq, :lazy

  def aggregate
    @sum = @left_child.sum + @right_child.sum
    @left_seq = [
      @left_child.left_seq,
      @left_child.sum + @right_child.left_seq
    ].max
    @right_seq = [
      @right_child.right_seq,
      @right_child.sum + @left_child.right_seq
    ].max
    @seq = [
      @left_child.seq,
      @right_child.seq,
      @left_child.right_seq + @right_child.left_seq
    ].max
  end

  def update(l, r, x)
    if @l == @r
      @sum = @left_seq = @seq = @right_seq = x
    elsif l == @l && r == @r
      @lazy = x
    else
      force
      if r <= @left_child.r
        @left_child.update(l, r, x)
      elsif @right_child.l <= l
        @right_child.update(l, r, x)
      else
        @left_child.update(l, @left_child.r, x)
        @right_child.update(@right_child.l, r, x)
      end
      @left_child.force
      @right_child.force
      aggregate
    end
  end

  def force
    return if !@lazy
    x = @lazy
    @lazy = nil

    @sum = x * (@r - @l + 1)
    if x >= 0
      @left_seq = @seq = @right_seq = x * (@r - @l + 1)
    else
      @left_seq = @seq = @right_seq = x
    end

    @left_child.update(@left_child.l, @left_child.r, x)
    @right_child.update(@right_child.l, @right_child.r, x)
  end

  def query(l, r)
    force
    if @l == @r || (l == @l && r == @r)
      return [@sum, @left_seq, @seq, @right_seq]
    elsif r <= @left_child.r
      return @left_child.query(l, r)
    elsif @right_child.l <= l
      return @right_child.query(l, r)
    else
      sum1, left_seq1, seq1, right_seq1 = @left_child.query(l, @left_child.r)
      sum2, left_seq2, seq2, right_seq2 = @right_child.query(@right_child.l, r)
      return [
        sum1 + sum2,
        [left_seq1, sum1 + left_seq2].max,
        [seq1, seq2, right_seq1 + left_seq2].max,
        [right_seq1 + sum2, right_seq2].max
      ]
    end
  end
end

n, q = gets.split.map(&:to_i)
weights = [nil]
weights += gets.split.map(&:to_i)

undirected = (0..n).map { [] }
(n - 1).times do
  u, v = gets.split.map(&:to_i)
  undirected[u] << v
  undirected[v] << u
end

# orientation
tree = Hash[(1..n).map {|i| [i, []] }]
parent = {}
queue = [1]
reached = Set[1]
until queue.empty?
  u = queue.shift
  undirected[u].each do |v|
    next if reached.include?(v)
    reached << v
    tree[u] << v
    parent[v] = u
    queue << v
  end
end
out_deg = Hash[(1..n).map {|i| [i, tree[i].size] }]

# p tree
# p parent
# p out_deg

# compute subtree size
subtree_size = Hash[(1..n).map {|i| [i, 1] }]
queue = (1..n).select {|i| out_deg[i] == 0 }
paths = []
until queue.empty?
  i = queue.shift
  tree[i].each do |j|
    subtree_size[i] += subtree_size[j]
  end
  j = parent[i]
  if j
    out_deg[j] -= 1
    queue << j if out_deg[j] == 0
  end
end

# p subtree_size

# Heavy-Light Decomposition & Lazy Segment Trees
queue = [1]
position = {}
ord = 0

segs = []
until queue.empty?
  i = queue.shift
  path = [i]
  loop do
    heavy = true
    tree[i].sort_by {|j| -subtree_size[j]}.each do |j|
      if heavy
        path << j
        i = j
        heavy = false
      else
        queue << j
      end
    end
    break if heavy
  end
  seg = LazySegmentTree.new(path.map {|i| weights[i] })
  (0..path.size-1).each do |i|
    position[path[i]] = [seg, i, ord, path]
  end
  segs << seg
  ord += 1
end

# process queries
q.times do
  type, u, v, c = gets.split.map(&:to_i)
  # p [u, v]
  
  if type == 1
    seg1, i, o1, path1 = position[u]
    seg2, j, o2, path2 = position[v]
    until o1 == o2
      if o1 > o2
        seg1.update(0, i, c)
        seg1, i, o1, path1 = position[u = parent[path1[0]]]
      else
        seg2.update(0, j, c)
        seg2, j, o2, path2 = position[v = parent[path2[0]]]
      end
    end
    if i <= j
      seg1.update(i, j, c)
    else
      seg2.update(j, i, c)
    end

  else
    seg1, i, o1, path1 = position[u]
    seg2, j, o2, path2 = position[v]
    sum1, l_seq1, seq1, r_seq1 = 0, -Float::INFINITY, -Float::INFINITY, -Float::INFINITY
    sum2, l_seq2, seq2, r_seq2 = 0, -Float::INFINITY, -Float::INFINITY, -Float::INFINITY
    until o1 == o2
      if o1 > o2
        sum0, l_seq0, seq0, r_seq0 = seg1.query(0, i)
        sum1, l_seq1, seq1, r_seq1 = [
          sum0 + sum1,
          [l_seq0, sum0 + l_seq1].max,
          [seq0, seq1, r_seq0 + l_seq1].max,
          [r_seq0 + sum1, r_seq1].max
        ]
        seg1, i, o1, path1 = position[u = parent[path1[0]]]
      else
        sum0, l_seq0, seq0, r_seq0 = seg2.query(0, j)
        sum2, l_seq2, seq2, r_seq2 = [
          sum0 + sum2,
          [l_seq0, sum0 + l_seq2].max,
          [seq0, seq2, r_seq0 + l_seq2].max,
          [r_seq0 + sum2, r_seq2].max
        ]
        seg2, j, o2, path2 = position[v = parent[path2[0]]]
      end
    end
    if i <= j
      sum0, l_seq0, seq0, r_seq0 = seg1.query(i, j)
      sum2, l_seq2, seq2, r_seq2 = [
        sum0 + sum2,
        [l_seq0, sum0 + l_seq2].max,
        [seq0, seq2, r_seq0 + l_seq2].max,
        [r_seq0 + sum2, r_seq2].max
      ]
    else
      sum0, l_seq0, seq0, r_seq0 = seg2.query(j, i)
      sum1, l_seq1, seq1, r_seq1 = [
        sum0 + sum1,
        [l_seq0, sum0 + l_seq1].max,
        [seq0, seq1, r_seq0 + l_seq1].max,
        [r_seq0 + sum1, r_seq1].max
      ]
    end
    puts [seq1, seq2, l_seq1 + l_seq2].max
  end
end