N마리의 외계인들이 직선 형태의 고속도로에서 N개의 트럭을 운전한다. 이 트럭들은 이상하게 움직이는데, 항상 1의 속도로 운전하며, 방향을 돌리는 지점의 좌표는 항상 정수이다.
외계인들의 수장인 민준이는 N마리의 외계인들이 트럭을 어떻게 운전하는 지에 대한 정보를 알고 있다. 트럭의 경로는 K개의 정수 A1, A2, …, Ak로 이루어진 수열이며, 아래 정보를 만족한다.
A1 < A2 > A3 < A4 > … 이거나 A1 > A2 < A3 > A4 < …
트럭은 A1에서 시작해서 A2, A3, …, Ak-1에서 방향을 돌린 후 Ak에서 도착하며, 트럭이 도착한 이후에는 UFO가 나타나서 트럭을 올려 보낸다.
어떤 외계인들끼리는 정보를 교환해야 하므로 몇 번 만나는지 알아야 한다. 민준이가 구해야 하는 외계인들의 쌍에 대해서는 아래 조건을 만족한다.
전체 데이터의 50%는 N ≤ 100, M, K ≤ 1,000이다.
입력 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
All trucks are moving with the same speed and no single truck is standing still at any given moment. Each truck takes 1 minute to pass the distance between two adjacent cities. You are given the route of each truck. All of the trucks start their route at the same initial moment. The route is given as an array of k cities: A1, A2, ..., Ak. The truck starts from city A1 and drives to city A2, then turns and drives to city A3 and so on. Given the fact that the truck turns, it will hold:
A1 < A2 > A3 < A4 > ... or A1 > A2 < A3 > A4 < ...
The time it takes for the truck to turn is negligible.
One possible route is 2, 5, 1, 7. The truck is at city number 2 initially, 3 minutes after departure it arrives to city number 5. It turns and continues towards the city number 1 in which it arrives 7 minutes after departure. It turns again and drives towards city number 7 in which it arrives at moment 13.
After the truck completes his route, aliens appear and take it away in their space rocket.
For some pairs of trucks, we want to know the number of times they encountered each other on the road. In other words, how many times they appeared to be on the same position (the position they encountered doesn’t need to be an integer; i.e. they could’ve encountered at position 2.5).
Write a programme that will, for a given number of trucks N and their routes and for M pairs of trucks, determine the number of encounters for each pair.
Please note: each pair of trucks we want to know the number of encounters for, it will hold:
The first integer in the line, Ki (2 <= Ki <= 3 * 10^5) represents the number of cities on the truck’s route. After it Ki integers follow, Aj (1 <= Aj <= 10^9), the ordinal numbers of the cities on the truck’s route given in the order which the truck visits them.
The sum of routes of all the trucks won’t exceed 3*10^5 .
Each of the following M lines contains two integers (ai , bi), the ordinal numbers of the trucks we want to know the number of encounters for.
입력 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
출처:coci 2013/2014 6/6 번역:functionx