import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.LinkedList; import java.util.List; import java.util.StringTokenizer; public class fio { private static final int DISTINCT_TREES = 999999999; private int N; private int[] substanceAmount; private int nReact; private Reaction[] reactions; private int nSeq; private Sequence[] sequence; private Tree tree; private int[] INORDER; private IntervalTree it; private List<Reaction>[] tReactions; private int[] mapInorder; public fio() { readData(); createTree(); tree.addDISTINCT_TREESNodes(); createINORDER(); createMapINORDER(); createIntervalTree(); createTReactions(); Writer.print(getSlime() + "\n"); } private void createMapINORDER() { mapInorder = new int[N]; for (int i=0; i<tree.size; ++i) if (INORDER[i] < N) mapInorder[INORDER[i]] = i; } private void readData() { this.N = Reader.nextInt(); this.nSeq = Reader.nextInt(); this.nReact = Reader.nextInt(); substanceAmount = new int[N]; for (int i = 0; i < N; ++i) substanceAmount[i] = Reader.nextInt(); sequence = new Sequence[nSeq]; for (int i = 0; i < nSeq; ++i) sequence[i] = new Sequence(Reader.nextInt()-1, Reader.nextInt()-1); reactions = new Reaction[nReact]; for (int i = 0; i < nReact; ++i) reactions[i] = new Reaction(Reader.nextInt()-1, Reader.nextInt()-1); } private void createTree() { tree = new Tree(N); for (int i = 0; i < nSeq; ++i) { int a = sequence[i].a; int b = sequence[i].b; tree.createParent(a, b); } } private void createINORDER() { INORDER = new int[tree.size]; createINORDER(tree.root, 0); } private int createINORDER(TreeNode root, int startIx) { if (root == null) return startIx; int leftIx = createINORDER(root.left, startIx); INORDER[leftIx] = root.name; return createINORDER(root.right, leftIx + 1); } private void createIntervalTree() { it = new IntervalTree(tree.size); for (int i=0; i<tree.size; ++i) it.update(i, INORDER[i]); } @SuppressWarnings("unchecked") private void createTReactions() { tReactions = new List[tree.size]; for (int i=0; i<tree.size; ++i) tReactions[i] = new LinkedList<Reaction>(); for (int i = 0; i < nReact; ++i) { int a = Math.min(reactions[i].a, reactions[i].b); int b = Math.max(reactions[i].a, reactions[i].b); a = mapInorder[a]; b = mapInorder[b]; int treeIx = it.getMax(a, b); if (treeIx != DISTINCT_TREES) tReactions[treeIx].add(reactions[i]); } } private long getSlime() { TreeNode[] nodes = tree.initial; long result = 0; for (Sequence s : sequence) { nodes[s.b] = nodes[s.b].parent; int n = nodes[s.b].name; for (Reaction r : tReactions[n]) { int min = Math.min(substanceAmount[r.a], substanceAmount[r.b]); substanceAmount[r.a] -= min; substanceAmount[r.b] -= min; result += 2 * min; } } return result; } /****************** INTERVAL TREE ******************/ private static class IntervalTree { private int[][] maxs; private int H; private int N; private IntervalTree(int N) { this.H = (int) (Math.log10(N - 1) / Math.log10(2)) + 2; H = Math.max(H, 1); this.N = 2*N;//(int) Math.pow(2, H); maxs = new int[H][this.N]; } private void update(int x, int v) { update(H - 1, x, v); } private void update(int y, int x, int v) { if (y < 0 || maxs[y][x] == v) return; maxs[y][x] = v; int x2 = (x & 1) == 0 ? x + 1 : x - 1; update(y - 1, x / 2, Math.max(v, maxs[y][x2])); } private int getMax(int _ixA, int _ixB) { int ixA = Math.min(_ixA, _ixB); int ixB = Math.max(_ixA, _ixB); int h = H-1; int max = maxs[h][ixA]; while (ixA < ixB) { if (isLeft(ixA) && isRight(ixB)) { h--; ixA/=2; ixB/=2; } else if (isLeft(ixA)) { max = Math.max(max, maxs[h][ixB]); h--; ixA /= 2; ixB = ixB / 2 - 1; } else if (isRight(ixB)) { max = Math.max(max, maxs[h][ixA]); h--; ixA = ixA / 2 + 1; ixB /= 2; } else { max = Math.max(max, maxs[h][ixA]); max = Math.max(max, maxs[h][ixB]); h--; ixA = ixA / 2 + 1; ixB = ixB / 2 - 1; } } if (ixA == ixB) max = Math.max(max, maxs[h][ixA]); return max; } private boolean isLeft(int x) { return (x & 1) == 0; } private boolean isRight(int x) { return (x & 1) == 1; } } /****************** TREE ******************/ private static class Tree { private TreeNode[] tmpNodes; private TreeNode[] initial; private TreeNode root; private int size; private Tree(int N) { tmpNodes = new TreeNode[N]; initial = new TreeNode[N]; for (int i = 0; i < N; ++i) initial[i] = tmpNodes[i] = new TreeNode(i); size = N; } public void addDISTINCT_TREESNodes() { TreeNode tmpRoot = null; int i = 0; for (; i < tmpNodes.length; ++i) if (tmpNodes[i] != null) break; tmpRoot = tmpNodes[i]; for (int j = i + 1; j < tmpNodes.length; ++j) if (tmpNodes[j] != null) { TreeNode parent = new TreeNode(DISTINCT_TREES); tmpRoot.parent = tmpNodes[j].parent = parent; parent.left = tmpRoot; parent.right = tmpNodes[j]; // allNodes[j] = null; tmpRoot = parent; size++; } root = tmpRoot; } public void createParent(int a, int b) { TreeNode parent = new TreeNode(size++); tmpNodes[a].parent = parent; tmpNodes[b].parent = parent; parent.left = tmpNodes[a]; parent.right = tmpNodes[b]; tmpNodes[a] = null; tmpNodes[b] = parent; } } private static class TreeNode { private int name = -1; private TreeNode parent, left, right; private TreeNode(int name) { this.name = name; } @Override public String toString(){ return ""+name; } } /****************** SEQUENCE ******************/ private static class Sequence { int a, b; public Sequence(int x, int y) { a = x; b = y; } } /****************** REACTION ******************/ private static class Reaction { int a, b; public Reaction(int x, int y) { a = Math.min(x, y); b = Math.max(x, y); } @Override public String toString(){ return "("+a+","+b+")"; } } /****************** MAIN ******************/ public static void main(String[] args) { Reader.init(System.in); new fio(); Writer.flush(); } // ========================= // based on @author: http://www.rgagnon.com/javadetails/java-0603.html static class Writer { private static BufferedWriter out; static { out = new BufferedWriter(new OutputStreamWriter(System.out), 512); } static void print(char c) { try { out.write(c); } catch (IOException e) { e.printStackTrace(); } } static void print(String str) { try { out.write(str); } catch (IOException e) { e.printStackTrace(); } } static void println() { try { out.write("\n"); } catch (IOException e) { e.printStackTrace(); } } static void flush() { try { out.flush(); } catch (IOException e) { e.printStackTrace(); } } @Override protected void finalize() { try { out.flush(); } catch (IOException e) { e.printStackTrace(); } } } // ========================= // based on @author: http://www.cpe.ku.ac.th/~jim/java-io.html static class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() { try { while (!tokenizer.hasMoreTokens()) { String str; str = reader.readLine(); if (str == null) return null; tokenizer = new StringTokenizer(str); } } catch (IOException e) { e.printStackTrace(); } return tokenizer.nextToken(); } static int nextInt() { try { return Integer.parseInt(next()); } catch (NumberFormatException e) { e.printStackTrace(); } return -1; } static double nextDouble() { try { return Double.parseDouble(next()); } catch (NumberFormatException e) { e.printStackTrace(); } return -1; } } // ========================= }
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.LinkedList; import java.util.List; import java.util.StringTokenizer; public class fio { private static final int DISTINCT_TREES = 999999999; private int N; private int[] substanceAmount; private int nReact; private Reaction[] reactions; private int nSeq; private Sequence[] sequence; private Tree tree; private int[] INORDER; private IntervalTree it; private List<Reaction>[] tReactions; private int[] mapInorder; public fio() { readData(); createTree(); tree.addDISTINCT_TREESNodes(); createINORDER(); createMapINORDER(); createIntervalTree(); createTReactions(); Writer.print(getSlime() + "\n"); } private void createMapINORDER() { mapInorder = new int[N]; for (int i=0; i<tree.size; ++i) if (INORDER[i] < N) mapInorder[INORDER[i]] = i; } private void readData() { this.N = Reader.nextInt(); this.nSeq = Reader.nextInt(); this.nReact = Reader.nextInt(); substanceAmount = new int[N]; for (int i = 0; i < N; ++i) substanceAmount[i] = Reader.nextInt(); sequence = new Sequence[nSeq]; for (int i = 0; i < nSeq; ++i) sequence[i] = new Sequence(Reader.nextInt()-1, Reader.nextInt()-1); reactions = new Reaction[nReact]; for (int i = 0; i < nReact; ++i) reactions[i] = new Reaction(Reader.nextInt()-1, Reader.nextInt()-1); } private void createTree() { tree = new Tree(N); for (int i = 0; i < nSeq; ++i) { int a = sequence[i].a; int b = sequence[i].b; tree.createParent(a, b); } } private void createINORDER() { INORDER = new int[tree.size]; createINORDER(tree.root, 0); } private int createINORDER(TreeNode root, int startIx) { if (root == null) return startIx; int leftIx = createINORDER(root.left, startIx); INORDER[leftIx] = root.name; return createINORDER(root.right, leftIx + 1); } private void createIntervalTree() { it = new IntervalTree(tree.size); for (int i=0; i<tree.size; ++i) it.update(i, INORDER[i]); } @SuppressWarnings("unchecked") private void createTReactions() { tReactions = new List[tree.size]; for (int i=0; i<tree.size; ++i) tReactions[i] = new LinkedList<Reaction>(); for (int i = 0; i < nReact; ++i) { int a = Math.min(reactions[i].a, reactions[i].b); int b = Math.max(reactions[i].a, reactions[i].b); a = mapInorder[a]; b = mapInorder[b]; int treeIx = it.getMax(a, b); if (treeIx != DISTINCT_TREES) tReactions[treeIx].add(reactions[i]); } } private long getSlime() { TreeNode[] nodes = tree.initial; long result = 0; for (Sequence s : sequence) { nodes[s.b] = nodes[s.b].parent; int n = nodes[s.b].name; for (Reaction r : tReactions[n]) { int min = Math.min(substanceAmount[r.a], substanceAmount[r.b]); substanceAmount[r.a] -= min; substanceAmount[r.b] -= min; result += 2 * min; } } return result; } /****************** INTERVAL TREE ******************/ private static class IntervalTree { private int[][] maxs; private int H; private int N; private IntervalTree(int N) { this.H = (int) (Math.log10(N - 1) / Math.log10(2)) + 2; H = Math.max(H, 1); this.N = 2*N;//(int) Math.pow(2, H); maxs = new int[H][this.N]; } private void update(int x, int v) { update(H - 1, x, v); } private void update(int y, int x, int v) { if (y < 0 || maxs[y][x] == v) return; maxs[y][x] = v; int x2 = (x & 1) == 0 ? x + 1 : x - 1; update(y - 1, x / 2, Math.max(v, maxs[y][x2])); } private int getMax(int _ixA, int _ixB) { int ixA = Math.min(_ixA, _ixB); int ixB = Math.max(_ixA, _ixB); int h = H-1; int max = maxs[h][ixA]; while (ixA < ixB) { if (isLeft(ixA) && isRight(ixB)) { h--; ixA/=2; ixB/=2; } else if (isLeft(ixA)) { max = Math.max(max, maxs[h][ixB]); h--; ixA /= 2; ixB = ixB / 2 - 1; } else if (isRight(ixB)) { max = Math.max(max, maxs[h][ixA]); h--; ixA = ixA / 2 + 1; ixB /= 2; } else { max = Math.max(max, maxs[h][ixA]); max = Math.max(max, maxs[h][ixB]); h--; ixA = ixA / 2 + 1; ixB = ixB / 2 - 1; } } if (ixA == ixB) max = Math.max(max, maxs[h][ixA]); return max; } private boolean isLeft(int x) { return (x & 1) == 0; } private boolean isRight(int x) { return (x & 1) == 1; } } /****************** TREE ******************/ private static class Tree { private TreeNode[] tmpNodes; private TreeNode[] initial; private TreeNode root; private int size; private Tree(int N) { tmpNodes = new TreeNode[N]; initial = new TreeNode[N]; for (int i = 0; i < N; ++i) initial[i] = tmpNodes[i] = new TreeNode(i); size = N; } public void addDISTINCT_TREESNodes() { TreeNode tmpRoot = null; int i = 0; for (; i < tmpNodes.length; ++i) if (tmpNodes[i] != null) break; tmpRoot = tmpNodes[i]; for (int j = i + 1; j < tmpNodes.length; ++j) if (tmpNodes[j] != null) { TreeNode parent = new TreeNode(DISTINCT_TREES); tmpRoot.parent = tmpNodes[j].parent = parent; parent.left = tmpRoot; parent.right = tmpNodes[j]; // allNodes[j] = null; tmpRoot = parent; size++; } root = tmpRoot; } public void createParent(int a, int b) { TreeNode parent = new TreeNode(size++); tmpNodes[a].parent = parent; tmpNodes[b].parent = parent; parent.left = tmpNodes[a]; parent.right = tmpNodes[b]; tmpNodes[a] = null; tmpNodes[b] = parent; } } private static class TreeNode { private int name = -1; private TreeNode parent, left, right; private TreeNode(int name) { this.name = name; } @Override public String toString(){ return ""+name; } } /****************** SEQUENCE ******************/ private static class Sequence { int a, b; public Sequence(int x, int y) { a = x; b = y; } } /****************** REACTION ******************/ private static class Reaction { int a, b; public Reaction(int x, int y) { a = Math.min(x, y); b = Math.max(x, y); } @Override public String toString(){ return "("+a+","+b+")"; } } /****************** MAIN ******************/ public static void main(String[] args) { Reader.init(System.in); new fio(); Writer.flush(); } // ========================= // based on @author: http://www.rgagnon.com/javadetails/java-0603.html static class Writer { private static BufferedWriter out; static { out = new BufferedWriter(new OutputStreamWriter(System.out), 512); } static void print(char c) { try { out.write(c); } catch (IOException e) { e.printStackTrace(); } } static void print(String str) { try { out.write(str); } catch (IOException e) { e.printStackTrace(); } } static void println() { try { out.write("\n"); } catch (IOException e) { e.printStackTrace(); } } static void flush() { try { out.flush(); } catch (IOException e) { e.printStackTrace(); } } @Override protected void finalize() { try { out.flush(); } catch (IOException e) { e.printStackTrace(); } } } // ========================= // based on @author: http://www.cpe.ku.ac.th/~jim/java-io.html static class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() { try { while (!tokenizer.hasMoreTokens()) { String str; str = reader.readLine(); if (str == null) return null; tokenizer = new StringTokenizer(str); } } catch (IOException e) { e.printStackTrace(); } return tokenizer.nextToken(); } static int nextInt() { try { return Integer.parseInt(next()); } catch (NumberFormatException e) { e.printStackTrace(); } return -1; } static double nextDouble() { try { return Double.parseDouble(next()); } catch (NumberFormatException e) { e.printStackTrace(); } return -1; } } // ========================= } |