Amphibious Operation
Limits: 3 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Ukrainian troops continue their active counteroffensive! Our paratroopers plan new attacking operations every day.
Currently, \(n\) villages are under enemy control, connected by \((n-1)\) roads of length 1 kilometer such that every village can be reached from any other (possibly through other villages). The \(i\)-th village contains \(a_i\) occupiers. The village numbered one is located furthest west, all villages directly connected to it are located slightly further east, those connected to them (except for village number 1) are even further east, and so on.
The paratroopers are considering \(q\) options for the next attack. The \(i\)-th option involves landing in the village numbered \(v_i\) and quickly neutralizing all enemies in the villages that are at a distance of no more than \(d_i\) kilometers from the landing site. Note that our paratroopers only move forward, and therefore they neutralize enemies only in the villages located further east than the landing site. Help find the exact number of neutralized enemies for each of the attack options.
Input
The first line contains two integers \(n\) and \(q\) — the number of villages and the number of attack options.
The second line contains \(n\) integers \(a_i\) — the number of occupiers in the \(i\)-th village.
The next \(n-1\) lines contain two integers \(u_i\) and \(w_i\) — the roads between the villages.
The next \(q\) lines contain two integers \(v_i\) and \(d_i\) — the landing site and the maximum distance at which the paratroopers will neutralize enemies.
Output
In \(q\) lines, output one number each — the number of neutralized enemies for each attack option.
Constraints
\(1 \le n, q \le 10^{5}\),
\(1 \le a_i \le 10^{9}\),
\(1 \le u_i, w_i \le n\),
\(1 \le v_i, d_i \le n\).
Problem scoring consists of the following subtasks:
1 point — example from the statement,
4 points — a block of tests in which \(n, q\le 10^{3}\).
5 points — a block of tests in which \(d_i = n\).
15 points — a block of tests without additional constraints.
Points for a subtask will be awarded only if you give the correct answer to all tests in the subtask.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 7 4 3 2 4 1 2 1 5 1 2 2 3 3 4 3 5 4 6 1 7 2 2 3 2 3 1 5 3 | 9 8 7 2 |
Notes
the answer to the first query is \(9\), because the paratroopers neutralize enemies in villages \(2,3,4,5\)
the answer to the second query is \(8\), because the paratroopers neutralize enemies in villages \(3,4,5,6\)
the answer to the third query is \(7\), because the paratroopers neutralize enemies in villages \(3,4,5\)
the answer to the fourth query is \(2\), because the paratroopers neutralize enemies in village \(5\)
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Amphibious Operation - Editorial
First of all, one needed to realize that we are given a rooted tree. Then the roads from a vertex to the vertices further east are the same as the edges to its children in the tree. So a query becomes: compute the sum of the vertices in the subtree of \(v_i\) at distance at most \(d_i\).
For convenience, compute \(h[i]\), the depth of the \(i\)-th vertex, that is, its distance to the root of the tree. For this we write, for example, a depth-first search. Then the vertices of a subtree have a greater depth than its root, and the distance from a vertex to its ancestor equals the difference of their depths. So to get the points for the first test group, it was enough to traverse the tree starting from \(v_i\) and going to vertices of greater depth, adding the value of a vertex to the answer if its depth is at most \(h[v_i] + d_i\). This solution takes \(O(q \cdot n)\).
For the second test group, one needed to notice that the distance from the current vertex to all others is always less than \(n\). Then the problem becomes computing the sum of a subtree, which can be done in the same traversal where we compute \(h[i]\), storing it in the array \(sum\).
For the group without restrictions, notice that we can add \(sum[v_i]\) and then subtract the values \(sum\) of the vertices of the subtree at depth \(h[v_i] + d_i + 1\), since the vertices at this depth must not be counted, and neither must their subtrees, where the depth is even greater. In the initial depth-first search, we add each vertex to the list of vertices at its depth. Then, for any vertex, all vertices of its subtree at a given depth go consecutively in the list for this depth. So we need to find the range of vertices at depth \(h[v_i] + d_i + 1\) that belong to the subtree of \(v_i\) and compute their sum. The sum can be computed with prefix sums, since there are no changes.
Finding the bounds of the range is harder. To do it, we record the Euler tour of the tree. This can be done in our initial depth-first search by storing for each vertex \(tin\) and \(tout\), the times when we entered and left it. With these values, we can tell whether a vertex \(u\) is strictly in the subtree of \(v\) by checking \(tin[v] < tin[u] < tout[u] < tout[v]\). Now for each depth we can keep not the list of vertices but the list of their values \(tin\). This list is sorted, since we add the vertices in the order we visited them. In this list we need to find \(l\), the index of the leftmost vertex of the level that is in the subtree of \(v\), that is, the \(u\) with the smallest \(tin[u] > tin[v]\). We also need \(r\), the index of the rightmost vertex of the level in the subtree of \(v\), that is, the \(u\) with the largest \(tin[u] < tout[v]\). These are the bounds of the range whose sum we need to subtract. To find them, we can use binary search over the entry times of the corresponding level.
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|