#P17417. 【MX-X31-T7】「FAOI-R14」Hello & Bye, Days(加强版)

【MX-X31-T7】「FAOI-R14」Hello & Bye, Days(加强版)

Background

on this lonely day

Cheers for the farewell against the light.

Maybe I am just a puppet.

Forced to cry before an inevitable ending.

Problem Description

The original problem is P17411, but it was passed by some solutions that were not intended to pass, so extra constraints were added to make the memory and time limits stricter.

You are given a tree with nn nodes. You need to maintain a set of key nodes SS, which is empty at the beginning. Let dis(x,y)\mathrm{dis}(x,y) be the distance between xx and yy on the tree, and define d(x)=min⁡y∈Sdis(x,y)d(x)=\min\limits_{y\in S}\mathrm{dis}(x,y).

Each node ii also has a positive integer weight wiw_i.

Then there are qq operations. The ii-th operation is of one of the following two types:

  • 1 x: If xx is in SS, delete xx from SS; otherwise, add xx into SS.

  • 2 x: Output ∑i=1n[d(i)=dis(x,i)]wi\sum\limits_{i=1}^n[d(i)=\mathrm{dis}(x,i)]w_i. It is guaranteed that x∈Sx\in S here. That is, count how many nodes ii satisfy that xx is one of the closest key nodes to ii.

Some test points require you to answer queries online.

Input Format

The first line contains three integers n,q,tn,q,t, representing the number of nodes, the number of operations, and a parameter related to forced online queries.

Next n−1n-1 lines each contain a pair of integers xi,yix_i,y_i, representing an edge.

The next line contains nn integers w1⋯wnw_1\cdots w_n, representing the weight of each node.

Next qq lines each contain two integers op zop\space z, where opop is the operation type, and zz is the information before decryption.

Let

$$x=z\mathbin{\operatorname{xor}}\mathrm{(lastans\times t)},$$

then xx is the real node index for this operation, where lastans\mathrm{lastans} is the answer to the previous query, initially 00.

It is guaranteed that:

  • After decryption, 1≤x≤n1\le x\le n.
  • For operation 2, after decryption x∈Sx\in S.
  • After each operation is executed, the set SS is non-empty.

The encrypted node index zz in the input may be 00.

Output Format

Output several lines, each line is the answer to one query.

5 7 0
1 2
2 3
3 4
4 5
1 1 1 1 1
1 1
2 1
1 5
2 1
2 5
1 1
2 5
5
3
3
5

Hint

Sample Explanation

The tree is a chain:

1−2−3−4−51-2-3-4-5
  • After adding key node 11, S={1}S=\{1\}. The closest key node of every node is 11, so the first query outputs 55.

  • After adding key node 55, S={1,5}S=\{1,5\}:

    • For key node 11, the nodes that satisfy the condition are 1,2,31,2,3.
    • For key node 55, the nodes that satisfy the condition are 3,4,53,4,5.

Node 33 has distance 22 to both key nodes, so it will be counted in both queries.

  • Finally, delete key node 11. Now S={5}S=\{5\}. The closest key node of every node is 55, so the output is 55.

Constraints

This problem has subtasks.

  • For 10%10\% of the testdata, n,q≤3000n,q\leq 3000.
  • For 40%40\% of the testdata, n,q≤5×104n,q\leq 5\times 10^4.
  • For another 10%10\% of the testdata, the tree is a chain.
  • For another 20%20\% of the testdata, t=0t=0.
  • For 100%100\% of the testdata, 1≤n,q≤2×1051\leq n,q\leq 2\times 10^5, 1≤xi,yi≤n1\leq x_i,y_i\leq n, t∈{0,1}t\in \{0,1\}, op∈{1,2}op\in \{1,2\}, 0≤z<230,1≤wi≤1030\leq z<2^{30},1\leq w_i\leq 10^3.

Translated by ChatGPT 5