File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed
Expand file tree Collapse file tree 1 file changed +53
-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 implements Comparable<line> {
8+ int x, y;
9+ line (int x , int y ) { this . x = x; this . y = y; }
10+ @Override
11+ public int compareTo (line o ) {
12+ if (this . x != o. x) return Integer . compare(this . x, o. x);
13+ return Integer . compare(this . y, o. y);
14+ }
15+ }
16+
17+ public static void main (String [] args ) throws IOException {
18+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
19+
20+ int N = Integer . parseInt(br. readLine());
21+
22+ line[] arr = new line [N ];
23+ for (int i = 0 ; i < N ; i++ ) {
24+ StringTokenizer st = new StringTokenizer (br. readLine());
25+ int a = Integer . parseInt(st. nextToken());
26+ int b = Integer . parseInt(st. nextToken());
27+ arr[i] = new line(a, b);
28+ }
29+ Arrays . sort(arr);
30+
31+ long res = 0 ;
32+ int x = arr[0 ]. x;
33+ int y = arr[0 ]. y;
34+
35+ for (int i = 1 ; i < N ; i++ ) {
36+ int ns = arr[i]. x;
37+ int ne = arr[i]. y;
38+ if (ns <= y) {
39+ if (ne > y) {
40+ y = ne;
41+ }
42+ } else {
43+ res += (long )(y - x);
44+ x = ns;
45+ y = ne;
46+ }
47+ }
48+ res += (long )(y - x);
49+
50+ System . out. println(res);
51+ }
52+ }
53+ ```
You can’t perform that action at this time.
0 commit comments