|
| 1 | +#include <bits/stdc++.h> |
| 2 | + |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +// 전체 데이터의 개수는 최대 1,000,000개 |
| 6 | +long long arr[1000001], tree[1000001]; |
| 7 | +// 데이터의 개수(n), 변경 횟수(m), 구간 합 계산 횟수(k) |
| 8 | +int n, m, k; |
| 9 | + |
| 10 | +// i번째 수까지의 누적 합을 계산하는 함수 |
| 11 | +long long prefixSum(int i) { |
| 12 | + long long result = 0; |
| 13 | + while(i > 0) { |
| 14 | + result += tree[i]; |
| 15 | + // 0이 아닌 마지막 비트만큼 빼가면서 이동 |
| 16 | + i -= (i & -i); |
| 17 | + } |
| 18 | + return result; |
| 19 | +} |
| 20 | + |
| 21 | +// i번째 수를 dif만큼 더하는 함수 |
| 22 | +void update(int i, long long dif) { |
| 23 | + while(i <= n) { |
| 24 | + tree[i] += dif; |
| 25 | + i += (i & -i); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// start부터 end까지의 구간 합을 계산하는 함수 |
| 30 | +long long intervalSum(int start, int end) { |
| 31 | + return prefixSum(end) - prefixSum(start - 1); |
| 32 | +} |
| 33 | + |
| 34 | +int main(void) { |
| 35 | + scanf("%d %d %d", &n, &m, &k); |
| 36 | + for(int i = 1; i <= n; i++) { |
| 37 | + long long x; |
| 38 | + scanf("%lld", &x); |
| 39 | + arr[i] = x; |
| 40 | + update(i, x); |
| 41 | + } |
| 42 | + int count = 0; |
| 43 | + while(count++ < m + k) { |
| 44 | + int op; |
| 45 | + scanf("%d", &op); |
| 46 | + // 업데이트(update) 연산인 경우 |
| 47 | + if(op == 1) { |
| 48 | + int index; |
| 49 | + long long value; |
| 50 | + scanf("%d %lld", &index, &value); |
| 51 | + update(index, value - arr[index]); // 바뀐 크기(dif)만큼 적용 |
| 52 | + arr[index] = value; // i번째 수를 value로 업데이트 |
| 53 | + } |
| 54 | + // 구간 합(interval sum) 연산인 경우 |
| 55 | + else { |
| 56 | + int start, end; |
| 57 | + scanf("%d %d", &start, &end); |
| 58 | + printf("%lld\n", intervalSum(start, end)); |
| 59 | + } |
| 60 | + } |
| 61 | + return 0; |
| 62 | +} |
0 commit comments