forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3.cpp
More file actions
62 lines (56 loc) ยท 1.67 KB
/
3.cpp
File metadata and controls
62 lines (56 loc) ยท 1.67 KB
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>
using namespace std;
// ์ ์ฒด ๋ฐ์ดํฐ์ ๊ฐ์๋ ์ต๋ 1,000,000๊ฐ
long long arr[1000001], tree[1000001];
// ๋ฐ์ดํฐ์ ๊ฐ์(n), ๋ณ๊ฒฝ ํ์(m), ๊ตฌ๊ฐ ํฉ ๊ณ์ฐ ํ์(k)
int n, m, k;
// i๋ฒ์งธ ์๊น์ง์ ๋์ ํฉ์ ๊ณ์ฐํ๋ ํจ์
long long prefixSum(int i) {
long long result = 0;
while(i > 0) {
result += tree[i];
// 0์ด ์๋ ๋ง์ง๋ง ๋นํธ๋งํผ ๋นผ๊ฐ๋ฉด์ ์ด๋
i -= (i & -i);
}
return result;
}
// i๋ฒ์งธ ์๋ฅผ dif๋งํผ ๋ํ๋ ํจ์
void update(int i, long long dif) {
while(i <= n) {
tree[i] += dif;
i += (i & -i);
}
}
// start๋ถํฐ end๊น์ง์ ๊ตฌ๊ฐ ํฉ์ ๊ณ์ฐํ๋ ํจ์
long long intervalSum(int start, int end) {
return prefixSum(end) - prefixSum(start - 1);
}
int main(void) {
scanf("%d %d %d", &n, &m, &k);
for(int i = 1; i <= n; i++) {
long long x;
scanf("%lld", &x);
arr[i] = x;
update(i, x);
}
int count = 0;
while(count++ < m + k) {
int op;
scanf("%d", &op);
// ์
๋ฐ์ดํธ(update) ์ฐ์ฐ์ธ ๊ฒฝ์ฐ
if(op == 1) {
int index;
long long value;
scanf("%d %lld", &index, &value);
update(index, value - arr[index]); // ๋ฐ๋ ํฌ๊ธฐ(dif)๋งํผ ์ ์ฉ
arr[index] = value; // i๋ฒ์งธ ์๋ฅผ value๋ก ์
๋ฐ์ดํธ
}
// ๊ตฌ๊ฐ ํฉ(interval sum) ์ฐ์ฐ์ธ ๊ฒฝ์ฐ
else {
int start, end;
scanf("%d %d", &start, &end);
printf("%lld\n", intervalSum(start, end));
}
}
return 0;
}