[백준 실버3] 11659 : 구간 합 구하기 4

수민이슈·2023년 10월 18일
0

[C++] 코딩테스트

목록 보기
97/116
post-thumbnail

🖊️ 문제

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


🖊️ 풀이

충격적..
문제가 굉장히 짧고, 이해가 쉽다.
그리고 엄청 쉽다...

DP인가? i부터 j까지의 합을 저장해놓아야하나? 했다.

근데 응 삽질

그냥 싹 다 0부터 n까지의 합으로 저장해두고,
start ~ end = start ~ end - 0 ~ start-1

이걸 이용하면 된다.

이게 실버3이나 된다니
이게 20분이나 걸리다니
충격적인..

#include <iostream>
using namespace std;

long long arr[100'001];

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

	int n, m;
	cin >> n >> m;

	arr[0] = 0;
	int input;
	for (int i = 1; i <= n; i++) {
		cin >> input;
		arr[i] = arr[i - 1] + input;
	}

	int start, end;
	long long result = 0;
	for (int i = 0; i < m; i++) {
		cin >> start >> end;
		cout << arr[end] - arr[start-1] << '\n';
	}
}

0개의 댓글