|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class BOJ34757 { |
| 6 | + |
| 7 | + static class IOManager { |
| 8 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 9 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 10 | + static StringTokenizer st = new StringTokenizer(""); |
| 11 | + |
| 12 | + private IOManager(){} |
| 13 | + |
| 14 | + static String nextLine() throws Exception { |
| 15 | + return br.readLine(); |
| 16 | + } |
| 17 | + |
| 18 | + static String nextToken() throws Exception { |
| 19 | + while (!st.hasMoreTokens()) { |
| 20 | + st = new StringTokenizer(nextLine()); |
| 21 | + } |
| 22 | + return st.nextToken(); |
| 23 | + } |
| 24 | + |
| 25 | + static int nextInt() throws Exception { |
| 26 | + return Integer.parseInt(nextToken()); |
| 27 | + } |
| 28 | + |
| 29 | + static long nextLong() throws Exception { |
| 30 | + return Long.parseLong(nextToken()); |
| 31 | + } |
| 32 | + |
| 33 | + static double nextDouble() throws Exception { |
| 34 | + return Double.parseDouble(nextToken()); |
| 35 | + } |
| 36 | + |
| 37 | + static void write(String content) throws Exception { |
| 38 | + bw.write(content); |
| 39 | + } |
| 40 | + |
| 41 | + public static void close() throws Exception { |
| 42 | + bw.flush(); |
| 43 | + bw.close(); |
| 44 | + br.close(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // |
| 49 | + |
| 50 | + static class Node { |
| 51 | + int left, right; |
| 52 | + long amount; |
| 53 | + Node(int left, int right, long amount) { |
| 54 | + this.left = left; |
| 55 | + this.right = right; |
| 56 | + this.amount = amount; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + static class Query extends Node implements Comparable<Query> { |
| 61 | + int lo, hi, mid, num; |
| 62 | + Query(int left, int right, long amount, int lo, int hi, int num) { |
| 63 | + super(left, right, amount); |
| 64 | + this.lo = lo; |
| 65 | + this.hi = hi; |
| 66 | + this.mid = (lo + hi) / 2; |
| 67 | + this.num = num; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public int compareTo(Query o) { |
| 72 | + if ((this.lo + this.hi) / 2 == (o.lo + o.hi) / 2) { |
| 73 | + return this.num - o.num; |
| 74 | + } |
| 75 | + return (this.lo + this.hi) / 2 - (o.lo + o.hi) / 2; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + static class DisjointSet { |
| 80 | + int[] root; |
| 81 | + |
| 82 | + DisjointSet() {} |
| 83 | + |
| 84 | + void init(int size) { |
| 85 | + root = new int[size + 2]; |
| 86 | + for(int i=1;i<=size + 1;i++) { |
| 87 | + root[i] = i; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + int find(int x) { |
| 92 | + return x == root[x] ? x : (root[x] = find(root[x])); |
| 93 | + } |
| 94 | + |
| 95 | + void union(int a, int b) { |
| 96 | + int x = find(a), y = find(b); |
| 97 | + if(x == y) { |
| 98 | + return; |
| 99 | + } |
| 100 | + root[x] = y; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + static class SegmentTree { |
| 105 | + long[] tree; |
| 106 | + |
| 107 | + SegmentTree() {} |
| 108 | + |
| 109 | + void init(int size) { |
| 110 | + tree = new long[size * 4]; |
| 111 | + } |
| 112 | + |
| 113 | + void update(int s, int e, int i, long v, int n) { |
| 114 | + if(s == e) { |
| 115 | + tree[n] += v; |
| 116 | + return; |
| 117 | + } |
| 118 | + int m = (s+e)>>1; |
| 119 | + if(i <= m) { |
| 120 | + update(s, m, i, v, n*2); |
| 121 | + } |
| 122 | + else { |
| 123 | + update(m+1, e, i, v, n*2+1); |
| 124 | + } |
| 125 | + tree[n] = tree[n*2] + tree[n*2+1]; |
| 126 | + } |
| 127 | + |
| 128 | + long rangeSum(int s, int e, int l, int r, int n) { |
| 129 | + if(l > r || l > e || r < s) { |
| 130 | + return 0; |
| 131 | + } |
| 132 | + if(l <= s && e <= r) { |
| 133 | + return tree[n]; |
| 134 | + } |
| 135 | + int m = (s+e)>>1; |
| 136 | + return rangeSum(s, m, l, r, n*2) + rangeSum(m+1, e, l, r, n*2+1); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + static int N, M, Q; |
| 141 | + static long[] snow; |
| 142 | + static Node[] works; |
| 143 | + static PriorityQueue<Query> queries; |
| 144 | + static SegmentTree seg = new SegmentTree(); |
| 145 | + static DisjointSet ds = new DisjointSet(); |
| 146 | + static int[] answer; |
| 147 | + |
| 148 | + public static void main(String[] args) throws Exception { |
| 149 | + N = IOManager.nextInt(); |
| 150 | + M = IOManager.nextInt(); |
| 151 | + Q = IOManager.nextInt(); |
| 152 | + |
| 153 | + snow = new long[N+2]; |
| 154 | + for(int i=1;i<=N;i++) { |
| 155 | + snow[i] = IOManager.nextLong(); |
| 156 | + } |
| 157 | + |
| 158 | + works = new Node[M]; |
| 159 | + for(int i=0;i<M;i++) { |
| 160 | + int left = IOManager.nextInt(); |
| 161 | + int right = IOManager.nextInt(); |
| 162 | + long amount = IOManager.nextLong(); |
| 163 | + works[i] = new Node(left, right, amount); |
| 164 | + } |
| 165 | + |
| 166 | + queries = new PriorityQueue<>(); |
| 167 | + for(int i=0;i<Q;i++) { |
| 168 | + int left = IOManager.nextInt(); |
| 169 | + int right = IOManager.nextInt(); |
| 170 | + long amount = IOManager.nextLong(); |
| 171 | + queries.add(new Query(left, right, amount, 0, M, i)); |
| 172 | + } |
| 173 | + |
| 174 | + answer = new int[Q]; |
| 175 | + while(!queries.isEmpty()) { |
| 176 | + seg.init(N); |
| 177 | + ds.init(N); |
| 178 | + PriorityQueue<Query> temp = new PriorityQueue<>(); |
| 179 | + for(int i=0;i<M;i++) { |
| 180 | + int left = works[i].left, right = works[i].right; |
| 181 | + long amount = works[i].amount; |
| 182 | + |
| 183 | + int x = ds.find(left); |
| 184 | + while(amount > 0 && x <= right) { |
| 185 | + long cur = seg.rangeSum(1, N, x, x, 1); |
| 186 | + long diff = Math.min(snow[x] - cur, amount); |
| 187 | + amount -= diff; |
| 188 | + seg.update(1, N, x, diff, 1); |
| 189 | + if (cur + diff == snow[x]) { |
| 190 | + ds.union(x, x + 1); |
| 191 | + x = ds.find(x + 1); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + while(!queries.isEmpty() && queries.peek().mid == i) { |
| 196 | + Query query = queries.poll(); |
| 197 | + long sum = seg.rangeSum(1, N, query.left, query.right, 1); |
| 198 | + if (sum >= query.amount) { |
| 199 | + query.hi = query.mid; |
| 200 | + } |
| 201 | + else { |
| 202 | + query.lo = query.mid + 1; |
| 203 | + } |
| 204 | + query.mid = (query.lo + query.hi) / 2; |
| 205 | + |
| 206 | + if(query.lo >= query.hi) { |
| 207 | + answer[query.num] = query.lo + 1; |
| 208 | + if(query.lo == M) { |
| 209 | + answer[query.num] = -1; |
| 210 | + } |
| 211 | + } |
| 212 | + else { |
| 213 | + if(query.mid != M) { |
| 214 | + temp.add(query); |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + queries = temp; |
| 220 | + } |
| 221 | + |
| 222 | + for(int i=0;i<Q;i++) { |
| 223 | + IOManager.write(answer[i] + "\n"); |
| 224 | + } |
| 225 | + |
| 226 | + IOManager.close(); |
| 227 | + } |
| 228 | + |
| 229 | +} |
| 230 | + |
| 231 | +``` |
0 commit comments