Korean English Japanese Chinese (Simplified) Chinese (Traditional)

 

 

 

 

 

11660번: 구간 합 구하기 5

첫째 줄에 표의 크기 N과 합을 구해야 하는 횟수 M이 주어진다. (1 ≤ N ≤ 1024, 1 ≤ M ≤ 100,000) 둘째 줄부터 N개의 줄에는 표에 채워져 있는 수가 1행부터 차례대로 주어진다. 다음 M개의 줄에는 네

www.acmicpc.net

 

 

 

 

난이도: 실버 1

풀이: 역시 구간합 array를 새로 지정해준 후 이후 연산을 구간합 array로 진행하면 훨씬 더 빠르게 연산을 수행할 수 있다. 

 

 

 

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
42
43
44
45
46
import java.util.*;
import java.io.*;
 
public class P11660 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        int A         = Integer.parseInt(st.nextToken());
        int B         = Integer.parseInt(st.nextToken());
        
        int arr[][]    = new int[A+1][A+1];
        for (int i = 1; i <= A; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 1; j <= A; j++) {
                arr[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        
        int arr2[][] = new int[A+1][A+1];
        for (int i = 1; i <= A; i++) {
            for (int j = 1; j <= A; j++) {
                arr2[i][j] = arr2[i-1][j] + arr2[i][j-1- arr2[i-1][j-1+ arr[i][j];
            }
        }
        
        
        int sum;
        int x1;
        int y1;
        int x2;
        int y2;
        
        for (int i = 1; i <= B; i++) {
            st        = new StringTokenizer(br.readLine());
            x1     = Integer.parseInt(st.nextToken());
            y1        = Integer.parseInt(st.nextToken());
            x2        = Integer.parseInt(st.nextToken());
            y2        = Integer.parseInt(st.nextToken());
            
            sum    = arr2[x2][y2] - arr2[x1-1][y2] - arr2[x2][y1-1+ arr2[x1-1][y1-1];
            System.out.println(sum);
        }
    }
}
 
cs
 

If you like this post, please give me a ❤️...!
 
✰Popular Posts✰
✰Recent Posts✰
 

❤ from Seoul, Daejeon, Tokyo, Fukuoka