Trees and Queries 10
Time limit2sMemory limit512 MB
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 vertices. A tree is a connected undirected graph with no cycle. The vertices are numbered from 1 to , 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 to 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 to to .
Exactly one path joins two vertices of a tree, so the path of each query is unique. If equals , the path is that single vertex.
Input
The first line contains the number of vertices ().
The second line contains the weights of vertices 1 through in order. Each weight is an integer whose absolute value is at most 10,000.
Each of the next lines contains two vertex numbers and joined by an edge. The given edges form a tree.
The next line contains the number of queries ().
Each of the next lines contains one query in the form 1 u v or 2 u v w, where and 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.