This page is still under construction.

Parts of this page are still being built. What you see may change.

Antarctic Exploration

Time limit30sMemory limit512 MB

Summary
Maintain a dynamic forest under link operations and point updates, answering whether two islands are connected and the total penguins on the path between them.
Level

Medium7 of 10

Topics
Union-find, Tree, Dynamic programming, Implementation
Solved
No attempts yet

Problem

Sanggeun owns the travel agency "Dreaming of Ice." The agency has bought NN islands near Antarctica and runs day trips there. The most popular animal among tourists is the emperor penguin, and it is easy to find on the islands.

The agency has grown more popular, to the point where moving tourists by boat is no longer efficient. Sanggeun plans to build bridges between the islands and move tourists by bus. He will manage the bridge-building process with a computer program.

The islands are numbered 11 through NN. At first there are no bridges, and the number of penguins living on each island is known. The number of penguins can change, but it is always between 00 and 10001000 inclusive.

Sanggeun's program must handle three commands.

  • bridge A B: build a bridge between islands AA and BB. (AA and BB are different.) The bridge must be built only when AA and BB cannot be reached from each other using the bridges built so far. Print yes if the bridge must be built, and no if it is already possible to travel between them and no bridge is needed.
  • penguins A X: a recount shows that island AA now has XX penguins. Print nothing.
  • excursion A B: tourists take a route that starts at island AA and ends at island BB. If AA can be reached from BB, compute and print the total number of penguins on all islands along the route. (Include AA and BB.) If the route is impossible, print impossible.

Write Sanggeun's program.

No further command is given before the answer to a bridge or excursion command is printed. Therefore, the standard output buffer must be flushed after printing.

Input

The first line gives the number of islands NN (1≤N≤30,0001 \le N \le 30,000).

The second line gives the number of penguins on each island.

The third line gives the number of commands QQ (1≤Q≤300,0001 \le Q \le 300,000).

The next QQ lines each contain one of the commands described in the problem.

Output

Print a line each time a bridge or excursion command is given.

Examples2

  1. Example 1

    Input
    5
    4 2 4 5 6
    10
    excursion 1 1
    excursion 1 2
    bridge 1 2
    excursion 1 2
    bridge 3 4
    bridge 3 5
    excursion 4 5
    bridge 1 3
    excursion 2 4
    excursion 2 5
    
    Expected output
    4
    impossible
    yes
    6
    yes
    yes
    15
    yes
    15
    16
    
  2. Example 2

    Input
    6
    1 2 3 4 5 6
    10
    bridge 1 2
    bridge 2 3
    bridge 4 5
    excursion 1 3
    excursion 1 5
    bridge 3 4
    excursion 1 5
    penguins 3 10
    excursion 1 3
    bridge 1 5
    
    Expected output
    yes
    yes
    yes
    6
    impossible
    yes
    15
    13
    no