Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- SQL
- geometry
- implement
- Number Theory
- array
- 코딩테스트
- 자바
- Matrix
- Tree
- string
- bit manipulation
- simulation
- sorting
- Binary Tree
- greedy
- Counting
- 코테
- database
- Stack
- Data Structure
- Class
- 구현
- two pointers
- java
- Method
- 파이썬
- Math
- Binary Search
- hash table
- dynamic programming
Archives
- Today
- Total
코린이의 소소한 공부노트
[백준 온라인 저지] 11659. 구간 합 구하기 4 본문
1. 입력
- 첫째 줄에 수의 개수 N과 합을 구해야 하는 횟수 M이 주어진다.
- 둘째 줄에는 N개의 수가 주어진다. 수는 1,000보다 작거나 같은 자연수이다.
- 셋째 줄부터 M개의 줄에는 합을 구해야 하는 구간 i와 j가 주어진다.
- 1 ≤ N ≤ 100,000
- 1 ≤ M ≤ 100,000
- 1 ≤ i ≤ j ≤ N
2. 출력
- 총 M개의 줄에 입력으로 주어진 i번째 수부터 j번째 수까지 합을 출력한다.
3. 예제
4. 코드
import java.util.*;
import java.io.*;
class Main {
static int n;
static int[] tree;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer token = new StringTokenizer(br.readLine());
n = Integer.valueOf(token.nextToken());
int m = Integer.valueOf(token.nextToken());
int[] nums = new int[n+1];
tree = new int[n+1];
token = new StringTokenizer(br.readLine());
for(int i=1 ; i<=n ; i++)
nums[i] = Integer.valueOf(token.nextToken());
for(int i=1 ; i<=n ; i++)
update(i, nums[i]);
for(int i=0 ; i<m ; i++){
token = new StringTokenizer(br.readLine());
bw.write(getPart(Integer.valueOf(token.nextToken()),Integer.valueOf(token.nextToken()))+"\n");
}
bw.flush(); bw.close();
}
static void update(int i, int dif) {
while(i<=n) {
tree[i] += dif;
i += (i&-i);
}
}
static int sum(int i) {
int ans = 0;
while(i>0) {
ans += tree[i];
i -= (i&-i);
}
return ans;
}
static int getPart(int a, int b) {
return sum(b) - sum(a-1);
}
}
- 60696KB, 692ms
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 35. Search Insert Position (0) | 2023.07.17 |
---|---|
[백준 온라인 저지] 1904. 01타일 (0) | 2023.07.14 |
[백준 온라인 저지] 20920. 영단어 암기는 괴로워 (0) | 2023.07.07 |
[백준 온라인 저지] 10814. 나이순 정렬 (0) | 2023.07.07 |
[백준 온라인 저지] 11651. 좌표 정렬하기 2 (0) | 2023.07.06 |