Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11660: 구간 합 구하기 5 #43

Open
minsoo0715 opened this issue Nov 21, 2023 · 0 comments
Open

11660: 구간 합 구하기 5 #43

minsoo0715 opened this issue Nov 21, 2023 · 0 comments
Assignees
Labels
#누적합 누적합 문제 @백준 백준(https://www.acmicpc.net) 문제 C/C++ c/c++로 해결한 문제들

Comments

@minsoo0715
Copy link
Owner

minsoo0715 commented Nov 21, 2023

11660: 구간 합 구하기 5

소스 코드

아이디어

0,0 ~ i, j 까지의 누적합은 다음과 같다.
s[i][j] = a[i][j] + s[i-1][j] + s[i][j-1] - s[i-1][j-1]

이때 (x1, y1) ~ (x2, y2) 사이의 구간 합은 다음과 같다.
s[x2][y2] - s[x1-1][y2] - s[x2][y1-1] + s[x1-1][y1-1]

구현

입력과 동시에 값을 누적.

for(int i = 1; i<=N; ++i) {
    for(int j = 1; j<=N; ++j) {
        cin >> s[i][j];
        s[i][j] += s[i-1][j] + s[i][j-1] - s[i-1][j-1];
    }
}

for(int m = 0; m<M; ++m) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    cout << s[x2][y2] - s[x1-1][y2] - s[x2][y1-1] + s[x1-1][y1-1] << endl;
}
@minsoo0715 minsoo0715 added @백준 백준(https://www.acmicpc.net) 문제 C/C++ c/c++로 해결한 문제들 작성예정 #누적합 누적합 문제 labels Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
#누적합 누적합 문제 @백준 백준(https://www.acmicpc.net) 문제 C/C++ c/c++로 해결한 문제들
Projects
None yet
Development

No branches or pull requests

1 participant