백준 11659번 구간합 구하기4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <queue>
#include<stack>
#include <cmath>
#include<string>
#include<vector>
#include<algorithm>
#include <cstring> //memset
using namespace std;
 
 
int n, m;
 
int arr[100001];
int total[100001];
int main(void){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 
    cin >> n >> m;
    total[0= 0;
    for (int i = 1; i <= n; i++) {
        cin >> arr[i];
        total[i] = total[i - 1+ arr[i];
    }
 
 
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        cout << total[b] - total[a - 1<< '\n';
    }
 
 
    return 0;
}
cs

문제 출처:https://www.acmicpc.net/problem/11659


간단한 부분합배열을 만들어서 푸는 문제였습니다.

  ios_base::sync_with_stdio(false);
    cin.tie(NULL);
 

이 코드를 추가 안하고 제출하니 자꾸 시간초과가 뜨네요. 

위 코드는 cin cout입출력 함수의 시간을 줄이는 효과가 있습니다.

+ Recent posts