-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_indexed_tree_test.cpp
89 lines (84 loc) · 1.81 KB
/
binary_indexed_tree_test.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
78
79
80
81
82
83
84
85
86
87
88
89
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
//Lowest Single Bit
#define LSB(i) ((i) & -(i))
struct FT
{
std::vector<ll> data;
FT(ll size)
{
data.resize(size+1);
}
void insert(ll idx, ll value)
{
while(idx < data.size())
{
data[idx] += value;
idx += LSB(idx);
}
}
ll rsq(ll from, ll to)
{
return getSum(to) - getSum(from-1);
}
ll getSum(ll elem)
{
ll value = 0;
while(elem != 0)
{
value += data[elem];
elem = elem & (elem - 1);
}
return value;
}
ll accessSingle(ll idx)
{
ll sum = data[idx];
if(idx > 0)
{
ll z = idx - (idx & -idx);
idx--;
while (idx != z)
{
sum -= data[idx];
idx -= (idx & -idx);
}
}
return sum;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll n,m;
cin >> n >> m;
ll inp[n];
struct FT tree(n);
struct FT freq(100001);
for(ll i = 0; i < n; i++) {
cin >> inp[i];
tree.insert(i+1,inp[i]);
freq.insert(inp[i], 1);
}
// for(ll i = 0; i < n; i++) {
// cout << freq.accessSingle(i+1) << " ";
// }
// cout << endl;
for(ll i = 0; i < m; i++) {
char a; ll b,c;
cin >> a;
if(a == 'C') {
cin >> b >> c;
freq.insert(c,1);
freq.insert(tree.accessSingle(b), -1);
tree.insert(b, c - tree.accessSingle(b));
} else if(a == 'S') {
cin >> b >> c;
cout << tree.rsq(b,c) << endl;
} else if(a == 'Q') {
cin >> b;
cout << freq.getSum(b) << endl;
}
}
}