|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +class Main { |
| 7 | + |
| 8 | + // IO field |
| 9 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + static StringTokenizer st = new StringTokenizer(""); |
| 12 | + |
| 13 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 14 | + static String nextToken() throws Exception { |
| 15 | + if(!st.hasMoreTokens()) nextLine(); |
| 16 | + return st.nextToken(); |
| 17 | + } |
| 18 | + static int nextInt() throws Exception { return Integer.parseInt(nextToken()); } |
| 19 | + static long nextLong() throws Exception { return Long.parseLong(nextToken()); } |
| 20 | + static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } |
| 21 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 22 | + |
| 23 | + // Additional field |
| 24 | + |
| 25 | + static int N; |
| 26 | + static PriorityQueue<int[]> Q = new PriorityQueue<>((a,b) -> { |
| 27 | + if(a[0] == b[0]) return b[1]-a[1]; |
| 28 | + return a[0]-b[0]; |
| 29 | + }); |
| 30 | + |
| 31 | + public static void main(String[] args) throws Exception { |
| 32 | + |
| 33 | + ready(); |
| 34 | + solve(); |
| 35 | + |
| 36 | + bwEnd(); |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + static void ready() throws Exception{ |
| 41 | + |
| 42 | + N = nextInt(); |
| 43 | + for(int i=0;i<N;i++) { |
| 44 | + int a = nextInt(), b = nextInt(); |
| 45 | + Q.offer(new int[] {a,1,i}); |
| 46 | + Q.offer(new int[] {b,-1,i}); |
| 47 | + } |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + static void solve() throws Exception{ |
| 52 | + |
| 53 | + int tot = 0; |
| 54 | + int[] len = new int[N]; |
| 55 | + int pos = 0; |
| 56 | + TreeSet<Integer> T = new TreeSet<>(); |
| 57 | + while(!Q.isEmpty()) { |
| 58 | + int prev = T.size(); |
| 59 | + if(prev == 1) len[T.first()] += Q.peek()[0]-pos; |
| 60 | + if(prev > 0) tot += Q.peek()[0]-pos; |
| 61 | + int now = Q.peek()[0]; |
| 62 | + while(!Q.isEmpty() && Q.peek()[0] == now) { |
| 63 | + int info = Q.peek()[1]; |
| 64 | + if(info == 1) T.add(Q.poll()[2]); |
| 65 | + else T.remove(Q.poll()[2]); |
| 66 | + } |
| 67 | + pos = now; |
| 68 | + } |
| 69 | + |
| 70 | + int ans = 0; |
| 71 | + for(int i=0;i<N;i++) ans = Math.max(ans, tot-len[i]); |
| 72 | + bw.write(ans + "\n"); |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +``` |
0 commit comments