Editorial for An Animal Contest 6 P6 - Generous Grizzly Gift Giving


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: andrewtam

First of all, root the tree at city 1. Denote the size of city v's subtree as size(v), denote the depth of city v as depth(v), and denote the lowest common ancestor of cities u and v as lca(u, v).

Observation 1: The number of gifts from city t that a grizzly v receives is equal to the number of cities u, u \ne v such that lca(u, v) = t.

With this observation, we can begin tackling subtask 1. We will find the confusion value of each grizzly v by performing a DFS on the tree and dynamically maintaining the inversions using a sparse 2D Fenwick tree. First of all, when we are at city 1, all of the gifts received should be from city 1. So initially, we should have 0 inversions, and our Fenwick tree should be updated with the value N-1 at (p_1, q_1). When we enter a child city u from parent city v, exactly size(u) of the gifts from city v should be converted to gifts from city u. However, since a grizzly does not receive a gift from himself, we also gain a gift from city v and lose a gift from city u. Update the inversion count and the Fenwick tree accordingly. When we exit city u and return to its parent v, we simply reverse the changes that we made. In this way, when we are at a particular city v, the inversion count and Fenwick tree are representative of the gifts that grizzly v will receive. So, when we sum up the inversion counts across all cities, we have our answer.

To solve subtask 2, we have to perform the same DFS traversal as in subtask 1, but we also need some additional information. More specifically, for each query, the answer is the total number of inversions minus any inversions that a particular grizzly v is involved in. A grizzly v can be involved in an inversion in two ways: first, the inversions formed by the gifts that grizzly v receives, and second, the inversions formed by the gifts that grizzly v gives. We will preprocess the answer for all v using the DFS. The first case can be handled by saving the inversion count at all cities v. The second case is quite a bit harder, since we cannot find the number of inversions for each city one at a time. Instead, we will consider the inversions caused by each gift and update all of the relevant grizzlies. Note that a gift from city v can only be given to a grizzly that comes from city v's subtree. Suppose we are at city v in the DFS and we want to find all of the inversions caused by the gift from city v. For simplicity, denote the state of the Fenwick tree at city v as state v. First of all, let's handle the case when a gift from city v is given to grizzly v. We can simply query state v of the Fenwick tree using the point (p_v, q_v) and then update v's subtree excluding v itself. For all of v's children u, consider that the gift is given to a grizzly from u's subtree. We want to query the point (p_v, q_v) over all states in u's subtree, and then update v's subtree excluding u's subtree. However, naively implementing this would be too slow. We need a better solution.

Note that each time we update the Fenwick tree at a city v, the update affects the states of all the cities in v's subtree and none others. In other words, the update affects exactly size(v) cities. So, what we will do is maintain a second Fenwick tree. In this Fenwick tree, when we perform an update in the first Fenwick tree, we will also update the second Fenwick tree to use size(v) multiplied by the update value in the first Fenwick tree. When we need to query (p_v, q_v) over all states in u's subtree, we can use the two Fenwick trees in combination. First of all, query the inversions in the first Fenwick tree—these will affect all of the states in the subtree, so we will use this value multiplied by size(u). Then, subtract the inversions in the second Fenwick tree from before we enter city u. Finally, add the inversions in the second Fenwick tree from after we finish our DFS on city u.

To solve subtask 3, we will do the same process as in subtask 2. We will find the total number of inversions and subtract the inversions which grizzlies u and v are involved in. However, we must also consider that grizzly u and v may be involved in the same inversion. As a result, we have subtracted some inversions twice, and therefore, we must add the inversions back. Grizzly u and v can be involved in the same inversion in two ways: first, grizzly u gives a gift to grizzly v that forms an inversion with another gift that grizzly v received (or vice versa, v gives a gift to u), and second, grizzly u and v each give a gift to a third grizzly t, and the gifts that they give form an inversion. Obviously, we can not preprocess the answer for all u, v, so instead, we will perform offline processing for only the pairs of grizzlies that are queried. The first case can be handled by simply querying the gift from lca(u, v) at both u and v. The second case is not quite so simple, and it requires additional observation.

Observation 2: For any three cities t, u, v, if depth(lca(t, v)) < depth(lca(u, v)), then lca(t, u) = lca(t, v). Otherwise, at least one of lca(t, u) or lca(t, v) is equal to lca(u, v).

This implies that if grizzly u and v each give a gift to a third grizzly t, and the gifts that they give form an inversion, then one of the gifts is from city lca(u, v). The other is along the path from u to lca(u, v) or the path from v to lca(u, v). Another smaller observation is that the set of gifts that a grizzly v gives is the same as the set of gifts that a grizzly v receives. Therefore, while we are performing our DFS we will reuse our first Fenwick tree, but this time it represents the set of gifts that the current grizzly gives. When we want the inversions that u and v cause for the second case, we query the inversions of the gift from lca(u, v) at both u and v using the first Fenwick tree. However, we may have counted some inversions with gifts from a city t, such that depth(t) < depth(lca(u, v)). Therefore, we subtract the number of inverted gifts twice when we are at lca(u, v), and we are done.


Comments

There are no comments at the moment.