File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-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+ public static void main(String[] args) throws IOException {
7+ BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
9+ int n = Integer.parseInt(br.readLine());
10+
11+ if (n == 0) {
12+ System.out.println(0);
13+ return;
14+ }
15+
16+ StringTokenizer st = new StringTokenizer(br.readLine());
17+ int currentStart = Integer.parseInt(st.nextToken());
18+ int currentEnd = Integer.parseInt(st.nextToken());
19+
20+ int totalLength = 0;
21+
22+ for (int i = 1; i < n; i++) {
23+ st = new StringTokenizer(br.readLine());
24+ int x = Integer.parseInt(st.nextToken());
25+ int y = Integer.parseInt(st.nextToken());
26+
27+ if (x <= currentEnd) {
28+ currentEnd = Math.max(currentEnd, y);
29+ } else {
30+ totalLength += (currentEnd - currentStart);
31+ currentStart = x;
32+ currentEnd = y;
33+ }
34+ }
35+
36+ totalLength += (currentEnd - currentStart);
37+
38+ System.out.println(totalLength);
39+ }
40+ }
41+ ```
You can’t perform that action at this time.
0 commit comments