#P4618. [SDOI2018] 原题识别

    ID: 3556 远端评测题 10000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2018各省省选山东O2优化枚举,暴力主席树

[SDOI2018] 原题识别

Description

“Human problem database” Little QQ practices very hard and has solved over ten thousand problems. Whenever someone asks him about a problem, Little QQ can tell within 11 second which OJOJ and which problem it is. Therefore, Little QQ is like an “original problem search engine”.

One day, Little QQ came to a rooted tree with nn nodes. The root is node 11, and each node has a problem printed on it. With his huge amount of experience, Little QQ quickly identified the source of every problem, and found that some problems had been reposted many times. He classified the problem on each node: the problem type of node ii is aia_i. The problems at node ii and node jj come from the same source if and only if ai=aja_i = a_j.

Solving the same problem multiple times does not improve skill, except increasing the number of ACAC submissions. To investigate the quality of the problems on this tree, Little QQ will ask the following two types of queries for a total of mm times:

  • 11 xx yy: If you solve all problems on the shortest path from node xx to node yy (including xx and yy), how many essentially different problems can you solve in total?

  • 22 AA BB: If you choose a node xx uniformly at random on the shortest path from node AA to the root (including AA and the root), and choose a node yy uniformly at random on the shortest path from node BB to the root (including BB and the root), what is the expected answer to query 11 xx yy?

Let cntxcnt_x denote the number of nodes on the shortest path from node xx to the root. Since Little QQ does not like fractions, and the answer to the second type of query can always be written as anscntA×cntB\frac{ans}{{cnt_A}\times{cnt_B}}, you only need to output the value of ansans.

Identifying these problems has consumed too much of Little QQ’s energy, so he cannot compute the answers to these simple queries by himself. Please write a program to answer all mm queries for Little QQ.

Input Format

The first line contains a positive integer TT, denoting the number of testdata.

For each testdata, the first line contains 55 positive integers n,p,SA,SB,SCn, p, SA, SB, SC, where nn is the number of nodes in the tree.

To reduce the input size to some extent, the tree edges and the problem type array a[]a[] will be generated by the following code:

unsigned int SA, SB, SC;
unsigned int rng61(){
	SA ^= SA << 16;
	SA ^= SA >> 5;
	SA ^= SA << 1;
	unsigned int t = SA;
	SA = SB;
	SB = SC;
	SC ^= t ^ SA;
	return SC;
}
void gen(){
	scanf("%d%d%u%u%u", &n, &p, &SA, &SB, &SC);
	for(int i = 2; i <= p; i++)
	addedge(i - 1, i);
	for(int i = p + 1; i <= n; i++)
	addedge(rng61() % (i - 1) + 1, i);
	for(int i = 1; i <= n; i++)
	a[i] = rng61() % n + 1;
}

The second line contains a positive integer mm, denoting the number of queries.

The next mm lines each contain 33 positive integers, in the form 11 xx yy or 22 AA BB, representing each query in order.

Output Format

For each testdata, output mm lines, one integer per line, answering each query in order.

2
5 3 10000 12345 54321
3
1 2 3
2 1 3
1 3 2
10 6 23456 77777 55555
5
1 1 10
2 3 5
2 7 5
2 5 4
1 8 6
1
5
1
4
34
61
45
3

Hint

  • $1 \le T \le 3,2 \le p \le n \le 10^5,1 \le m \le 2\times 10^5$.
  • $10^4 \le SA, SB, SC \le 10^6,1 \le x, y, A, B \le n$.

Subtask 11 (3030 points): only contains type 11 queries.

Subtask 22 (3030 points): satisfies p=np = n.

Subtask 33 (4040 points): no additional constraints.

Translated by ChatGPT 5