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 |
Tags
- 스택
- 백트래킹
- 시뮬레이션
- 다이나믹 프로그래밍
- 유니온 파인드
- Team Fortress 2
- DFS
- 우선순위 큐
- c++
- 문자열
- 구현
- 브루트포스
- 그리디 알고리즘
- 다익스트라
- 자료구조
- 트리
- 투 포인터
- 정렬
- 백준
- VR
- BFS
- 알고리즘
- 유니티
- 재귀
- 누적 합
- 수학
- ue5
- Unreal Engine 5
- XR Interaction Toolkit
- 그래프
Archives
- Today
- Total
1일1알
백준 1275번 커피숍2 C++ 본문
세그먼트 트리를 이용하여 문제를 해결하였다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
using namespace std;
using ll = long long;
ll MakeTree(int start, int end, int node, vector<ll>& v, vector<ll>& tree) {
if (start == end) return tree[node] = v[start];
ll mid = (start + end) / 2;
return tree[node] = MakeTree(start, mid, node * 2, v, tree) +
MakeTree(mid + 1, end, node * 2 + 1, v, tree);
}
ll GetSum(int start, int end, int node, int left, int right, vector<ll>& tree) {
if (left > end || right < start) return 0;
if (left <= start && end <= right) return tree[node];
ll mid = (start + end) / 2;
return GetSum(start, mid, node * 2, left, right, tree) +
GetSum(mid + 1, end, node * 2 + 1, left, right, tree);
}
void Update(int start, int end, int node, int index, ll val, vector<ll>& tree) {
if (index<start || index>end) return;
tree[node] += val;
if (start == end) return;
ll mid = (start + end) / 2;
Update(start, mid, node * 2, index, val, tree);
Update(mid + 1, end, node * 2 + 1, index, val, tree);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, q;
cin >> n >> q;
vector<ll> v(n);
vector<ll> tree(n * 4);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
MakeTree(0, n - 1, 1, v, tree);
for (int i = 0; i < q; i++) {
int x, y, a, b;
cin >> x >> y >> a >> b;
int start = min(x - 1, y - 1);
int end = max(x - 1, y - 1);
a -= 1;
ll change = b - v[a];
v[a] = b;
cout << GetSum(0, n - 1, 1, start, end, tree) << "\n";
Update(0, n - 1, 1, a, change, tree);
}
};
'알고리즘' 카테고리의 다른 글
백준 2374번 같은 수로 만들기 C++ (0) | 2022.02.09 |
---|---|
백준 14600번 샤워실 바닥 깔기 (Small) C++ (0) | 2022.02.08 |
백준 1802번 종이 접기 C++ (0) | 2022.02.06 |
백준 6576번 쿼드 트리 C++ (0) | 2022.02.06 |
백준 3678번 카탄의 개척자 C++ (0) | 2022.02.04 |