OTOCI - OTOCI

题意翻译

给出n个结点以及每个点初始时对应的权值wi。起始时点与点之间没有连边。有3类操作: 1、bridge A B:询问结点A与结点B是否连通。 如果是则输出“no”。否则输出“yes”,并且在结点A和结点B之间连一条无向边。 2、penguins A X:将结点A对应的权值wA修改为X。 3、excursion A B:如果结点A和结点B不连通,则输出“impossible”。 否则输出结点A到结点B的路径上的点对应的权值的和。 给出q个操作,要求在线处理所有操作。 数据范围:1<=n<=30000, 1<=q<=300000, 0<=wi<=1000。 任意时刻每个节点对应的权值都是1到1000的整数。

题目描述

[English](/problems/OTOCI/en/) [Vietnamese](/problems/OTOCI/vn/)Some time ago Mirko founded a new tourist agency named "Dreams of Ice". The agency purchased N icy islands near the South Pole and now offers excursions. Especially popular are the emperor penguins, which can be found in large numbers on the islands. Mirko's agency has become a huge hit; so big that it is no longer cost-effective to use boats for the excursions. The agency will build bridges between islands and transport tourists by buses. Mirko wants to introduce a computer program to manage the bridge building process so that fewer mistakes are made. The islands are numbered 1 through N. No two islands are initially connected by bridges. The initial number of penguins on each island is known. That number may change, but will always be between 0 and 1000 (inclusive). Your program must handle the following three types of commands: - "bridge A B" – an offer was received to build a bridge between islands A and B (A and B will be different). To limit costs, your program must accept the offer only if there isn't already a way to get from one island to the other using previously built bridges. If the offer is accepted, the program should output "yes", after which the bridge is built. If the offer is rejected, the program should output "no". - "penguins A X" – the penguins on island A have been recounted and there are now X of them. This is an informative command and your program does not need to respond. - "excursion A B" – a group of tourists wants an excursion from island A to island B. If the excursion is possible (it is possible to get from island A to B), the program should output the total number of penguins the tourists would see on the excursion (including islands A and B). Otherwise, your program should output "impossible".

输入输出格式

输入格式


The first line contains the integer N (1 ≤ N ≤ 30 000), the number of islands. The second line contains N integers between 0 and 1000, the initial number of penguins on each of the islands. The third line contains an integer Q (1 ≤ Q ≤ 300 000), the number of commands. Q commands follow, each on its own line.

输出格式


Output the responses to commands "bridge" and "excursion", each on its own line.

输入输出样例

输入样例 #1

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

输出样例 #1

4
impossible
yes
6
yes
yes
15
yes
15
16

输入样例 #2

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

输出样例 #2

yes
yes
yes
6
impossible
yes
15
13
no