#P4312. [COI 2009] OTOCI

    ID: 3246 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2009并查集O2优化树链剖分,树剖Link-Cut Tree,LCTCOCI

[COI 2009] OTOCI

Description

Not long ago, Mirko founded a travel agency called “Polar Dream.” The agency bought nn islands near the Arctic and offers sightseeing services.

The most popular attraction is, of course, the emperor penguins. These little fellows often move in groups between the islands. Mirko’s agency suffered a major setback, making cruise tours no longer cost-effective. The agency will build bridges between islands and use sightseeing buses to carry tourists.

Mirko wants to develop a computer program to manage the bridge-building process to avoid unexpected errors. The islands are labeled from 11 to nn. Initially, there are no bridges between islands, and the number of emperor penguins on each island is known. Although the number on each island may change, it is always within [0,1000][0, 1000]. Your program must process the following three commands:

  • bridge u v: Ask whether nodes uu and vv are connected. If they are, output no; otherwise output yes, and add an undirected edge between uu and vv.
  • penguins u x: Set the weight wuw_u of node uu to xx.
  • excursion u v: If nodes uu and vv are not connected, output impossible. Otherwise, output the sum of the weights of the nodes on the path from uu to vv.

There are qq operations in total.

Input Format

  • The first line contains an integer nn, the number of nodes.
  • The second line contains nn integers; the ii-th integer is the initial weight wiw_i of node ii.
  • The third line contains an integer qq, the number of operations.
  • Each of the next qq lines contains one operation as described above.

Output Format

Output the results of all bridge and excursion operations, one answer per line.

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
4
impossible
yes
6
yes
yes
15
yes
15
16
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
yes
yes
yes
6
impossible
yes
15
13
no

Hint

Constraints
For 100%100\% of the testdata, 1n3×1041 \le n \le 3 \times 10^4, 1q3×1051 \le q \le 3 \times 10^5, 0wi10000 \le w_i \le 1000.

Translated by ChatGPT 5