Ornithology

시간 제한5초메모리 제한1024 MB

요약
각 새의 시작 위치와 도착 위치가 주어질 때, 이동 경로가 서로 교차하는 새 쌍의 수를 센다.
난이도

보통10점 중 6점

유형
정렬, 누적 합, 조합론
정답자
아직 제출이 없습니다

문제

On the outskirts of the town, close to the farm, there stand two parallel power lines, separated by a narrow dirt road. The power lines were a favorite resting spot for a variety of birds.

Today, on this cool autumn morning, the lines are filled with a group of birds. On the first line there are nn consecutive positions for birds numbered 0,1,…,n−10, 1, \dots , n - 1. On the second power line there are also nn positions numbered in the same manner.

Initially, every position of the first line is occupied by some birds (possibly zero) and there are no birds on the second power line. Each bird has its desired position to fly to on the second line. No two birds on the same position on the first line share the same desired position.

At one moment, all the birds at once will decide to fly to their desired positions. Every bird flies along the straight line segment connecting its initial and desired position.

It can happen that some pairs of birds crash into each other during their flight. This can happen when their corresponding line segments cross. We shall call such unordered pair of birds dangerous pair.

For example, if a bird on position 22 wants to go to position 11 and the bird on position 1 wants to go to position 22, their paths cross.

The birds will not collide if their paths have the same desired position (on the second line) or if they start from the same position (on the first line). In other words a pair of birds with the same initial or desired positions is not considered a dangerous pair.

The task is very simple. Compute the number of dangerous pairs of birds.

입력

First line of input contains an integer nn (1≤n≤2⋅1051 ≤ n ≤ 2 \cdot 10^5) representing the number of positions on both power lines.

Each of the next nn lines describe the desired positions of the birds. The ii-th line starts with an integer p_ip\_i (0≤p_i≤n0 ≤ p\_i ≤ n) representing the number of birds on position ii.

Then, there are p_ip\_i distinct numbers q_i,1,…,q_i,p_iq\_{i,1}, \dots , q\_{i,p\_i} (0≤q_i,j≤n−10 ≤ q\_{i,j} ≤ n-1) representing the desired places of the p_ip\_i birds.

It is guaranteed that the sum ∑_i=0n−1p_i\sum\_{i=0}^{n-1}{p\_i} does not exceed 2⋅1052 \cdot 10^5.

출력

Output exactly one line containing one integer – the number of dangerous pairs of birds.

예제1

  1. 예제 1

    입력
    3
    2 1 2
    1 0
    1 1
    
    예상 출력
    3