#P16879. [GKS 2022 #C] Ants on a Stick

[GKS 2022 #C] Ants on a Stick

Problem Description

Ada has NN ants labelled from 11 to NN. She decides to test John's concentration skills. She takes a stick LL cm long, and drops the ants on it.

The positions on the stick at which the ants are dropped are represented by an integer array PP, where ant ii is dropped at the position PiP_i (that is, PiP_i cm away from the left end) on the stick. Each ant travels either to the left or right with a constant speed of 11 cm per second. The initial directions of the ants is represented by an array DD, where the direction of ant ii is DiD_i: 00 if left, and 11 if right. When two ants meet, they bounce off each other and reverse their directions. The ants fall off the stick when they reach either end of it.

Ada challenges John to find the exact order in which the ants fall off the stick. John needs your help!

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow.

The first line of each test case contains two integers, NN and LL: the number of ants, and the length of the stick, respectively.

The next NN lines describe the positions and directions of the ants. The ii-th line contains two integers, PiP_i and DiD_i: the position and direction of ant ii, respectively.

Output Format

For each test case, output one line containing Case #xx: A1A2ANA_1 A_2 \ldots A_N, where xx is the test case number (starting from 11), and AiA_i is the label of the ii-th ant that falls off the stick. In other words, the first ant to fall off the stick is the ant labelled A1A_1, the second is the ant labelled A2A_2, and so on. If multiple ants fall off at the same time, output their labels in the increasing order.

3
1 5
1 1
2 7
4 1
5 0
4 10
8 0
2 1
6 1
4 0
Case #1: 1
Case #2: 2 1
Case #3: 1 2 3 4

Hint

In Sample Case #11, as there is only a single ant (labelled 11), it is the only one to fall off. The time at which it falls off is 44 seconds.

In Sample Case #22, the two ants move towards each other, meet at 0.50.5 seconds and reverse their directions. Ant 22 then reaches the right end of the stick at 33 seconds, whereas ant 11 reaches the left end at 55 seconds. Thus, ant 22 falls off the stick, followed by ant 11.

In Sample Case #33, ants 22 and 44 move towards each other and meet at 11 second. Similarly, ants 11 and 33 also move towards each other and meet at 11 second. All 44 ants then change directions.

  • Ants 11 and 22 move towards either ends of the stick and fall off at 44 seconds.
  • Ants 33 and 44 move towards each other and meet at 33 seconds. They change directions and move towards either ends of the stick, and fall off at 88 seconds.

Limits

1T1001 \le T \le 100.

N<LN < L.

Di{0,1}D_i \in \{0, 1\}, for all ii.

0<Pi<L0 < P_i < L, for all ii.

All PiP_i are distinct.

Test Set 11

1N101 \le N \le 10.

1L1001 \le L \le 100.

Test Set 22

1N1031 \le N \le 10^3.

1L1091 \le L \le 10^9.

Test Set 33

1L1091 \le L \le 10^9.

For at most 1515 cases:

1N1051 \le N \le 10^5.

For the remaining cases:

1N1031 \le N \le 10^3.