knightnemo commited on
Commit
b89166b
·
verified ·
1 Parent(s): ffea3ed

Upload code_segments/segment_237.txt with huggingface_hub

Browse files
Files changed (1) hide show
  1. code_segments/segment_237.txt +27 -0
code_segments/segment_237.txt ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ This is an easy version of this problem. The differences between the versions are the constraint on $m$ and $r_i < 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.
2
+
3
+ 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 < 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:
4
+
5
+ $$\max\limits_{i = 1}^m a_i < \min\limits_{i = 1}^m b_i$$
6
+
7
+ 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.
8
+
9
+ An inversion of a permutation $p$ is a pair of integers $(i, j)$ ($1 \le i < j \le n$) such that $p_i > p_j$.
10
+
11
+ 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.
12
+
13
+ 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.
14
+
15
+ The $i$-th of the following $m$ lines contains two integers $l_i, r_i$ ($1 \le l_i < r_i \le n$) — the $i$-th interval.
16
+
17
+ Additional constraint on the input in this version: $r_i < l_{i + 1}$ holds for each $i$ from $1$ to $m - 1$.
18
+
19
+ It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^3$.
20
+
21
+ For each test case, if there is no interesting permutation, output a single integer $-1$.
22
+
23
+ Otherwise, output a single integer — the maximum number of inversions.
24
+
25
+ In the third test case, the interesting permutation with the maximum number of inversions is $[5, 2, 4, 3, 1]$.
26
+
27
+ 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