Home BOJ. line Drawing (2170)
Post
Cancel

BOJ. line Drawing (2170)

[Link] https://www.acmicpc.net/problem/2170


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.*;
import java.io.*;

public class Main {
	static BufferedReader br;
	// static StringBuilder sb = new StringBuilder();
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		int n = toi(br.readLine());
		String[] line;
		ArrayList<Line> al = new ArrayList<>();
		for(int i = 0; i < n; i++) {
			line = br.readLine().split(" ");
			long a = Long.parseLong(line[0]), b = Long.parseLong(line[1]);
			al.add(new Line(a, b));
		}
		al.sort((l, r) -> {
			if(l.left == r.left) return l.right < r.right ? 1 : -1;
			else return l.left > r.left ? 1 : -1;
		});

		long sl = al.get(0).left, sr = al.get(0).right, totLen = sr - sl;
		for(Line ll : al) {
			if(sl <= ll.left && ll.right <= sr) continue;
			else if(sl == ll.left) {
				totLen += ll.right - sr;
			} else {
				if(sr <= ll.left) {
					totLen += ll.right - ll.left;
				} else totLen += ll.right - sr;
			}
			sl = ll.left;
			sr = ll.right;
		}

		print(totLen);
	}

	static class Line {
		long left, right;
		Line(long left, long right) { this.left = left; this.right = right; }
	}

	static int toi(String s) { return Integer.parseInt(s); }
	static long tol(String s) { return Long.parseLong(s); }
	static String[] getLine() throws IOException { return br.readLine().split(" "); }
	static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); }
	static <T> void print(T s) { System.out.print(s); }
	static <T> void println(T s) { System.out.println(s); }
}
This post is licensed under CC BY 4.0 by the author.