Under construction

Still being built. Things may change

Postal Reform

Time limit2sMemory limit1024 MB

Summary
Given a tree of city heights, process point updates to tower heights and answer the maximum height on the path between two cities.
Level

Medium6 of 10

Topics
Tree, Segment tree
Solved
No attempts yet

Problem

Flatland is going through a season of reforms. Recently the roads were reformed so that every city can be reached from any other city, and there is exactly one simple path between any two cities. The wizards were also reformed, so each city has exactly one wizard. Now the postal system is being reformed.

The newly founded postal agency "Ex-Fedya" offers a unique service called the collective parcel. This service lets you send parcels to the residents of all cities on some path, at the price of a regular parcel. Surprisingly, only the wizards of Flatland have started using it, and they send each other magic cacti in large numbers.

The agency has run into an unexpected problem. All wizards live in towers, and they do not build stairs to them. On top of that, they change the height of their towers from time to time. So to deliver a parcel to a wizard who lives in a tower of height hh, the courier must carry at least hh meters of rope.

You are in charge of logistics. Given the tower heights and their changes, you must find the minimum length of rope that must be issued to a courier who delivers parcels between cities ii and jj.

Input

The first line contains nn, the number of cities in Flatland (1≤n≤500001 \le n \le 50000). The second line contains nn positive integers, none exceeding 10510^5, the tower heights in the cities. The next n−1n-1 lines each contain two integers uiu_i and viv_i, describing the ii-th road (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \ne v_i). The next line contains kk, the number of queries (1≤k≤1000001 \le k \le 100000). Each of the next kk lines is one of the following:

  • A notice from the wizard in city ii that the height of his tower is now hh, in the form !! ii hh (1≤i≤n1 \le i \le n, 1≤h≤1051 \le h \le 10^5).
  • A request from the courier for rope to deliver parcels to every city on the path from ii to jj, inclusive, in the form ?? ii jj (1≤i,j≤n1 \le i, j \le n).

Output

For each delivery query, print the minimum length of rope that must be issued to the courier.

Examples2

  1. Example 1

    Input
    3
    1 2 3
    1 3
    2 3
    5
    ? 1 2
    ! 1 5
    ? 2 3
    ! 3 2
    ? 1 2
    
    Expected output
    3
    3
    5
    
  2. Example 2

    Input
    1
    100
    5
    ! 1 1
    ? 1 1
    ! 1 1000
    ? 1 1
    ! 1 1
    
    Expected output
    1
    1000