#codefestival2015qualBd. [codefestival_2015_qualB_d]Squares, Pieces and Coloring
[codefestival_2015_qualB_d]Squares, Pieces and Coloring
Problem Statement
squares are arranged in a row. The squares are numbered through from left to right.
We have pieces numbered through and a counter that can store an integer between and inclusive.
We will perform processes using these tools. Initially, the squares are painted white. The process is as follows:
- Place piece on square , and set the counter to .
- Look at the color of the square where piece is placed. If the square is white, paint it black and increment the counter by . Otherwise (if the square is black), move piece one square right. As a result, a square may contain multiple pieces.
- Terminate the process if the value of the counter is Otherwise, go back to step
Find the final position of each of the pieces after all the processes.
Input
Input is given from Standard Input in the following format:
:
- The first line contains an integer
- The following lines describe the processes. The of them contains two space-separated integers and denoting the initial position of piece in the process and the termination condition of the process, respectively.
Partial Points
Partial points may be awarded in this problem:
- points will be awarded for passing the test set satisfying the following: The number of the square where each piece is located after all the processes does not exceed .
- Another points will be awarded for passing the test set satisfying
- Another points will be awarded for passing the test set without additional constraints.
Output
Print lines, the of which should contain the number of the square where the piece is located after all the processes. Be sure to print a newline at the end of output.
Sample Input 1
4
3 3
10 1
4 2
2 4
Sample Output 1
5
10
7
11
Illustrated below is the state of the squares and the pieces after each process:
Sample Input 2
8
2 1
3 1
1 1
5 1
1 1
9 1
8 2
7 9
Sample Output 2
2
3
1
5
4
9
10
18
Sample Input 3
5
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
Sample Output 3
1999999999
2999999999
3999999999
4999999999
5999999999
Beware that the correct output may not fit into a -bit integer.