File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments