티스토리 뷰
10999번: 구간 합 구하기 2
첫째 줄에 수의 개수 N(1 ≤ N ≤ 1,000,000)과 M(1 ≤ M ≤ 10,000), K(1 ≤ K ≤ 10,000) 가 주어진다. M은 수의 변경이 일어나는 횟수이고, K는 구간의 합을 구하는 횟수이다. 그리고 둘째 줄부터 N+1번째 줄
www.acmicpc.net
백준 소스코드 [C++] 10999 구간 합 구하기 2
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
typedef long long ll;
using namespace std;
int n, m, k;
vector<ll> tree, arr,lazy;
void input() {
cin >> n >> m >> k;
arr.resize(n);
tree.resize(4 * n);
lazy.resize(4 * n);
for (int i = 0;i < n;i++) cin >> arr[i];
}
ll init(int i, int S, int E) {
if (S == E) return tree[i] = arr[S];
else return tree[i] = init(2 * i, S, (S + E) / 2)
+ init(2 * i + 1, (S + E) / 2 + 1, E);
}
void update_lazy(int i, int S, int E) {
if (lazy[i] != 0) {
tree[i] += (E - S + 1) * lazy[i];
if (S != E) {
lazy[2 * i] += lazy[i];
lazy[2 * i + 1] += lazy[i];
}
lazy[i] = 0;
}
}
void update_range(int i, int S, int E, int L,int R, ll val) {
update_lazy(i, S, E);
if (E < L || R < S) return;
if (L <= S && E <= R) {
tree[i] += (E - S + 1) * val;
if (S != E) {
lazy[2 * i] += val;
lazy[2 * i + 1] += val;
}
return;
}
update_range(2 * i, S, (S + E) / 2, L, R,val);
update_range(2 * i + 1, (S + E) / 2 + 1, E, L, R,val);
tree[i] = tree[2 * i] + tree[2 * i + 1];
}
ll query(int i, int S, int E, int L, int R) {
update_lazy(i, S, E);
if (E < L || R < S) return 0;
if (L <= S && E <= R) return tree[i];
return query(2 * i, S, (S + E) / 2, L, R)
+ query(2 * i + 1, (S + E) / 2 + 1, E, L, R);
}
void output() {
int a, b, c, d;
for (int i = 0;i < m + k;i++) {
cin >> a;
if (a == 1) {
cin >> b >> c >> d;
update_range(1, 0, n - 1, b - 1, c - 1, d);
}
else {
cin >> b >> c;
cout << query(1, 0, n - 1, b - 1, c - 1) << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
input();
init(1, 0, n - 1);
output();
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 1260 DFS와 BFS (0) | 2020.11.13 |
---|---|
백준 소스코드 [C++] 16975 수열과 쿼리 21 (0) | 2020.11.12 |
백준 소스코드 [C++] 18436 수열과 쿼리 37 (0) | 2020.11.11 |
백준 소스코드 [C++] 11758 CCW (0) | 2020.11.11 |
백준 소스코드 [C++] 1275 커피숍 2 (0) | 2020.11.11 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- IOS
- MeTal
- WWDC21
- 컴퓨터 추상화
- 네트워크 플로우
- HIG
- Testable
- 강한 순환 참조
- CompositionalLayout
- test coverage
- 최단경로 알고리즘
- observeOn
- 에드몬드 카프 알고리즘
- WWDC16
- WWDC17
- 네트워크 유량
- rxswift
- 부스트캠프 6기
- CPU와 Memory
- 최단경로문제
- 코딩대회
- WWDC19
- mach-o
- 최대 매칭
- 벨만포드 시간복잡도
- 포드 풀커슨 알고리즘
- 다익스트라 시간복잡도
- 벨만포드 알고리즘
- State Restoration
- 최단경로 문제
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함