New Year and Ascent Sequence
Time limit2sMemory limit1024 MB
Given n sequences, count ordered pairs whose concatenation contains an increasing pair, using each sequence's own ascent and its min and max values.
- Level
Medium6 of 10
- Topics
- Sorting, Binary search, Implementation, Math
- Solved
- No attempts yet
Problem
A sequence of length has an ascent if there exists a pair of indices such that and . For example, the sequence has an ascent because of the pair , but the sequence does not have an ascent.
The concatenation of sequences and is the sequence obtained by writing down and one right after another without changing the order. For example, the concatenation of and is the sequence . The concatenation of sequences and is denoted .
Gyeonggeun thinks that sequences with ascents bring luck. For the new year he wants to make many such sequences. Gyeonggeun has sequences , which may have different lengths.
Gyeonggeun will consider all pairs of sequences and () and check whether the concatenation has an ascent. He may select the same sequence twice, and the order of selection matters.
Count the number of pairs of sequences whose concatenation has an ascent.
Input
The first line contains the number (), the number of sequences.
The next lines contain the number (), the length of , followed by integers () that form the sequence .
The sum of all does not exceed .
Output
Print a single integer, the number of pairs of sequences whose concatenation has an ascent.
Notes
For the first example, the following arrays have an ascent: . Arrays with the same contents are counted once per occurrence.