#P16957. [SCCPC 2026] 那一年的秘密基地

    ID: 19041 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 显示难度提高+/省选− 上传者: 标签>四川线段树树状数组2026省赛/邀请赛

[SCCPC 2026] 那一年的秘密基地

Problem Description

That summer, everyone made a promise under the shade of the trees in the town: they must find the most suitable secret base.

There are nn locations in the town, connected by n1n-1 paths. Between any two locations, there is a unique simple path. Therefore, these locations and paths form an unrooted tree, with locations numbered from 11 to nn.

Every day, everyone visits all locations in the town one by one following an order. This order is represented by a permutation a1,a2,,ana_1,a_2,\ldots,a_n of length nn, where aia_i is the ii-th visited location.

If we choose location rr as the secret base, we can root the whole tree at rr. Then, for two different locations x,yx,y, if xx lies on the simple path from rr to yy, we say xx is an ancestor of yy.

Everyone believes that if a location is visited earlier, but one of its ancestors is visited later, then an “exposure risk” occurs. Formally, for a secret base rr, define the danger level fr(a)f_r(a) as the number of pairs (i,j)(i,j) satisfying: 1i<jn1\le i<j\le n, and when rooted at rr, aja_j is an ancestor of aia_i.

In other words, fr(a)f_r(a) is the number of pairs of locations such that, in the current visiting order, the later location is an ancestor of the earlier location.

However, as time goes by, plans may change. Next, there are qq operations. Each operation gives an integer xx, meaning we swap the two adjacent locations axa_x and ax+1a_{x+1} in the visiting order.

Before all operations, and after each operation, you need to choose the most suitable secret base again to make the danger level as small as possible, and output this minimum danger level.

Input Format

The first line contains two integers n,qn,q (1n,q21051\le n,q\le 2\cdot 10^5), representing the number of locations and the number of operations.

The second line contains nn integers a1,a2,,ana_1,a_2,\ldots,a_n (1ain1\le a_i\le n), representing the initial visiting order. It is guaranteed that a1,a2,,ana_1,a_2,\ldots,a_n is a permutation.

The next n1n-1 lines each contain two integers ui,viu_i,v_i (1ui,vin1\le u_i,v_i\le n), indicating there is a path between location uiu_i and location viv_i. It is guaranteed that these n1n-1 paths form a tree.

The next qq lines each contain one integer xix_i (1xi<n1\le x_i<n), indicating an operation where axia_{x_i} and axi+1a_{x_i+1} need to be swapped.

Output Format

Output q+1q+1 lines.

The first line outputs the minimum danger level for the initial visiting order.

Then, the ii-th line outputs the minimum danger level after the (i1)(i-1)-th operation.

5 3
3 5 1 2 4
1 2
2 3
2 4
4 5
2
4
1
3
3
4
4

Hint

Translated by ChatGPT 5