|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | +public class JY_2170 { |
| 4 | + |
| 5 | + static class Pos implements Comparable<Pos> { |
| 6 | + int x, y; |
| 7 | + |
| 8 | + public Pos(int x, int y) { |
| 9 | + super(); |
| 10 | + this.x = x; |
| 11 | + this.y = y; |
| 12 | + } |
| 13 | + @Override |
| 14 | + public int compareTo(Pos other) { |
| 15 | + return this.x - other.x; |
| 16 | + } |
| 17 | + |
| 18 | + @Override |
| 19 | + public String toString() { |
| 20 | + return "Pos [x=" + x + ", y=" + y + "]"; |
| 21 | + } |
| 22 | + |
| 23 | + } |
| 24 | + |
| 25 | + public static void main(String[] args) throws IOException { |
| 26 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 27 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 28 | + |
| 29 | + int N = Integer.parseInt(st.nextToken()); |
| 30 | + |
| 31 | + List<Pos> pList = new ArrayList<>(); |
| 32 | + for(int i=0; i<N; i++) { |
| 33 | + st = new StringTokenizer(br.readLine()); |
| 34 | + int x = Integer.parseInt(st.nextToken()); |
| 35 | + int y = Integer.parseInt(st.nextToken()); |
| 36 | + |
| 37 | + pList.add(new Pos(x, y)); |
| 38 | + } |
| 39 | + |
| 40 | + // x기준으로 정렬 |
| 41 | + Collections.sort(pList); |
| 42 | + |
| 43 | + int s = pList.get(0).x; |
| 44 | + int e = pList.get(0).y; |
| 45 | + int ans = 0; |
| 46 | + for(int i=1; i<N; i++) { |
| 47 | + Pos now = pList.get(i); |
| 48 | + |
| 49 | + // now.x가 end보다 작다면 겹침 -> end 갱신 |
| 50 | + if(e >= now.x) { |
| 51 | + e = Math.max(e, now.y); |
| 52 | + } |
| 53 | + // 겹치지 않는 경우 |
| 54 | + else { |
| 55 | + // 기존것 더해주고 새로 업데이트 |
| 56 | + ans += (e - s); |
| 57 | + s = now.x; |
| 58 | + e = now.y; |
| 59 | + } |
| 60 | + } |
| 61 | + ans += (e - s); |
| 62 | + System.out.println(ans); |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments