01-09 04:37
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[BAEKJOON] ๋ฐฑ์ค€ ๋ˆ„์  ํ•ฉ 11659 :: ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ 4 ๋ณธ๋ฌธ

Algorithm/Baekjoon

[BAEKJOON] ๋ฐฑ์ค€ ๋ˆ„์  ํ•ฉ 11659 :: ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ 4

miinsun 2022. 4. 22. 18:08

 

๐Ÿ’ฌ  ๋ฌธ์ œ ์„ค๋ช…

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

 

๐Ÿ”จ  ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ 

  • ์ฒซ์งธ ์ค„์— ์ˆ˜์˜ ๊ฐœ์ˆ˜ N๊ณผ ํ•ฉ์„ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ํšŸ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค.
  • ๋‘˜์งธ ์ค„์—๋Š” N๊ฐœ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
  • ์ˆ˜๋Š” 1,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.
  • ์…‹์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์—๋Š” ํ•ฉ์„ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ๊ตฌ๊ฐ„ i์™€ j๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

 

์ถœ๋ ฅ

  • ์ด M๊ฐœ์˜ ์ค„์— ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ i๋ฒˆ์งธ ์ˆ˜๋ถ€ํ„ฐ j๋ฒˆ์งธ ์ˆ˜๊นŒ์ง€ ํ•ฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

์ œํ•œ

  • 1 ≤ N ≤ 100,000
  • 1 ≤ M ≤ 100,000
  • 1 ≤ i ≤ j ≤ N
  •  

 

์˜ˆ์ œ ์ž…๋ ฅ 1)

5 3
5 4 3 2 1
1 3
2 4
5 5

 

์˜ˆ์ œ ์ถœ๋ ฅ 1)

12
9
1

 

โ€‹

๐Ÿ’ป  Main.java

/* ๋ฐฑ์ค€ ๋ˆ„์  ํ•ฉ - 11659 :: ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ 4 */
import java.util.*;
import java.io.*;

public class Main{

    static int N;

    public static void main(String[] args)throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        
        long[] arr = new long[N + 1];
        long[] sum = new long[N + 1];
        st = new StringTokenizer(br.readLine());
        
        // 1๋ถ€ํ„ฐ N๊นŒ์ง€ ๋ˆ„์ ํ•ฉ ๊ตฌํ•˜๊ธฐ
        for(int i = 1; i <= N; i++) {
        	arr[i] = Long.parseLong(st.nextToken());
        	
        	if(i == 1) {
        		sum[i] = arr[i];
        	}
        	else {
        		sum[i] = sum[i - 1] + arr[i];
        	}
        }
        
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < M; i++) {
        	st = new StringTokenizer(br.readLine());
        	int s = Integer.parseInt(st.nextToken());
        	int e = Integer.parseInt(st.nextToken());
        	
        	if(s == 1)
        		sb.append(sum[e]).append('\n');
        	else
        		sb.append(sum[e] - sum[s - 1]).append('\n');
        }
        
        System.out.println(sb);
    }
}
Comments