https://www.acmicpc.net/problem/2042
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
#include<stdio.h>
#include<stack>
#include<cmath>
struct pos {
int front;
int end;
long long sum;
pos() :front(-1), end(-1), sum(0) {};
pos(int x, int y, long long sum) :front(x), end(y), sum(sum) {}
};
struct treegenstpos {
int front;
int end;
int arrpos;
};
long long sum(pos* tree,int arrpos,int b, int c) {
int t = 2 * (arrpos + 1);
if (tree[arrpos].end <= c && tree[arrpos].front >= b)
return tree[arrpos].sum;
if (tree[arrpos].end < b || tree[arrpos].front > c)
return 0;
return sum(tree, t, b, c)+ sum(tree, t-1, b, c);
}
int main(int argc, char* argv[]) {
int n, m, k, h, size;
long long* sumarr, *arr, te = 0;
pos* tree;
std::stack<treegenstpos> treegenst;
scanf("%d %d %d", &n, &m, &k);
h = (int)ceil(log2((double)n)) + 1;
size = pow(2, h) - 1;
tree = new pos[size];
sumarr = new long long[n];
arr = new long long[n];
for (int i = 0; i < n; i++) {
scanf("%lld", &arr[i]);
te = sumarr[i] = te + arr[i];
}
treegenst.push({ 0,n - 1,0 });
while (!treegenst.empty()) {
treegenstpos temp = treegenst.top();
treegenst.pop();
if (temp.front == 0)
tree[temp.arrpos] = { temp.front,temp.end,sumarr[temp.end] };
else
tree[temp.arrpos] = { temp.front,temp.end,sumarr[temp.end] - sumarr[temp.front - 1] };
if (temp.front != temp.end) {
treegenst.push({ temp.front,(temp.front + temp.end) / 2,2 * (temp.arrpos + 1) - 1 });
treegenst.push({ ((temp.front + temp.end) / 2) + 1,temp.end,2 * (temp.arrpos + 1) });
}
}
delete[] sumarr;
for (int tc = 0; tc < k+m; tc++) {
int a, b;
long long c;
treegenstpos treepos = { 0,0,0 };
scanf("%d %d %lld", &a, &b, &c);
if (a == 1) {
b--;
long long sub;
sub = c - arr[b];
arr[b] = c;
tree[treepos.arrpos].sum += sub;
do{
int t = 2 * (treepos.arrpos + 1);
if (size != 1) {
if (tree[t].front <= b) {
treepos.front = tree[t].front;
treepos.end = tree[t].end;
treepos.arrpos = t;
}
else if (tree[t - 1].end >= b) {
treepos.front = tree[t - 1].front;
treepos.end = tree[t - 1].end;
treepos.arrpos = t - 1;
}
tree[treepos.arrpos].sum += sub;
}
} while (treepos.front != treepos.end);
}
else {
long long result;
if (size != 0) {
result=sum(tree, 0, b - 1, c - 1);
}
else
result = arr[0];
printf("%lld\n", result);
}
}
delete[] tree;
delete[] arr;
return 0;
}
|
cs |
'기타 코딩 > PS' 카테고리의 다른 글
[백준] 숫자 카드 - 10815번 (0) | 2024.02.12 |
---|---|
[백준] 가장 큰 정사각형 - 1915번 (0) | 2020.10.22 |
[백준] 행렬 곱셈 순서 - 11049번 (0) | 2020.10.20 |
[백준] 테트로미노 - 14500번 (0) | 2020.10.20 |
[백준] 뱀 - 3190번 (0) | 2020.10.20 |