Computer Science/알고리즘

백준 Java | BOJ 2042 구간 합 구하기

토마토. 2023. 2. 1. 14:51
package DAY03.P2042;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

public class Main {
    public static int N;
    public static int M;
    public static int K;
    public static long[] array;
    public static long[] sum;

    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("src/DAY03/P2042/input.txt"));
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] input = bf.readLine().split(" ");

        N = Integer.parseInt(input[0]);
        M = Integer.parseInt(input[1]);
        K = Integer.parseInt(input[2]);

        array = new long[N];
        for (int i=0;i<N;i++){
            array[i] = Long.parseLong(bf.readLine());
        }


        sum = new long[N];
        for (int i=0;i<N;i++){
            if (i==0){
                sum[i] = array[i];
            } else {
                sum[i] = (array[i] + sum[i-1]);
            }
        }

        for (int i=0;i<M+K;i++){
            String[] command = bf.readLine().split(" ");
            int a = Integer.parseInt(command[0]);
            int b = Integer.parseInt(command[1]);
            long c = Long.parseLong(command[2]);
            if (a==1){
                long delta = array[b-1];
                array[b-1] = c;
                for (int j=b-1;j<N;j++){
                    // change 누적합
                    sum[j] = sum[j] + c - delta;
                }
            } else {
                // get 누적합
                if (b==1){
                    System.out.println(sum[(int) (c-1)]);
                } else {
                    System.out.println(sum[(int) (c-1)]-sum[b-2]);
                }
            }
        }
    }
}