Henu
개발냥발
Henu
전체 방문자
오늘
어제
  • 분류 전체보기 (411)
    • DevOps (52)
      • Kubernetes (19)
      • Docker (14)
      • AWS (3)
      • Nginx (4)
      • Linux (4)
      • ArgoCD (1)
      • CN (2)
      • NATS (0)
      • Git (5)
    • Back-End (30)
      • Django (18)
      • Spring (5)
      • JPA (1)
      • MSA (5)
    • CS (87)
      • SystemSoftware (20)
      • OS (25)
      • Computer Architecture (16)
      • Network (23)
      • Database (2)
    • Lang (21)
      • Java (9)
      • Python (4)
      • C# (8)
    • Life (12)
    • 블록체인 (2)
    • Algorithm (204)
      • BOJ (160)
      • 프로그래머스 (19)
      • LeetCode (4)
      • SWEA (1)
      • 알고리즘 문제 해결 전략 (8)
      • DS, algorithms (7)
      • Checkio (5)
    • IT (2)

블로그 메뉴

  • GitHub
  • 글쓰기
  • 관리자

공지사항

  • Free!

인기 글

태그

  • 백트래킹
  • Kubernetes
  • 프로그래머스
  • docker
  • BFS
  • Network
  • 다이나믹 프로그래밍
  • django
  • DFS
  • boj

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Henu

개발냥발

[백준 1275] 커피숍2 (C++)
Algorithm/BOJ

[백준 1275] 커피숍2 (C++)

2021. 7. 31. 16:54
 

1275번: 커피숍2

첫째 줄에 수의 개수 N과 턴의 개수 Q가 주어진다.(1 ≤ N, Q ≤ 100,000) 둘째 줄에는 처음 배열에 들어가 있는 정수 N개가 주어진다. 세 번째 줄에서 Q+2번째 줄까지는 x y a b의 형식으로 x~y까지의 합

www.acmicpc.net

세그먼트 트리


 

 

[백준 11505] 구간 곱 구하기 (C++)

11505번: 구간 곱 구하기 첫째 줄에 수의 개수 N(1 ≤ N ≤ 1,000,000)과 M(1 ≤ M ≤ 10,000), K(1 ≤ K ≤ 10,000) 가 주어진다. M은 수의 변경이 일어나는 횟수이고, K는 구간의 곱을 구하는 횟수이다. 그리고.

hyeo-noo.tistory.com

위 문제와 똑같은 문제이다.

 

a번째수를 b로 바꾸어야 하므로 a번째 수를 포함하고 있는 tree[node]값들을 모두 바꾸어야 한다.

따라서 update를 할 때 리프노드(a번째 수)에서부터 올라오면서 업데이트를 해준다.

 

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
#define fasti ios_base::sync_with_stdio(false); cin.tie(0);
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 1e9+7
#define pii pair<int, int>
 
typedef long long ll;
// typedef pair<int, int> pii;
 
using namespace std;
 
int N, Q;
ll arr[100001];
ll tree[400004];
 
void input(){
    cin >> N >> Q;
    for(int i = 1; i <= N; i++){
        cin >> arr[i];
    }
}
 
ll make_tree(int startidx, int endidx, int node){
    if(startidx == endidx) return tree[node] = arr[startidx];
    int mid = (startidx + endidx) / 2;
    return tree[node] = make_tree(startidx, mid, node*2) + \
                        make_tree(mid+1, endidx, node*2+1);
}
 
ll getquery(int startidx, int endidx, int targetlidx, int targetridx, int node){
    if(targetridx < startidx || targetlidx > endidx) return 0;
    if(targetlidx <= startidx && endidx <= targetridx) return tree[node];
    int mid = (startidx + endidx) / 2;
    return getquery(startidx, mid, targetlidx, targetridx, node*2) + \
           getquery(mid+1, endidx, targetlidx, targetridx, node*2+1);
}
 
ll update(int startidx, int endidx, int targetidx, ll up, int node){
    if(targetidx < startidx || targetidx > endidx) return tree[node];
    if(startidx == endidx) return tree[node] = up;
    int mid = (startidx + endidx) / 2;
    return tree[node] = update(startidx, mid, targetidx, up, node*2) + \
                        update(mid+1, endidx, targetidx, up, node*2+1);
}
 
void solve(){
    make_tree(1, N, 1);
    ll x, y, a, b;
    for(int i = 0; i < Q; i++){
        cin >> x >> y >> a >> b;
        if(x > y) swap(x, y);
        cout << getquery(1, N, x, y, 1) << "\n";
        update(1, N, a, b, 1);
    }
}
 
int main(){
    fastio
    input();
    solve();
    
    return 0;
}
 
Colored by Color Scripter
cs

'Algorithm > BOJ' 카테고리의 다른 글

[백준 10999] 구간 합 구하기 2 (C++)  (0) 2021.07.31
[백준 1701] Cubeditor (C++)  (0) 2021.07.31
[백준 9019] DSLR (C++)  (0) 2021.07.30
[백준 2188] 축사 배정 (C++)  (0) 2021.07.30
[백준 11505] 구간 곱 구하기 (C++)  (0) 2021.07.29
    'Algorithm/BOJ' 카테고리의 다른 글
    • [백준 10999] 구간 합 구하기 2 (C++)
    • [백준 1701] Cubeditor (C++)
    • [백준 9019] DSLR (C++)
    • [백준 2188] 축사 배정 (C++)

    티스토리툴바