|
| 1 | +```java |
| 2 | +import java.util.ArrayList; |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Comparator; |
| 5 | + |
| 6 | +class Main { |
| 7 | + |
| 8 | + |
| 9 | + static int N; |
| 10 | + static int size; |
| 11 | + static Node[] segments; |
| 12 | + static Point[] arr; |
| 13 | + static long D_MIN = Long.MIN_VALUE >> 1; |
| 14 | + static Node DEFAULT = new Node(D_MIN, D_MIN, D_MIN, 0); |
| 15 | + |
| 16 | + public static void main(String[] args) throws Exception { |
| 17 | + |
| 18 | + N = read(); |
| 19 | + |
| 20 | + ArrayList<Point> list = new ArrayList<>(); |
| 21 | + |
| 22 | + for(int i = 0; i < N; i++) { |
| 23 | + list.add(new Point (read(), read(), 1)); |
| 24 | + } |
| 25 | + |
| 26 | + int M = read(); |
| 27 | + |
| 28 | + for(int i = 0; i < M; i++) { |
| 29 | + list.add(new Point(read(), read(), -1)); |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + int S = read(); |
| 34 | + int E = read(); |
| 35 | + |
| 36 | + |
| 37 | + arr = list.toArray(new Point[0]); |
| 38 | + |
| 39 | + for(int i = 0; i < N; i++) arr[i].val *= S; |
| 40 | + for(int i = N; i < N + M; i++) arr[i].val *= E; |
| 41 | + |
| 42 | + |
| 43 | + N += M; |
| 44 | + |
| 45 | + |
| 46 | + // x좌표가 작은 순으로 정렬 |
| 47 | + Arrays.sort(arr, (o1, o2) -> { |
| 48 | + if(o1.x == o2.x) return o1.y - o2.y; |
| 49 | + return o1.x - o2.x; |
| 50 | + }); |
| 51 | + |
| 52 | + |
| 53 | + int cnt = 1; |
| 54 | + for(int i = 0; i < N; i++) { |
| 55 | + arr[i].index = cnt; |
| 56 | + while(i != N - 1 && arr[i].x == arr[i + 1].x) { |
| 57 | + arr[i + 1].index = cnt; |
| 58 | + i++; |
| 59 | + } |
| 60 | + cnt++; |
| 61 | + } |
| 62 | + |
| 63 | + size = 1; |
| 64 | + |
| 65 | + while(size < cnt) size <<= 1; |
| 66 | + |
| 67 | + Arrays.sort(arr, Comparator.comparingInt(o -> o.y)); |
| 68 | + |
| 69 | + |
| 70 | + long max = S; |
| 71 | + for(int i = 0; i < N; i++) { |
| 72 | + |
| 73 | + // y가 같으면 보지 않아야 됨 |
| 74 | + int start = i; |
| 75 | + while(i != N-1 && arr[i].y == arr[i + 1].y) { |
| 76 | + i++; |
| 77 | + } |
| 78 | + |
| 79 | + segments = new Node[(size << 1) + 1]; |
| 80 | + Arrays.fill(segments,DEFAULT); |
| 81 | + |
| 82 | + for(int j = start; j < N; j++) { |
| 83 | + update(arr[j].index, arr[j].val); |
| 84 | + while(j != N - 1 && arr[j].y == arr[j + 1].y) { |
| 85 | + update(arr[j + 1].index, arr[j + 1].val); |
| 86 | + j++; |
| 87 | + } |
| 88 | + // 업데이트 치고 max 찾고 |
| 89 | + max = Math.max(max, segments[2].max); |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + System.out.println(max); |
| 95 | + } |
| 96 | + |
| 97 | + static void update(int index, long val) { |
| 98 | + index += size; |
| 99 | + |
| 100 | + if(segments[index] == DEFAULT) { |
| 101 | + segments[index] = new Node(val, val, val, val); |
| 102 | + } else { |
| 103 | + segments[index].rmax += val; |
| 104 | + segments[index].lmax += val; |
| 105 | + segments[index].max += val; |
| 106 | + segments[index].sum += val; |
| 107 | + } |
| 108 | + |
| 109 | + index = (index + 1) >> 1; |
| 110 | + while(index >= 2) { |
| 111 | + segments[index] = combine(segments[(index << 1) - 1], segments[index << 1]); |
| 112 | + index = (index + 1) >> 1; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + static Node combine(Node lNode, Node rNode) { |
| 117 | + return new Node( |
| 118 | + Math.max(lNode.lmax, lNode.sum + rNode.lmax), |
| 119 | + Math.max(rNode.rmax, rNode.sum + lNode.rmax), |
| 120 | + Math.max(lNode.rmax + rNode.lmax, Math.max(lNode.max, rNode.max)), |
| 121 | + rNode.sum + lNode.sum |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + static class Node { |
| 126 | + long rmax; |
| 127 | + long lmax; |
| 128 | + long max; |
| 129 | + long sum; |
| 130 | + |
| 131 | + public Node(long lmax, long rmax,long max, long sum) { |
| 132 | + this.rmax = rmax; |
| 133 | + this.lmax = lmax; |
| 134 | + this.max = max; |
| 135 | + this.sum = sum; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + static class Point { |
| 140 | + int x; |
| 141 | + int y; |
| 142 | + int val; |
| 143 | + int index; |
| 144 | + |
| 145 | + public Point(int x, int y, int val) { |
| 146 | + this.x = x; |
| 147 | + this.y = y; |
| 148 | + this.val = val; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private static int read() throws Exception { |
| 153 | + int d, o; |
| 154 | + boolean negative = false; |
| 155 | + |
| 156 | + while (true) { |
| 157 | + d = System.in.read(); |
| 158 | + if (d == -1) return -1; // EOF |
| 159 | + if (!Character.isWhitespace(d)) break; |
| 160 | + } |
| 161 | + |
| 162 | + // 음수 처리 |
| 163 | + if (d == '-') { |
| 164 | + negative = true; |
| 165 | + d = System.in.read(); |
| 166 | + } |
| 167 | + |
| 168 | + o = d & 15; |
| 169 | + while (true) { |
| 170 | + d = System.in.read(); |
| 171 | + if (d == -1 || Character.isWhitespace(d)) break; |
| 172 | + o = (o << 3) + (o << 1) + (d & 15); |
| 173 | + } |
| 174 | + |
| 175 | + return negative ? -o : o; |
| 176 | + } |
| 177 | + |
| 178 | + |
| 179 | +} |
| 180 | +``` |
0 commit comments