Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class RMQ {
  4.  
  5.     private static long[] t;
  6.     private static int power;
  7.  
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader blackDiamond = new BufferedReader(new InputStreamReader(System.in));
  10.         int n = Integer.parseInt(blackDiamond.readLine());
  11.         long[] a = new long[n];
  12.         String[] values = blackDiamond.readLine().split(" ");
  13.         for (int i = 0; i < n; i++) {
  14.             a[i] = Long.parseLong(values[i]);
  15.         }
  16.         buildExtendedArray(a);
  17.         while (blackDiamond.ready()) {
  18.             String[] instructions = blackDiamond.readLine().split(" ");
  19.             switch (instructions[0]) {
  20.                 case "min":
  21.                     System.out.println(rmq(0,0, power - 1,
  22.                             Integer.parseInt(instructions[1]) - 1, Integer.parseInt(instructions[2]) - 1));
  23.                     break;
  24.                 case "set":
  25.                     set(Integer.parseInt(instructions[1]) - 1, Integer.parseInt(instructions[2]) - 1,
  26.                             Long.parseLong(instructions[3]));
  27.                     break;
  28.                 case "add":
  29.                     add(Integer.parseInt(instructions[1]) - 1, Integer.parseInt(instructions[2]) - 1,
  30.                             Long.parseLong(instructions[3]));
  31.                     break;
  32.             }
  33.         }
  34.     }
  35.  
  36.     private static void set(int index1, int index2, long value) {
  37.         for (int index = index1; index <= index2; index++) {
  38.             t[power + index - 1] = value;
  39.         }
  40.         int downward = (power + index2 - 2) / 2;
  41.         if (t.length > 1) {
  42.             while ((downward >= 0)) {
  43.                 t[downward] = min(t[2 * downward + 1], t[2 * downward + 2]);
  44.                 downward--;
  45.             }
  46.         }
  47.     }
  48.  
  49.     private static void add(int index1, int index2, long value) {
  50.         for (int index = index1; index <= index2; index++) {
  51.             t[power + index - 1] += value;
  52.         }
  53.         int downward = (power + index2 - 2) / 2;
  54.         if (t.length > 1) {
  55.             while ((downward >= 0)) {
  56.                 t[downward] = min(t[2 * downward + 1], t[2 * downward + 2]);
  57.                 downward--;
  58.             }
  59.         }
  60.     }
  61.  
  62.     private static long rmq(int index, int l, int r, int a, int b) {
  63.         if ((a > r) || (b < l)) {
  64.             return Long.MAX_VALUE;
  65.         } else if ((l >= a) && (r <= b)) {
  66.             return t[index];
  67.         }
  68.         int m = l + (r - l) / 2;
  69.         return min(rmq(2 * index + 1, l, m, a, b), rmq(2 * index + 2, m + 1, r, a, b));
  70.     }
  71.  
  72.     private static void buildExtendedArray(long[] a) {
  73.         power = 1;
  74.         while (power < a.length) {
  75.             power *= 2;
  76.         }
  77.         t = new long[2 * power - 1];
  78.         for (int i = 0; i < a.length; i++) {
  79.             t[power + i - 1] = a[i];
  80.         }
  81.         for (int i = a.length; i < power; i++) {
  82.             t[power + i - 1] = Long.MAX_VALUE;
  83.         }
  84.         int i = power - 2;
  85.         while (i > -1) {
  86.             t[i] = min(t[2 * i + 1], t[2 * i + 2]);
  87.             i--;
  88.         }
  89.     }
  90.  
  91.     private static long min(long a, long b) {
  92.         return a < b ? a : b;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement