[BOJ] 10999 구간 합 구하기 2

구간 합 구하기 2

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
#include <bits/stdc++.h>

#define MAX_N 1000001
#define ll long long
using namespace std;

ll n, m, k;
ll seg[MAX_N * 4], lazy[MAX_N * 4], A[MAX_N], psum[MAX_N];

void updateLazy(ll u, ll s, ll e) {
if(!lazy[u]) return;
seg[u] += lazy[u] * (e - s + 1);
if(s != e) {
lazy[u * 2] += lazy[u];
lazy[u * 2 + 1] += lazy[u];
}
lazy[u] = 0;
}

void update(ll u, ll s, ll e, ll l, ll r, ll v) {
updateLazy(u, s, e);
if(r < s or e < l) return;
if(l <= s and e <= r) {
seg[u] += v * (e - s + 1);
if(s != e) {
lazy[u * 2] += v;
lazy[u * 2 + 1] += v;
}
return;
}
ll m = (s + e) >> 1;
update(u * 2, s, m, l, r, v);
update(u * 2 + 1, m + 1, e, l, r, v);
seg[u] = seg[u * 2] + seg[u * 2 + 1];
}

ll query(ll u, ll s, ll e, ll l, ll r) {
updateLazy(u, s, e);
if(r < s or e < l) return 0ll;
if(l <= s and e <= r) return seg[u];
ll m = (s + e) >> 1;
return query(u * 2, s, m, l, r) + query(u * 2 + 1, m + 1, e, l, r);
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m>>k;
for(ll i = 1; i <= n; i++) cin>>A[i];
for(ll i = 1; i <= n; i++) psum[i] = psum[i-1] + A[i];

for(ll i = 0, op, s, e, v; i < m + k; i++) {
cin>>op>>s>>e;
if(op == 1) {
cin>>v;
update(1, 1, n, s, e, v);
} else {
cout<<query(1, 1, n, s, e) + psum[e] - psum[s-1]<<'\n';
}
}
return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/21/PS/BOJ/10999/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.