[BOJ] 11659번 : 구간 합 구하기 4

2024. 8. 31. 16:34Algorithm

1. problem : 

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

 

 

2. solution 1:

#include <bits/stdc++.h>
using namespace std;
int n, m;
int nums[100005]; 
int idx_sum[100005]; 


int main(void) {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++) cin >> nums[i]; 
	idx_sum[1] = nums[1]; 
	for (int i = 2; i <= n; i++) {
		idx_sum[i] = idx_sum[i - 1] + nums[i];
	}
	while (m--) {
		int i, j;
		cin >> i >> j; 
		cout << idx_sum[j] - idx_sum[i-1] << '\n';
	}
}

 

3. solution 2: 

// Authored by : BaaaaaaaaaaarkingDog
// Co-authored by : -
// http://boj.kr/7df92c8710f24a799c1f862fc4cc9269
#include <bits/stdc++.h>
using namespace std;

int n, m;
int a[100004], d[100004];

int main(void){
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  d[0] = 0;
  for(int i = 1; i <= n; i++){
    cin >> a[i];
    d[i] = d[i-1] + a[i];
  }
  while(m--){
    int i, j;
    cin >> i >> j;
    cout << d[j] - d[i-1] << '\n';
  }
}

source code 출처 : https://github.com/encrypted-def/basic-algo-lecture/blob/master/0x10/solutions/11659.cpp

 

basic-algo-lecture/0x10/solutions/11659.cpp at master · encrypted-def/basic-algo-lecture

바킹독의 실전 알고리즘 강의 자료. Contribute to encrypted-def/basic-algo-lecture development by creating an account on GitHub.

github.com

 

'Algorithm' 카테고리의 다른 글

[BOJ] 1003번 : 피보나치 함수  (2) 2024.09.01
[BOJ] 12852번 : 1로 만들기 2  (0) 2024.08.31
[BOJ] 11726번 : 2 x n 타일링  (0) 2024.08.31
[BOJ] 1149번 : RGB거리  (0) 2024.08.31
[BOJ] 9095번 : 1,2,3 더하기  (0) 2024.08.31