-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathc.java
63 lines (61 loc) · 1.93 KB
/
c.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.io.*;
import java.util.*;
public class Program{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
static StringTokenizer st = new StringTokenizer("");
static int T, N, K, p[][];
static boolean f[];
static int hemi(int a1, int a2, int b){
if(a1 > a2){
int tmp = a1;
a1 = a2;
a2 = tmp;
}
if(b > a1 && b < a2) return 0;
return 1;
}
public static void main(String args[]) throws IOException{
T = nexti();
while(T-- > 0){
N = nexti();
K = nexti();
p = new int[K][2];
f = new boolean[2*N];
Arrays.fill(f, false);
for(int i = 0; i < K; i++){
p[i][0] = nexti() - 1;
p[i][1] = nexti() - 1;
f[p[i][0]] = true;
f[p[i][1]] = true;
}
int ans = 0;
int rem = N-K;
ans += rem * (rem-1) / 2;
for(int i = 0; i < K; i++){
for(int j = i+1; j < K; j++){
int h1 = hemi(p[i][0], p[i][1], p[j][0]);
int h2 = hemi(p[i][0], p[i][1], p[j][1]);
if(h1 != h2) ans++;
}
int v0 = 0, v1 = 0;
for(int j = 0; j < 2*N; j++){
if(f[j]) continue;
if(hemi(p[i][0], p[i][1], j) == 0) v0++;
else v1++;
}
int v = Math.min(v0, v1);
ans += v;
}
pw.println(ans);
}
br.close(); pw.close();
}
static String next() throws IOException{
while(!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
static int nexti() throws IOException{
return Integer.parseInt(next());
}
}