자바로 백준 11659 풀기

hong030·2023년 7월 1일
0

문제 풀이)

먼저 데이터 범위를 확인하면, 합을 구해야 하는 횟수는최대 10만이다. 구간마다 모두 합을 구하면 모든 구간 합 계산을 끝낼 수 없다.


import java.io.*;
import java.util.StringTokenizer;
public class Main{
	public static void main(String[]args)throws IOException {
		
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(bf.readLine());
		int D = Integer.parseInt(st.nextToken());
		int T = Integer.parseInt(st.nextToken());
		
		int sum[] = new int[D+1];
		sum[0] = 0;
		st = new StringTokenizer(bf.readLine());
		for(int i=1;i<D+1;i++) {
			sum[i] = sum[i-1] + Integer.parseInt(st.nextToken());
		}
		
		for(int i=0;i<T;i++) {
			st = new StringTokenizer(bf.readLine());
			int fir = Integer.parseInt(st.nextToken());
			int last = Integer.parseInt(st.nextToken());
			System.out.println(sum[last]-sum[fir-1]);
		}
	}
}

profile
자바 주력, 프론트 공부 중인 초보 개발자. / https://github.com/hongjaewonP

0개의 댓글