Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class RMQ {
- private static long[] t;
- private static int power;
- public static void main(String[] args) throws IOException {
- BufferedReader blackDiamond = new BufferedReader(new InputStreamReader(System.in));
- int n = Integer.parseInt(blackDiamond.readLine());
- long[] a = new long[n];
- String[] values = blackDiamond.readLine().split(" ");
- for (int i = 0; i < n; i++) {
- a[i] = Long.parseLong(values[i]);
- }
- buildExtendedArray(a);
- while (blackDiamond.ready()) {
- String[] instructions = blackDiamond.readLine().split(" ");
- switch (instructions[0]) {
- case "min":
- System.out.println(rmq(0,0, power - 1,
- Integer.parseInt(instructions[1]) - 1, Integer.parseInt(instructions[2]) - 1));
- break;
- case "set":
- set(Integer.parseInt(instructions[1]) - 1, Integer.parseInt(instructions[2]) - 1,
- Long.parseLong(instructions[3]));
- break;
- case "add":
- add(Integer.parseInt(instructions[1]) - 1, Integer.parseInt(instructions[2]) - 1,
- Long.parseLong(instructions[3]));
- break;
- }
- }
- }
- private static void set(int index1, int index2, long value) {
- for (int index = index1; index <= index2; index++) {
- t[power + index - 1] = value;
- }
- int downward = (power + index2 - 2) / 2;
- if (t.length > 1) {
- while ((downward >= 0)) {
- t[downward] = min(t[2 * downward + 1], t[2 * downward + 2]);
- downward--;
- }
- }
- }
- private static void add(int index1, int index2, long value) {
- for (int index = index1; index <= index2; index++) {
- t[power + index - 1] += value;
- }
- int downward = (power + index2 - 2) / 2;
- if (t.length > 1) {
- while ((downward >= 0)) {
- t[downward] = min(t[2 * downward + 1], t[2 * downward + 2]);
- downward--;
- }
- }
- }
- private static long rmq(int index, int l, int r, int a, int b) {
- if ((a > r) || (b < l)) {
- return Long.MAX_VALUE;
- } else if ((l >= a) && (r <= b)) {
- return t[index];
- }
- int m = l + (r - l) / 2;
- return min(rmq(2 * index + 1, l, m, a, b), rmq(2 * index + 2, m + 1, r, a, b));
- }
- private static void buildExtendedArray(long[] a) {
- power = 1;
- while (power < a.length) {
- power *= 2;
- }
- t = new long[2 * power - 1];
- for (int i = 0; i < a.length; i++) {
- t[power + i - 1] = a[i];
- }
- for (int i = a.length; i < power; i++) {
- t[power + i - 1] = Long.MAX_VALUE;
- }
- int i = power - 2;
- while (i > -1) {
- t[i] = min(t[2 * i + 1], t[2 * i + 2]);
- i--;
- }
- }
- private static long min(long a, long b) {
- return a < b ? a : b;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement