forked from spaghetti-source/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfenwick_tree.cc
More file actions
145 lines (133 loc) · 3.5 KB
/
fenwick_tree.cc
File metadata and controls
145 lines (133 loc) · 3.5 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// Fenwick Tree (aka. binary indexed tree)
//
// Description:
// A data structure that allows
// add(k,a): b[k] += a
// sum(k): b[0] + ... + b[k-1]
// lower_bound(a): min { k : sum(k) >= a }
//
// Algorithm:
// [ 1000 ]
// [ 100 ] [ ]
// [ 010 ] [ ] [ 110 ] [ ]
// [001] [ ] [011] [ ] [101] [ ] [111] [ ]
//
// - x[k] maintains the segment b[*,k).
// - k + (k & (k+1)) is the immediate ancestor of k
// - k - (k & (k-1)) is the rightmost left segment of k
//
// Complexity:
// O(log n) access, n space.
//
// Verified:
// SPOJ3266, SPOJ3267, SPOJ3377
#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
#define fst first
#define snd second
#define all(c) ((c).begin()), ((c).end())
template <class T>
struct fenwick_tree {
vector<T> x;
fenwick_tree(int n) : x(n+1) { }
void add(int k, T a) {
for (++k; k < x.size(); k += k&-k) x[k] += a;
}
T sum(int k) {
T s = 0;
for (; k > 0; k &= k-1) s += x[k];
return s;
}
int lower_bound(T a) {
if (a <= 0) return 0;
int k = x.size()-1;
for (int s: {1,2,4,8,16}) k |= (k >> s);
for (int p = ++k; p > 0; p >>= 1, k |= p)
if (k < x.size() && x[k] < a) a -= x[k]; else k ^= p;
return k+1;
}
};
namespace full {
template <class T>
struct fenwick_tree {
vector<T> x;
fenwick_tree(int n) : x(n+1) { }
// initialize by a constant
fenwick_tree(int n, T a) : x(n+1, a) {
x[0] = 0;
for (int k = 1; k+(k&-k) <= n; ++k) x[k+(k&-k)] += x[k];
}
// initialize by a vector
fenwick_tree(vector<T> y) : x(y.size()+1) {
for (int k = 0; k < y.size(); ++k) x[k+1] = y[k];
for (int k = 1; k+(k&-k) < x.size(); ++k) x[k+(k&-k)] += x[k];
}
// b[k] += a
void add(int k, T a) {
for (++k; k < x.size(); k += k&-k) x[k] += a;
}
// sum b[0,k)
T sum(int k) {
T s = 0;
for (; k > 0; k &= k-1) s += x[k];
return s;
}
// min { k : sum(k) >= a }; it requires b[k] >= 0
int lower_bound(T a) {
if (a <= 0) return 0;
int k = x.size()-1;
for (int s: {1,2,4,8,16}) k |= (k >> s);
for (int p = ++k; p > 0; p >>= 1, k |= p)
if (k < x.size() && x[k] < a) a -= x[k]; else k ^= p;
return k+1;
}
// max { k : sum(k) <= a }; it requires b[k] >= 0
int upper_bound(T a) {
int k = x.size()-1;
for (int s: {1,2,4,8,16}) k |= (k >> s);
for (int p = ++k; p > 0; p >>= 1, k |= p)
if (k < x.size() && x[k] <= a) a -= x[k]; else k ^= p;
return k;
}
};
} // full
int main() {
full::fenwick_tree<int> T(6, 1);
cout << "lower_bound" << endl;
cout << T.lower_bound(0) << endl;
cout << T.lower_bound(1) << endl;
cout << T.lower_bound(2) << endl;
cout << T.lower_bound(3) << endl;
cout << T.lower_bound(4) << endl;
cout << "upper_bound" << endl;
cout << T.upper_bound(0) << endl;
cout << T.upper_bound(1) << endl;
cout << T.upper_bound(2) << endl;
cout << T.upper_bound(3) << endl;
cout << T.upper_bound(4) << endl;
return 0;
int cases;
scanf("%d", &cases);
for (int icases = 0; icases < cases; ++icases) {
int n, u;
scanf("%d %d", &n, &u);
fenwick_tree<int> FT(n+1);
for (int k = 0; k < u; ++k) {
int i, j, a;
scanf("%d %d %d", &i, &j, &a);
FT.add(i, a);
FT.add(j+1, -a);
}
int q;
scanf("%d", &q);
for (int k = 0; k < q; ++k) {
int i;
scanf("%d", &i);
printf("%d\n", FT.sum(i));
}
}
}