File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.util.* ;
3+ import java.io.* ;
4+
5+ public class Main {
6+ public static void main (String [] args ) throws IOException {
7+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
8+ int N = Integer . parseInt(br. readLine());
9+ StringTokenizer st;
10+
11+ int sum = 0 ;
12+ boolean first = true ;
13+ int left = 0 ;
14+ int right = 0 ;
15+
16+ for (int i = 0 ; i < N ; i++ ) {
17+ st = new StringTokenizer (br. readLine());
18+ int x = Integer . parseInt(st. nextToken());
19+ int y = Integer . parseInt(st. nextToken());
20+ if (first) {
21+ left = x;
22+ right = y;
23+ sum += y - x;
24+ first = false ;
25+ continue ;
26+ }
27+
28+ if (right <= x) {
29+ sum += y - x;
30+ left = x;
31+ right = y;
32+ } else {
33+ if (y < right) continue ;
34+ int tmp = y - right;
35+ sum += tmp;
36+ right = y;
37+ }
38+ }
39+ System . out. println(sum);
40+ br. close();
41+ }
42+ }
43+ ```
You can’t perform that action at this time.
0 commit comments