Skip to content

Commit 4928db8

Browse files
Shuffle
1 parent a2b07c2 commit 4928db8

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

1366/B/Main.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.Scanner;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
Scanner sc = new Scanner(System.in);
6+
7+
int t = sc.nextInt();
8+
for (int tc = 0; tc < t; ++tc) {
9+
int n = sc.nextInt();
10+
int x = sc.nextInt() - 1;
11+
int m = sc.nextInt();
12+
int[] l = new int[m];
13+
int[] r = new int[m];
14+
for (int i = 0; i < m; ++i) {
15+
l[i] = sc.nextInt() - 1;
16+
r[i] = sc.nextInt() - 1;
17+
}
18+
19+
System.out.println(solve(n, x, l, r));
20+
}
21+
22+
sc.close();
23+
}
24+
25+
static int solve(int n, int x, int[] l, int[] r) {
26+
int minIndex = x;
27+
int maxIndex = x;
28+
for (int i = 0; i < l.length; ++i) {
29+
if (!(l[i] > maxIndex || r[i] < minIndex)) {
30+
minIndex = Math.min(minIndex, l[i]);
31+
maxIndex = Math.max(maxIndex, r[i]);
32+
}
33+
}
34+
35+
return maxIndex - minIndex + 1;
36+
}
37+
}

0 commit comments

Comments
 (0)