|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static class taxi implements Comparable<taxi> { |
| 7 | + int start, end; |
| 8 | + taxi(int s, int e) { |
| 9 | + this.start = s; |
| 10 | + this.end = e; |
| 11 | + } |
| 12 | + public int compareTo(taxi o) { |
| 13 | + if (this.start != o.start) return Integer.compare(this.start, o.start); |
| 14 | + return Integer.compare(this.end, o.end); |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + public static void main(String[] args) throws IOException { |
| 19 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 20 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 21 | + int N = Integer.parseInt(st.nextToken()); |
| 22 | + int M = Integer.parseInt(st.nextToken()); |
| 23 | + |
| 24 | + List<taxi> list = new ArrayList<>(); |
| 25 | + for (int i = 0; i < N; i++) { |
| 26 | + st = new StringTokenizer(br.readLine()); |
| 27 | + int a = Integer.parseInt(st.nextToken()); |
| 28 | + int b = Integer.parseInt(st.nextToken()); |
| 29 | + if (a > b) { |
| 30 | + list.add(new taxi(b, a)); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + if (list.isEmpty()) { |
| 35 | + System.out.println(M); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + Collections.sort(list); |
| 40 | + |
| 41 | + long extra = 0; |
| 42 | + int curStart = list.get(0).start; |
| 43 | + int curEnd = list.get(0).end; |
| 44 | + |
| 45 | + for (int i = 1; i < list.size(); i++) { |
| 46 | + taxi t = list.get(i); |
| 47 | + if (t.start <= curEnd) { |
| 48 | + curEnd = Math.max(curEnd, t.end); |
| 49 | + } else { |
| 50 | + extra += (long)(curEnd - curStart) * 2; |
| 51 | + curStart = t.start; |
| 52 | + curEnd = t.end; |
| 53 | + } |
| 54 | + } |
| 55 | + extra += (long)(curEnd - curStart) * 2; |
| 56 | + |
| 57 | + long answer = M + extra; |
| 58 | + System.out.println(answer); |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
0 commit comments