Dev.baelanche

[백준 11441] 합 구하기 본문

Data Structure & Algorithm/PS - JAVA

[백준 11441] 합 구하기

baelanche 2019. 6. 16. 18:11
반응형

 

11659번 문제와 완전히 유사한 구간합 문제이다.

 

public class Main {
    
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int n = Integer.parseInt(br.readLine());
        int a[] = new int[n];
        int psum[] = new int[n+1];
        
        StringTokenizer st = new StringTokenizer(br.readLine());
        for(int i=0; i<n; i++) {
            a[i] = Integer.parseInt(st.nextToken());
            psum[i+1] = psum[i] + a[i];
        }
        
        int m = Integer.parseInt(br.readLine());
        for(int i=0; i<m; i++) {
            st = new StringTokenizer(br.readLine());
            int s = Integer.parseInt(st.nextToken());
            int e = Integer.parseInt(st.nextToken());
            System.out.println(psum[e] - psum[s-1]);
        }
    }
}
반응형
Comments