2024-04-24 21:40:00

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
 
 
public class Main {
    public static void main(String[] args) throws IOException 
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
        String[] input = br.readLine().split(" ");
 
        int N = Integer.parseInt(input[0]);
        int M = Integer.parseInt(input[1]);
        
        String[] num = br.readLine().split(" ");
        int[] nums = new int[N];
        for (int i =0; i < N; i++)
            nums[i] = Integer.parseInt(num[i]);
        
        int[] dp = new int[N+1];
        dp[0= 0;
        for (int i = 1; i < dp.length; i++)
            dp[i] = dp[i - 1+ nums[i - 1];
        
        for (int j = 0; j < M; j++)
        {
 
            
            String[] line = br.readLine().split(" ");
            int a = Integer.parseInt(line[0]);
            int b = Integer.parseInt(line[1]);
        
            
            System.out.println(dp[b] - dp[a - 1]);
        }
        
        
    }
}
 
cs

 

입력되는 값이 워낙 많아서 그런지 알고리즘 자체는 맞았는데, 계속 시간 초과가 났었다.

입출력의 시간을 줄여주는 방법을 사용해서 출ㄹ겨해 주었다.