File tree Expand file tree Collapse file tree 1 file changed +64
-0
lines changed
Expand file tree Collapse file tree 1 file changed +64
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+
7+ static class line {
8+ int x, y;
9+ line (int x , int y ) {
10+ this . x = x;
11+ this . y = y;
12+ }
13+ }
14+
15+ public static void main (String [] args ) throws Exception {
16+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
17+ StringTokenizer st;
18+ int N = Integer . parseInt(br. readLine());
19+
20+ line[] arr = new line [N ];
21+
22+ for (int i = 0 ; i < N ; i++ ) {
23+ st = new StringTokenizer (br. readLine());
24+ int a = Integer . parseInt(st. nextToken());
25+ int b = Integer . parseInt(st. nextToken());
26+
27+ if (a > b) {
28+ int temp = a;
29+ a = b;
30+ b = temp;
31+ }
32+
33+ arr[i] = new line(a, b);
34+ }
35+
36+ Arrays . sort(arr, (o1, o2) - > {
37+ if (o1. x == o2. x) return o1. y - o2. y;
38+ return o1. x - o2. x;
39+ });
40+
41+ long answer = 0 ;
42+
43+ int curStart = arr[0 ]. x;
44+ int curEnd = arr[0 ]. y;
45+
46+ for (int i = 1 ; i < N ; i++ ) {
47+ int nx = arr[i]. x;
48+ int ny = arr[i]. y;
49+
50+ if (nx <= curEnd) {
51+ curEnd = Math . max(curEnd, ny);
52+ } else {
53+ answer += (curEnd - curStart);
54+ curStart = nx;
55+ curEnd = ny;
56+ }
57+ }
58+
59+ answer += (curEnd - curStart);
60+
61+ System . out. println(answer);
62+ }
63+ }
64+ ```
You can’t perform that action at this time.
0 commit comments