Antarctic Expedition
Time limit5sMemory limit128 MB
Process bridge, penguin-update, and route-sum queries on an incrementally connected forest, requiring dynamic connectivity checks and path-sum queries under updates.
- Level
Hard8 of 10
- Topics
- Union-find, Tree, Segment tree, Graph
- Solved
- No attempts yet
Problem
A travel company operates N islands near Antarctica as one-day tour destinations. Emperor penguins live on the islands, and the company wants to build bridges between islands so tourists can travel by bus.
The islands are numbered from 1 to N. Initially there are no bridges. The number of penguins on every island is known, but it may change after commands. The number is always between 0 and 1000, inclusive.
Your program must process three kinds of commands.
bridge A B: Build a bridge between islandsAandB. IfAandBare not already reachable using existing bridges, build the bridge and printyes. If they are already reachable, no new bridge is needed, so printno. The two islands are distinct.penguins A X: The number of penguins on islandAchanges toX. This command prints nothing.excursion A B: Tourists take a route that starts on islandAand ends on islandB. If the two islands are connected, print the total number of penguins on every island along the route, includingAandB. If they are not connected, printimpossible.
Write a program that processes all commands in order.
Input
The first line contains the number of islands N. (1 <= N <= 30000)
The second line contains the number of penguins on islands 1 through N, in order.
The third line contains the number of commands Q. (1 <= Q <= 300000)
Each of the next Q lines contains one command: bridge A B, penguins A X, or excursion A B.
Output
For each bridge command and each excursion command, print the required result on its own line.