luogu#P4811. [COCI 2014/2015 #3] KAMIONI

[COCI 2014/2015 #3] KAMIONI

Problem Description

Translated from COCI 2014/2015 Contest 3 T6 "KAMIONI".

There is a road, and we view it as a number line. There are NN trucks on the road. Initially, all trucks are at integer points. All trucks start moving at the same time, and they all move at the same speed. No truck stays still. Each truck can move 11 unit of distance in 11 minute.

For each truck, its "route" is given. A route is represented by an array A1,A2,,AkA_1,A_2,\dots,A_k with kk elements. The truck first drives from A1A_1 to A2A_2, then immediately turns around and drives to A3A_3, and so on. Ignore the time spent on turning around. Since the truck turns around, we guarantee:

$$A_1 < A_2 > A_3 < A_4 > \dots\ \mathrm{or}\ A_1 > A_2 < A_3 > A_4 < \dots$$

One possible route is 25172→5→1→7 (the given points are either the start, the end, or turning points). At the beginning the truck is at 22. After 33 minutes it reaches position 55, and then turns around. The truck continues to position 11; at this time 77 minutes have passed since it started. The truck turns around again and drives to position 77; at this time 1313 minutes have passed since it started.

When a truck reaches the end of its route, mysterious Planet6174 aliens appear and take it back to their spaceship.

The routes of these NN trucks are given. Now there are MM queries, and each query contains two trucks. For each query, output how many times this pair of trucks appears at the same position. The position does not have to be an integer; for example, they can meet at position 2.52.5.

Note that for every pair of trucks that is queried (not an arbitrary pair), it is guaranteed that:

  • When one of them is taken away by the Planet6174 aliens, they are not at the same position.
  • They are not at the same position at the initial time, nor at the moment when one of them turns around.

Planet6174: The person who translated this problem has already been dealt with (joking).

Input Format

The first line contains two integers NN and M(1N105,1M105)M(1 \le N \le 10^5,1 \le M \le 10^5), representing the number of trucks and the number of queried pairs.

The next NN lines describe the routes.
The ii-th line describes the route of the ii-th truck. The first integer on the line is Ki(1Ki3105)K_i(1 \le K_i \le 3 \cdot 10^5), the length of the route. This is followed by KiK_i integers Aj(1Aj109)A_j(1 \le A_j \le 10^9), representing the points on truck ii's route. The given points are either the start, the end, or turning points.
The total sum of route lengths of all trucks does not exceed 31053 \cdot 10 ^ 5.

The next MM lines each contain two integers (ai,bi)(a_i,b_i), representing the two trucks in the ii-th query.

Output Format

Output MM lines. The ii-th line should contain the number of meetings for the ii-th queried pair of trucks.

3 3
3 1 3 1
2 2 1
3 3 1 3
1 2
2 3
3 1
1
0
2
2 1
4 1 6 3 6
7 3 4 2 6 5 6 1
1 2
3
3 4
3 1 4 2
4 3 4 2 4
3 4 1 3
1 2
2 3
3 1
1 3
2
1
2
2

Hint

For 50%50\% of the testdata, it is guaranteed that N102,Ki103,M103N \le 10^2,K_i \le 10^3,M \le 10^3.

Translated by ChatGPT 5