SOLUTION INFO
C++ · main.cpp
- 작성자
- suin8
- 공동 작성자
- 없음
#include <bits/stdc++.h>
using namespace std;
int num[100010], sum[100010];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M; cin >> N >> M;
// 입력과 동시에 누적합을 구해놓습니다.
for(int i = 1;i <= N;i++) {
cin >> num[i];
sum[i] = sum[i - 1] + num[i];
}
// end까지의 합 - begin전 까지의 합 = begin ~ end사이 합
for(int i = 0;i < M;i++) {
int begin, end;
cin >> begin >> end;
cout << sum[end] - sum[begin - 1] << '\n';
}
return 0;
}
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.
SOLUTION INFO
Java · Main.java
- 작성자
- suin8
- 공동 작성자
- 없음
import java.util.*;
import java.io.*;
public class Main {
static int[] num = new int[100010];
static int[] sum = new int[100010];
public static void main(String[] args) {
FastReader rd = new FastReader();
int N = rd.nextInt();
int M = rd.nextInt();
// 1부터 i까지 합을 구해놓는다.
for(int i = 1;i <= N;i++) {
num[i] = rd.nextInt();
sum[i] = sum[i - 1] + num[i];
}
// end까지의 합에서 begin전까지의 합을 빼면 그 중간 값들의 합이 나온다.
// 매번 시행마다 더하면 시간초과
for(int i = 0;i < M;i++) {
int begin = rd.nextInt();
int end = rd.nextInt();
System.out.println(sum[end] - sum[begin - 1]);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.
SOLUTION INFO
Python · main.py
- 작성자
- cieske
- 공동 작성자
- 없음
import sys
def input():
return sys.stdin.readline().rstrip()
n, m = map(int, input().split())
lst = list(map(int, input().split()))
prefix_sum = [lst[0]]
for i in range(1, n): # 누적 합 구해두기
prefix_sum.append(prefix_sum[i-1] + lst[i])
for _ in range(m):
x, y = map(int, input().split())
x -= 1
if x: print(prefix_sum[y-1] - prefix_sum[x-1])
else: print(prefix_sum[y-1])
SOLUTION DESCRIPTION
풀이 설명
등록된 풀이 설명이 없습니다.