#P7768. 「CGOI-1」收税

    ID: 8893 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>树状数组Special JudgeO2优化可持久化线段树差分

「CGOI-1」收税

Background

An easy check-in problem.

Because holding the Ugly Contest cost too much money, ac decided to send Push_Y to collect taxes.

Problem Description

The Ugly Country consists of nn cities, and city 11 is the capital. These nn cities are connected by n1n-1 bidirectional roads of length 11. Starting from city xx, moving away from the capital (that is, moving to child nodes), all cities within distance at most hh are the tax-collection range of city xx.

City ii needs to pay aia_i yuan in income tax. However, since the tax officer Push_Y likes XOR very much, the final tax paid will be the XOR sum of the tax amounts that should be paid by every city in its tax-collection range.

Push_Y will ask you mm queries. In each query, he asks how many thousand yuan of income tax city xx will collect when the tax-collection distance is hh.

Simplified statement:

Given a tree with nn nodes, rooted at node 11. The weight of node ii is aia_i. Let depudep_u be the depth of node uu, and the root has depth 11. There are qq queries. Each query gives two integers x,hx,h, asking for the value of uson(x)depudepxhai\bigoplus_{u\in son(x)\land dep_u-dep_x\le h}a_i after dividing by 10001000.

Here, i=1ni\bigoplus_{i=1}^ni means $1\operatorname{xor} 2\operatorname{xor}\cdots\operatorname{xor} n$.

Here, \land means “and”, and xor\operatorname{xor} means XOR.

Input Format

The first line contains two positive integers n,mn,m, representing the number of cities and the number of queries.

The second line contains nn positive integers aia_i, representing the income tax amount that each city should pay.

The third line contains n1n-1 positive integers. The ii-th number fif_i means that city i+1i+1 is connected by a road to city fif_i.

Starting from line 44, there are mm lines. Each line contains two positive integers x,hx,h, representing one query.

Output Format

For each query, output one line with one real number, representing the amount of tax collected by that city.

Keep three digits after the decimal point.

6 3
604 545 402 378 25 13
1 2 2 3 3
1 2
3 0
2 4
0.149
0.402
0.733
6 3
6 5 4 3 2 1
1 2 2 3 3
1 2
3 0
2 4
0.004
0.004
0.001

Hint

For 30%30\% of the testdata, 1n,m1031\le n,m\le 10^3.

For 70%70\% of the testdata, 1n,m5×1041\le n,m\le5 \times 10^4. Among them, 20%20\% of the testdata are chains.

For 100%100\% of the testdata, 1n,m1061\le n,m\le 10^6, 1ai1091 \le a_i \le 10^9, 1xn1\le x \le n, 0hn0 \le h \le n.

Translated by ChatGPT 5