DMOPC '20 Contest 3 P2 - Bob and Parallel-Ks

View as PDF

Submit solution


Points: 7 (partial)
Time limit: 2.0s
Memory limit: 512M

Author:
Problem type

Bob is composing a song for M singers to perform! The song lasts for N beats, and the x-th singer is assigned a series of N notes ax,1,,ax,N to sing on each of the beats. Notes are represented by integer values, and the M notes sung on a single beat are all distinct.

Unfortunately, Bob needs to watch out for parallel-Ks. A parallel-K is a triple (x,y,t) such that ay,tax,t=ay,t+1ax,t+1=K. In other words, a parallel-K is two singers x and y, plus a beat t, such that the notes that x and y sing form an interval of K on both beats t and t+1.

Parallel-Ks make music sound absolutely horrendous (for some reason), so please help Bob find all the parallel-Ks in his song!

Constraints

1N20
1K109
1aij109
For a given j, a1j,,aMj are distinct.

Subtask 1 [2/15]

1M1000

Subtask 2 [5/15]

1M50000

Subtask 3 [8/15]

1M200000

Input Specification

The first line contains three space-separated integers: M, N, and K.
The next M lines each contain N space-separated integers, ai1,,aiN, the notes sung on each beat by singer i.

Output Specification

The number of distinct parallel-Ks in Bob's song. (Two parallel-Ks (x1,y1,t1) and (x2,y2,t2) are distinct if x1x2, or y1y2, or t1t2.)

Sample Input

Copy
5 3 5
5 6 6
10 11 11
15 16 16
105 116 118
110 111 113

Sample Output

Copy
5

Explanation for Sample Output

Singers 1 and 2 form two parallel-5s: one between beats 1 and 2, and another between beats 2 and 3. Singers 2 and 3 also form two parallel-5s. Finally, singers 5 and 4 form one parallel-5 between beats 2 and 3. In total, there are five parallel-5s: (1,2,1), (1,2,2), (2,3,1), (2,3,2), and (5,4,2). (Note that (4,5,2), (5,4,1), and (4,5,1) do not fit the definition of a parallel-5.)


Comments

There are no comments at the moment.