|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +
|
| 5 | +public class Main { |
| 6 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + private static TreeMap<Integer, Integer> buildings; |
| 9 | + private static List<Event> events; |
| 10 | + private static int N; |
| 11 | +
|
| 12 | + public static void main(String[] args) throws IOException { |
| 13 | + init(); |
| 14 | +
|
| 15 | + int height = 0; |
| 16 | + for (Event event : events) { |
| 17 | + if (event.flag) { |
| 18 | + buildings.put(event.height, buildings.getOrDefault(event.height, 0)+1); |
| 19 | + if (height < buildings.lastKey()) { |
| 20 | + height = buildings.lastKey(); |
| 21 | + bw.write(event.x + " " + height + " "); |
| 22 | + } |
| 23 | + } else { |
| 24 | + buildings.put(event.height, buildings.get(event.height)-1); |
| 25 | + if (buildings.get(event.height) == 0) buildings.remove(event.height); |
| 26 | + if (height > buildings.lastKey()) { |
| 27 | + height = buildings.lastKey(); |
| 28 | + bw.write(event.x + " " + height + " "); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | +
|
| 33 | + bw.flush(); |
| 34 | + bw.close(); |
| 35 | + br.close(); |
| 36 | + } |
| 37 | +
|
| 38 | + private static void init() throws IOException { |
| 39 | + N = Integer.parseInt(br.readLine()); |
| 40 | +
|
| 41 | + events = new ArrayList<>(); |
| 42 | + buildings = new TreeMap<>(); |
| 43 | + buildings.put(0, 1); |
| 44 | +
|
| 45 | + for (int i = 0; i < N; i++) { |
| 46 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 47 | + int l = Integer.parseInt(st.nextToken()); |
| 48 | + int h = Integer.parseInt(st.nextToken()); |
| 49 | + int r = Integer.parseInt(st.nextToken()); |
| 50 | +
|
| 51 | + events.add(new Event(l, h, true)); |
| 52 | + events.add(new Event(r, h, false)); |
| 53 | + } |
| 54 | +
|
| 55 | + events.sort((o1, o2) -> { |
| 56 | + if (o1.x != o2.x) return Integer.compare(o1.x, o2.x); |
| 57 | + |
| 58 | + if (o1.flag != o2.flag) { |
| 59 | + return o1.flag ? -1 : 1; |
| 60 | + } |
| 61 | + |
| 62 | + if (o1.flag) { |
| 63 | + return Integer.compare(o2.height, o1.height); |
| 64 | + } |
| 65 | + return Integer.compare(o1.height, o2.height); |
| 66 | + }); |
| 67 | + } |
| 68 | +
|
| 69 | + static class Event { |
| 70 | + int x; |
| 71 | + int height; |
| 72 | + boolean flag; |
| 73 | +
|
| 74 | + public Event(int x, int height, boolean flag) { |
| 75 | + this.x = x; |
| 76 | + this.height = height; |
| 77 | + this.flag = flag; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
0 commit comments