-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFenwick.cpp
More file actions
54 lines (52 loc) · 1.13 KB
/
Fenwick.cpp
File metadata and controls
54 lines (52 loc) · 1.13 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
struct DF{
int *fen, sz;
vector<int> all;
void pre(int x){
all.push_back(x);
}
void init(){
sort(all.begin(), all.end());
all.resize(unique(all.begin(), all.end()) - all.begin());
sz = all.size() + 1;
fen = new int[sz];
memset(fen, 0, sz * 4);
}
int hamid(int x){
x = lower_bound(all.begin(), all.end(), x) - all.begin();
int ans = 0;
for(; x; x ^= x & -x)
ans += fen[x];
return ans;
}
void majid(int x){
x = lower_bound(all.begin(), all.end(), x) - all.begin();
for(x++; x < sz; x += x & -x)
fen[x]++;
}
};
// Update : element, Get : range
int fen[maxn];
int get(int p){
int ans = 0;
for(; p; p ^= p & -p) ans += fen[p];
return ans;
}
int get(int l, int r){ // returns sum of range [l, r)
return get(r) - get(l);
}
void upd(int p, int v){ // adds v to p'th element
for(p++; p < maxn; p += p & -p) fen[p] += v;
}
// Update : range, Get : element
int fen[maxn];
int get(int p){
int ans = 0;
for(p++; p; p ^= p & -p) ans += fen[p];
return ans;
}
void upd(int p, int v){
for(p++; p < maxn; p += p & -p) fen[p] += v;
}
void upd(int l, int r, int v){ // adds v to range [l, r)
upd(l, v), upd(r, -v);
}