#P7680. [COCI 2008/2009 #5] JAGODA
[COCI 2008/2009 #5] JAGODA
Description
Slavko keeps rabbits, and every day he feeds them various fruits and vegetables. However, the rabbits like strawberries the most. Strawberries are hard to find in the middle of winter and are expensive, so Slavko only gives strawberries to some of the rabbits. Slavko numbers the rabbits from to . To help track how many strawberries each rabbit gets, he decided to use the following strawberry distribution procedure.
Every day, Slavko buys strawberries, then chooses a rabbit , and gives it the first strawberry. After that, rabbit gets the second strawberry, rabbit gets the third, and so on.
He assigns each rabbit a matchbox that is initially empty, and then places these matchboxes in a row. Let be the largest integer such that . Then the rabbits can be divided into groups, each consisting of matchboxes (starting from the first; the last group may have fewer than matchboxes). Next to each group of matchboxes, there is also a cup. We say that consecutive matchboxes and their cup form a group.
After giving the rabbits strawberries, Slavko puts one match into the matchbox of each rabbit that received a strawberry, unless he would end up putting matches into all matchboxes of some group. In that case, he does not put matches into all matchboxes of that group; instead, he puts one match into the cup of that group. In this way, the total number of strawberries received by the rabbits can be computed from the number of matches in the matchboxes plus the number of matches in the cups. For example, suppose Slavko has rabbits, i.e. . The number is the largest integer such that , so . Therefore, the rabbits will be divided into four groups (the last group has only two rabbits and two matchboxes), and naturally there will be four cups, as shown in the figure below:

If Slavko buys strawberries and gives the first one to rabbit , then the cups and matchboxes will look as follows:

Now, write a program that is given the number of rabbits , the number of days , and for each of the days the number of strawberries bought and the index of the rabbit that receives the first strawberry. For each day, output the total number of matches in all matchboxes and cups that Slavko used on that day.
Input Format
The input consists of lines.
The first line contains two integers , representing the total number of rabbits (also the number of matchboxes) and the number of days.
The next lines each contain two integers . In the -th of these lines, is the number of strawberries bought on day , and is the index of the rabbit that receives the first strawberry.
Output Format
Output lines. The -th line should contain one integer, the total number of matches in all matchboxes and cups that Slavko used on day .
11 3
6 5
3 1
11 1
4
1
6
16 3
2 2
12 3
6 11
2
7
3
Hint
[Sample 1 Explanation]
For sample , the arrangement of rabbits, matchboxes, and cups is exactly the example shown in the statement.
The first day is the example from the statement, so Slavko puts in matches on day .
On the second day, Slavko buys strawberries and gives the first strawberry to rabbit . Then rabbits , , and all receive strawberries. Since these three rabbits are in the same group, Slavko only needs to put one match into the first cup. Therefore, Slavko puts in match on day .
On the third day, Slavko can give strawberries to all of his rabbits, so he needs to put one match into every cup. In total, he puts matches into the cups. After placing them, there are matches in the cups he used that day (that is, all cups), so the output is .
[Constraints]
For all data, , , .
[Source]
This problem is from COCI 2008-2009 CONTEST 5 T3 JAGODA. According to the original testdata configuration, the full score is points.
Translated and整理 (zhenglǐ) by Eason_AC.
Translated by ChatGPT 5
京公网安备 11011102002149号