#5405. Problem 1. Lineup Queries

0

Problem 1. Lineup Queries

Problem 1. Lineup Queries

USACO 2026 First Contest, Silver

There is a line of cows, initially (i.e. at time t=0t = 0) containing only cow 00 at position 00 (here, a cow is at position kk if there are kk cows in front of it). At time tt for t=1,2,3,t=1,2,3,\dots, the cow at position 0 moves to position t/2\lfloor t/2\rfloor, every cow in positions 1t/21\dots \lfloor t/2\rfloor moves forward one position, and cow tt joins the line at the end of the line (position tt).

Answer QQ (1Q1051\le Q\le 10^5) independent queries, each of one of the following types:

  1. At what position is cow cc immediately after time tt (0ct10180\le c\le t\le 10^{18})?
  2. Which cow is at position xx immediately after time tt (0xt10180\le x\le t\le 10^{18})?

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains QQ, the number of queries.

The next QQ lines each contain three integers specifying a query either of the form "1 c t" or "2 x t."

OUTPUT FORMAT (print output to the terminal / stdout):

Output the answer to each query on a separate line.

SAMPLE INPUT:


2
1 4 9
2 2 9

SAMPLE OUTPUT:


2
4

Lineups immediately after various times:


t = 0 | 0
t = 1 | 0 1
t = 2 | 1 0 2
t = 3 | 0 1 2 3
t = 4 | 1 2 0 3 4
t = 5 | 2 0 1 3 4 5
t = 6 | 0 1 3 2 4 5 6
t = 7 | 1 3 2 0 4 5 6 7
t = 8 | 3 2 0 4 1 5 6 7 8
t = 9 | 2 0 4 1 3 5 6 7 8 9

Immediately after t=9t=9, the location of cow 44 is 22, and the cow located at position 22 is 44.

SAMPLE INPUT:


22
1 0 9
1 1 9
1 2 9
1 3 9
1 4 9
1 5 9
1 6 9
1 7 9
1 8 9
1 9 9
2 0 9
2 1 9
2 2 9
2 3 9
2 4 9
2 5 9
2 6 9
2 7 9
2 8 9
2 9 9
1 0 1000000000000000000
2 0 1000000000000000000

SAMPLE OUTPUT:


1
3
0
4
2
5
6
7
8
9
2
0
4
1
3
5
6
7
8
9
483992463350322770
148148148148148148

SCORING: Input 3: Q1000,t100Q\le 1000, t\le 100Input 4: t5000t\le 5000Inputs 5-8: All queries are of type 1.Inputs 9-12: All queries are of type 2.

Problem credits: Agastya Goel