※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 15244번 문제인 Debug이다.
문제는 아래 링크를 확인하자.

https://www.acmicpc.net/problem/15244 

 

15244번: Debug

Example 1 description: The procedure is called with arguments 1, 1, 2, 1. After that the array contains values {4, 3, 4, 3, 4, 3, 4, 3, 4, 3}. Sum of indices 2 to 6 (inclusive) is 4+3+4+3+4 = 18. Example 2 description: After the procedure calls, the array

www.acmicpc.net

각 procedure을 같은 X값끼리 묶어 값별로 한 번씩 시뮬레이션한 뒤(O(NlgN)), 누적합 배열을 만들어(O(N)) 각 쿼리를 처리해(쿼리당 O(1)) 문제를 해결하자.

 

아래는 제출한 소스코드이다.

#include <iostream>
using namespace std;
typedef long long ll;

int N, K, Q;
ll cnt[1000001];
ll psum[1000000];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> N >> K;
	while (K--) {
		int x; cin >> x;
		cnt[x]++;
	}

	for (int i = 1; i <= N; i++) {
		ll& curcnt = cnt[i];
		if (curcnt) {
			for (int k = 0; k < N; k += i) psum[k] += curcnt;
		}
	}

	for (int i = 1; i < N; i++) psum[i] += psum[i - 1];

	cin >> Q;
	while (Q--) {
		int L, R; cin >> L >> R;
		ll ans;
		if (L) cout << psum[R] - psum[L - 1] << '\n';
		else cout << psum[R] << '\n';
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 15245 // C++] Boom!  (0) 2023.05.15
[BOJ 1911 // C++] 흙길 보수하기  (0) 2023.05.14
[BOJ 15243 // C++] Tiling  (0) 2023.05.12
[BOJ 15506 // C++] 정원사  (1) 2023.05.11
[BOJ 9507 // C++] Generations of Tribbles  (0) 2023.05.10

+ Recent posts