Archive

BOJ. Minimum Value (10868)

Archived technical note: BOJ. Minimum Value (10868).

Problem: BOJ 10868 — Minimum Value · 한국어 · 日本語

Each query gives an inclusive 1-indexed range [a, b]; return its minimum. The iterative segment tree stores the N values in tree[N..2N). Internal nodes are built bottom-up: tree[i] is the minimum of its two children. This compact layout works even when N is not a power of two.

Convert a query to the zero-indexed half-open interval [a - 1, b), then shift both endpoints by N to address the leaves. While the left endpoint is before the right endpoint, consume tree[left] when left is a right child, and consume tree[right - 1] when the right endpoint is odd. Move both endpoints to their parents and repeat. These boundary checks select exactly the disjoint tree nodes covering the requested interval; initializing the answer to Integer.MAX_VALUE handles every valid query, including a single element. The loop runs in O(log N).

Building the tree takes O(N), and M queries take O(M log N), for O(N + M log N) total time. The tree stores 2N integers, so auxiliary storage is O(N).

Java

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Main {
    private static final class FastScanner {
        private final BufferedInputStream input =
                new BufferedInputStream(System.in);
        private final byte[] buffer = new byte[1 << 16];
        private int position;
        private int length;

        int nextInt() throws IOException {
            int c;
            do {
                c = read();
            } while (c <= ' ' && c != -1);

            int sign = 1;
            if (c == '-') {
                sign = -1;
                c = read();
            }
            int value = 0;
            while (c > ' ') {
                value = value * 10 + c - '0';
                c = read();
            }
            return sign * value;
        }

        private int read() throws IOException {
            if (position == length) {
                length = input.read(buffer);
                position = 0;
                if (length == -1) {
                    return -1;
                }
            }
            return buffer[position++];
        }
    }

    public static void main(String[] args) throws Exception {
        FastScanner input = new FastScanner();
        int n = input.nextInt();
        int m = input.nextInt();
        int[] tree = new int[2 * n];

        for (int i = 0; i < n; i++) {
            tree[n + i] = input.nextInt();
        }
        for (int i = n - 1; i > 0; i--) {
            tree[i] = Math.min(tree[i << 1], tree[i << 1 | 1]);
        }

        BufferedWriter output = new BufferedWriter(
                new OutputStreamWriter(System.out));
        while (m-- > 0) {
            int left = input.nextInt() - 1 + n;
            int right = input.nextInt() + n;
            int minimum = Integer.MAX_VALUE;

            while (left < right) {
                if ((left & 1) != 0) {
                    minimum = Math.min(minimum, tree[left++]);
                }
                if ((right & 1) != 0) {
                    minimum = Math.min(minimum, tree[--right]);
                }
                left >>= 1;
                right >>= 1;
            }

            output.write(Integer.toString(minimum));
            output.newLine();
        }
        output.flush();
    }
}