#5403. Problem 2. Lineup Counting Queries

0

Problem 2. Lineup Counting Queries

Problem 2. Lineup Counting Queries

USACO 2026 First Contest, Platinum

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 the following form:

  • Out of cows l1r1l_1\dots r_1, how many are located at positions l2r2l_2\dots r_2 immediately after time tt? ($0\le l_1\le r_1\le t, 0\le l_2\le r_2 \le t, 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 five integers specifying a query of the form "l1l_1 r1r_1 l2l_2 r2r_2 tt."

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

Output the answer to each query on a separate line.

SAMPLE INPUT:


4
0 9 0 9 9
3 5 4 5 9
4 5 3 5 9
1 1 3 3 9

SAMPLE OUTPUT:


10
2
1
1

Lineups at 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

At t=9t=9 the cows from front to back are [2,0,4,1,3,5,6,7,8,9][2,0,4,1,3,5,6,7,8,9].

To answer the third query, the cows at positions 353\dots 5 are [1,3,5][1,3,5], and only one of them is in the range 454\dots 5.

SAMPLE INPUT:


1
0 1000000000000000000 0 1000000000000000000 1000000000000000000

SAMPLE OUTPUT:


1000000000000000001

SCORING: Input 3: Q1000,t100Q\le 1000, t\le 100Inputs 4-7: l1=r1l_1 = r_1 for all queriesInputs 8-14: r12l1r_1 \leq 2 \cdot l_1 for all queriesInputs 15-21: No additional constraints

Problem credits: Agastya Goel and Benjamin Qi