๐Ÿ“– [๋ฐฑ์ค€์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด] Q.11659 ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ 4 ๋ฌธ์ œ ํ’€์ด

๐Ÿ“– ๋ฌธ์ œ

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

์ˆ˜ N๊ฐœ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, i๋ฒˆ์งธ ์ˆ˜๋ถ€ํ„ฐ j๋ฒˆ์งธ ์ˆ˜๊นŒ์ง€ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๐Ÿ” ์ ‘๊ทผ๋ฒ•

Q.11657 ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ4 ๋ฌธ์ œ ํ’€์ด์ž…๋‹ˆ๋‹ค.

๋ถ€๋ถ„ํ•ฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

1๋ฒˆ์งธ๋ถ€ํ„ฐ j๋ฒˆ์งธ๊นŒ์ง€์˜ ๋ˆ„์ ํ•ฉ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ 

j๋ฒˆ์งธ๊นŒ์ง€์˜ ๋ˆ„์ ํ•ฉ์—์„œ i-1๋ฒˆ์งธ๊นŒ์ง€์˜ ๋ˆ„์ ํ•ฉ์„ ๋นผ๋ฉด

i๋ฒˆ์งธ๋ถ€ํ„ฐ j๋ฒˆ์งธ๊นŒ์ง€์˜ ๋ถ€๋ถ„ ํ•ฉ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ’ป ์ฝ”๋“œ

package problem.sum;

import java.io.*;

public class Main_11659 {
    static int sum[];
    static int num[];
    static BufferedReader br;
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static int N = -1;
    static int M = -1;
    static StringBuilder sb;

    public static void main(String[] args) throws IOException {
        sb = new StringBuilder();
        br = new BufferedReader(new InputStreamReader(System.in));
        String input[] = br.readLine().split(" ");
        N = Integer.parseInt(input[0]);
        M = Integer.parseInt(input[1]);
        num = new int[N+1];
        sum = new int[N+1];
        String input2[] = br.readLine().split(" ");
        for(int i=1; i<=N; i++){
            num[i] = Integer.parseInt(input2[i-1]);
        }
        init();
        for(int a=0; a<M; a++){
            String input3[] = br.readLine().split(" ");
            int i = Integer.parseInt(input3[0]);
            int j = Integer.parseInt(input3[1]);
            solve(i,j);
        }
        bw.write(sb.toString());
        br.close();
        bw.flush();
        bw.close();
    }

    public static void init(){
        for(int i=1; i<=N; i++){
            sum[i] = sum[i-1]+num[i];
        }
    }

    public static void solve(int start, int end){
        int result = sum[end] - sum[start-1];
        sb.append(result+"\n");
    }
}



Written on January 5, 2021