Codeforces Round 968 (Div. 2)
E1
Turtle and Inversions (Easy Version)
Codeforces
2600
原题
← D2E2 →
翻译中…

This is an easy version of this problem. The differences between the versions are the constraint on $m$ and $r_i \lt l_{i + 1}$ holds for each $i$ from $1$ to $m - 1$ in the easy version. You can make hacks only if both versions of the problem are solved.

翻译中…

Turtle gives you $m$ intervals $[l_1, r_1], [l_2, r_2], \ldots, [l_m, r_m]$. He thinks that a permutation $p$ is interesting if there exists an integer $k_i$ for every interval ($l_i \le k_i \lt r_i$), and if he lets $a_i = \max\limits_{j = l_i}^{k_i} p_j, b_i = \min\limits_{j = k_i + 1}^{r_i} p_j$ for every integer $i$ from $1$ to $m$, the following condition holds:

翻译中…

$$\max\limits_{i = 1}^m a_i \lt \min\limits_{i = 1}^m b_i$$

翻译中…

Turtle wants you to calculate the maximum number of inversions of all interesting permutations of length $n$, or tell him if there is no interesting permutation.

翻译中…

An inversion of a permutation $p$ is a pair of integers $(i, j)$ ($1 \le i \lt j \le n$) such that $p_i \gt p_j$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n, m$ ($2 \le n \le 5 \cdot 10^3, 0 \le m \le \frac{n}{2}$) — the length of the permutation and the number of intervals.

翻译中…

The $i$-th of the following $m$ lines contains two integers $l_i, r_i$ ($1 \le l_i \lt r_i \le n$) — the $i$-th interval.

翻译中…

Additional constraint on the input in this version: $r_i \lt l_{i + 1}$ holds for each $i$ from $1$ to $m - 1$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^3$.

翻译中…

Output

翻译中…

For each test case, if there is no interesting permutation, output a single integer $-1$.

翻译中…

Otherwise, output a single integer — the maximum number of inversions.

翻译中…

Note

翻译中…

In the third test case, the interesting permutation with the maximum number of inversions is $[5, 2, 4, 3, 1]$.

翻译中…

In the fourth test case, the interesting permutation with the maximum number of inversions is $[4, 8, 7, 6, 3, 2, 1, 5]$. In this case, we can let $[k_1, k_2] = [1, 7]$.

翻译中…

In the fifth test case, the interesting permutation with the maximum number of inversions is $[4, 7, 6, 3, 2, 1, 5]$.

翻译中…

In the sixth test case, the interesting permutation with the maximum number of inversions is $[4, 7, 3, 6, 2, 5, 1]$.

翻译中…

样例

Input #1
6
2 0
2 1
1 2
5 1
2 4
8 2
1 4
6 8
7 2
1 3
4 7
7 3
1 2
3 4
5 6
Output #1
1
0
8
21
15
15
InputCopy OutputCopy
题目信息
题号
2003E1
时间限制
2 s
内存限制
512 MB
标签
暴力
分治
动态规划
贪心
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。