[BaekJoon] 11505 구간 곱 구하기

오태호·2023년 6월 5일
0

백준 알고리즘

목록 보기
240/395
post-thumbnail

1.  문제 링크

https://www.acmicpc.net/problem/11505

2.  문제


3.  소스코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    static final int DIVISOR = 1_000_000_007;
    static int N, M, K;
    static int[] nums;
    static int[][] commands;

    static void input() {
        Reader scanner = new Reader();

        N = scanner.nextInt();
        M = scanner.nextInt();
        K = scanner.nextInt();
        nums = new int[N];
        commands = new int[M + K][3];

        for(int idx = 0; idx < N; idx++) nums[idx] = scanner.nextInt();
        for(int idx = 0; idx < M + K; idx++) {
            int commandType = scanner.nextInt(), start = scanner.nextInt(), end = scanner.nextInt();

            commands[idx][0] = commandType;
            commands[idx][1] = start;
            commands[idx][2] = end;
        }
    }

    static void solution() {
        StringBuilder sb = new StringBuilder();
        int treeHeight = getTreeHeight(N, 2);
        int nodeCnt = Math.toIntExact(getTreeNodeCnt(treeHeight));
        int startIdx = (int)Math.pow(2, treeHeight - 1);
        int[] segmentTree = makeSegmentTree(nodeCnt, startIdx);

        for(int idx = 0; idx < M + K; idx++) {
            int[] command = commands[idx];
            if(command[0] == 1)
                changeNum(startIdx + (command[1] - 1), command[2], segmentTree);
            else
                sb.append(getMultiplyNums(startIdx + (command[1] - 1), startIdx + (command[2] - 1), segmentTree)).append('\n');
        }

        System.out.print(sb);
    }

    static void changeNum(int target, int newNum, int[] segmentTree) {
        segmentTree[target] = newNum;
        target = target / 2;

        while(target != 0) {
            segmentTree[target] = (int)((long)segmentTree[target * 2] * segmentTree[target * 2 + 1] % DIVISOR);
            target /= 2;
        }
    }

    static int getMultiplyNums(int start, int end, int[] segmentTree) {
        int multiply = 1;

        while(start <= end) {
            if(start % 2 == 1) multiply = (int)(((long)multiply * segmentTree[start]) % DIVISOR);
            if(end % 2 == 0) multiply = (int)(((long)multiply * segmentTree[end]) % DIVISOR);

            start = (start + 1) / 2;
            end = (end - 1) / 2;
        }

        return multiply;
    }

    static int[] makeSegmentTree(int nodeCnt, int startIdx) {
        int[] segmentTree = new int[nodeCnt];
        Arrays.fill(segmentTree, 1);

        for(int idx = 0; idx < N; idx++)
            segmentTree[startIdx + idx] = nums[idx];

        for(int idx = startIdx - 1; idx > 0; idx--)
            segmentTree[idx] = (int)((long)segmentTree[idx * 2] * segmentTree[idx * 2 + 1] % DIVISOR);

        return segmentTree;
    }

    static int getTreeHeight(int nodeNum, int base) {
        return (int)Math.ceil(Math.log(nodeNum) / Math.log(base)) + 1;
    }

    static long getTreeNodeCnt(int height) {
        return Math.round(Math.pow(2, height));
    }

    public static void main(String[] args) {
        input();
        solution();
    }

    static class Reader {
        BufferedReader br;
        StringTokenizer st;

        public Reader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while(st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
    }
}
profile
자바, 웹 개발을 열심히 공부하고 있습니다!

0개의 댓글