-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegmentTree2.cpp
77 lines (67 loc) · 1.28 KB
/
SegmentTree2.cpp
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
#include <iostream>
using namespace std;
int arSum(int ar[], int s, int e) {
int sum = 0;
for (int i = s; i < e; i++) sum += ar[i];
return sum;
}
class SegmentTree {
int *st;
int N;
void update(int ar[], int a, int b, int root) {
st[root] = arSum(ar, a, b);
if (a != b-1) {
update(ar, a, (a+b)/2, 2*root+1);
update(ar, (a+b)/2, b, 2*root+2);
}
}
public:
SegmentTree(int ar[], int n) {
st = new int[4*n];
N = n;
update(ar, 0, n, 0);
}
int getSum(int x) {
int a = 0, b = N, curr = 0;
int sum = 0;
while (true) {
if (x == b-1) {
sum += st[curr]; break;
}
if (a == b-1) break;
if (x < (a+b)/2) {
b = (a+b)/2;
curr = 2*curr + 1;
}
else {
sum += st[2*curr + 1];
a = (a+b)/2;
curr = 2*curr + 2;
}
}
return sum;
}
void changeElem(int x, int val) {
int del = val - (getSum(x) - getSum(x-1));
int a = 0, b = N, curr = 0;
while (true) {
st[curr] += del;
if (a == b-1) break;
if (x < (a+b)/2) {
b = (a+b)/2;
curr = 2*curr + 1;
}
else {
a = (a+b)/2;
curr = 2*curr + 2;
}
}
}
};
int main() {
int ar[6] = {5, 1, 7, 3, -4, 6};
SegmentTree s(ar, 6);
for (int i=0; i < 6; i++) cout << s.getSum(i) << endl;
s.changeElem(3, 0);
for (int i=0; i < 6; i++) cout << s.getSum(i) << endl;
}