알고리즘

C++ 알고리즘 - 백준 11659 구간 합 구하기 4 (누적합 알고리즘)

마루설아 2025. 1. 6. 19:44

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

 

#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

int main(void) {
	/************** C++ Init **************/
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	/************** C++ Init **************/


	int input1, input2, input3, input4, input5;
	int sum = 0;
	vector<int> v;
	cin >> input1 >> input2;

	v.push_back(0);

	for (int i = 1; i <= input1; i++) {
		cin >> input3;
		v.push_back(input3 + v[i - 1]);
	}

	for (int i = 0; i < input2; i++) {
		cin >> input4 >> input5;
		cout << v[input5] - v[input4 - 1] << endl;
	}
}