#P3591. [POI 2015 R3] 访问 Visits

    ID: 2642 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2015POI块状链表,块状数组,分块

[POI 2015 R3] 访问 Visits

Description

You are given a tree with nn vertices. Each edge has length 11, and the weight of vertex ii is aia_i.

Byteasar wants to traverse the whole tree. He will follow a permutation bb of 11 to nn and make n1n-1 walks. On the ii-th walk, he goes from vertex bib_i to vertex bi+1b_{i+1}, and the step size for this walk is cic_i.

For a single walk, suppose the start is xx, the end is yy, and the step size is kk. Byteasar starts at xx and moves forward along the unique path from xx to yy, taking steps of exactly kk edges each time. The testdata guarantees that the distance of each walk is a multiple of kk.

Please help Byteasar compute, for each walk, the sum of the weights of all vertices he visits during that walk.

Input Format

  • The first line contains a positive integer nn (2n500002 \le n \le 50000), the number of vertices.
  • The second line contains nn positive integers, where the ii-th number is aia_i (1ai100001 \le a_i \le 10000), the weight of vertex ii.
  • The next n1n-1 lines each contain two positive integers u,vu, v (1u,vn1 \le u, v \le n), indicating that there is an edge between uu and vv.
  • The next line contains nn distinct positive integers, where the ii-th number is bib_i (1bin1 \le b_i \le n), describing the route (a permutation).
  • The next line contains n1n-1 positive integers, where the ii-th number is cic_i (1ci<n1 \le c_i < n), the step size for the ii-th walk.

Output Format

Output n1n-1 lines. The ii-th line should contain one positive integer: the sum of the weights of all vertices visited during the ii-th walk.

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

10
6
10
5

Hint

Original title: Odwiedziny.

Translated by ChatGPT 5