Editorial for COCI '14 Contest 3 #5 Stogovi

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.

Let us imagine a tree of stacks such that each stack from the task corresponds to a path to a certain node in the tree from the root of the tree. Each node will have an integer assigned to it, excluding the root, which will be assigned zero. Every stack label will then have a tree node assigned to it, call it . Precisely the number of the node is the number from the top of stack , and climbing up to the root we would see the whole stack. All the while ignoring the zero at the root, of course.

Let us make sure that this is really possible. At the beginning of the game, we have one empty stack and make a tree with the node zero. While copying stack and adding a new number we simply add a new node to the tree, assign a number to it and set as its parent. While copying stack and removing an element from its top, we will output the number in node and assign the parent of to the new stack. It is evident that the tree-like structure is stable after each operation and that all operations are of constant complexity.

We are still left with efficiently answering the query of type c. For starters, let us notice that the numbers placed on stacks are always ascending. In other words, no two tree nodes will have the same number written in them. So the question "how many of the same numbers are in stacks and " is equal to the question "how many of the same nodes, excluding the root, is on the path from the root to and on the path from the root to ". All such nodes will be in the first part of the path from the root to because once the paths part ways, they will never meet again. Therefore, it is sufficient to find the last such node and output how many nodes there are on the path from the root to itself.

Such node is also called the lowest common ancestor in a tree and there are various methods to find it. The most popular solution is "jumps of powers of 2 length" (for more information, consult this link, of the complexity for preprocessing and per query.

Additionally, we need to notice that in this task we don't have a complete tree for which we can initially build a jumping table, but it isn't a problem. When adding a new node (the only moment when the tree changes), we will calculate all its jumps in the complexity because the jumps of its ancestors have already been calculated.

The total time and memory complexity of this solution is .