QOJ.ac

QOJ

حد الوقت: 4 s حد الذاكرة: 1024 MB مجموع النقاط: 100

#18826. Tree and Query 23

الإحصائيات

We have a tree consisting of $N$ vertices numbered from $0$ to $N-1$. Additionally, an integer sequence $A_0, A_1, \ldots, A_{N-1}$ of length $N$ is given.

For integers $l, r$ satisfying $0 \le l < r \le N$, vertices $v, d$, and a nonnegative integer $k$, define $f(l, r, v, d, k) = 10^{-999999999} dist(v, d) + \sum_{i = l}^{r - 1} (A_i + k) dist(v, i)$. Here, $dist(a, b)$ is the length of the shortest path between two vertices $a$ and $b$.

The following queries must be processed:

  • l r d k: Output the vertex $v$ that minimizes $f(l, r, v, d, k)$. It can be proven that such $v$ is unique.

Note that the queries are given online. See the Input section for details.

Input

The first line contains the size of the tree $N$. ($1 \le N \le 150{,}000$)

The next $N-1$ lines each contain two integers $u, v$, indicating that there is an edge connecting vertices $u$ and $v$. ($0 \le u, v \le N-1$)

The next line contains the sequence $A_0, A_1, \ldots, A_{N-1}$. ($0 \le A_i \le 150{,}000$)

The next line contains the number of queries $Q$. ($1 \le Q \le 150{,}000$)

The next $Q$ lines each contain four integers $a, b, z, d$. ($0 \le a, b, d \le N-1$, $0 \le z \le 150{,}000$)

Let $X_i$ be the answer to the $i$-th query ($0 \le i \le Q-1$). For the $i$-th query, $l, r, k$ can be recovered by the following formulas. $d$ is used as given in the input.

  • $a^\prime = (a + \sum_{j = 0}^{i - 1} X_j) \bmod N$
  • $b^\prime = (b + 2 \sum_{j = 0}^{i - 1} X_j) \bmod N$
  • $k = (z + (\sum_{j = 0}^{i - 1} X_j)^2) \bmod 150\,001$
  • $l = \min(a^\prime, b^\prime)$
  • $r = 1 + \max(a^\prime, b^\prime)$

Output

Output the answers to the problem over $Q$ lines, one per line.

Examples

Input 1

5
0 1
1 3
3 2
3 4
1000 10 1 100 10000
15
0 0 0 0
0 1 150000 1
2 0 0 2
3 0 150000 3
4 2 150000 4
1 1 149975 0
0 0 149965 1
1 2 149951 2
1 4 149901 3
3 4 149804 4
2 0 149745 0
3 1 149639 1
1 4 149517 2
4 3 149375 3
0 1 149160 4

Output 1

0
0
0
1
4
1
1
3
4
2
3
3
3
4
4

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.