Skip to content

Commit 833fb41

Browse files
authored
Merge pull request #390 from hitonanode/add-wavelet-matrix-test
add test of wavelet matrix
2 parents 787df6c + d4f86d3 commit 833fb41

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#define PROBLEM "https://judge.yosupo.jp/problem/static_range_sum_with_upper_bound"
2+
#include "../wavelet_matrix.hpp"
3+
4+
#include <iostream>
5+
using namespace std;
6+
7+
int main() {
8+
cin.tie(nullptr), ios::sync_with_stdio(false);
9+
10+
int N, Q;
11+
cin >> N >> Q;
12+
vector<int> A(N);
13+
for (auto &a : A) cin >> a;
14+
const wavelet_matrix<int> wm(A);
15+
16+
vector weights(wm.D(), vector<long long>(wm.N() + 1));
17+
for (int i = 0; i < N; ++i) {
18+
wm.index_apply(i, [&](int d, int idx) { weights[d][idx + 1] += A[i]; });
19+
}
20+
21+
for (auto &v : weights) {
22+
for (int i = 1; i < (int)v.size(); ++i) v[i] += v[i - 1];
23+
}
24+
25+
while (Q--) {
26+
int l, r, x;
27+
cin >> l >> r >> x;
28+
const int cnt = wm.range_freq(l, r, x + 1);
29+
30+
long long sum = 0;
31+
wm.prod(l, r, x + 1, [&](int d, int L, int R) { sum += weights[d][R] - weights[d][L]; });
32+
33+
cout << cnt << ' ' << sum << '\n';
34+
}
35+
}

data_structure/wavelet_matrix.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ cout << ans << endl;
7272
## 問題例
7373
7474
- [Library Checker: Point Add Rectangle Sum](https://judge.yosupo.jp/problem/point_add_rectangle_sum)
75+
- [Library Checker: Static Range Sum with Upper Bound](https://judge.yosupo.jp/problem/static_range_sum_with_upper_bound)
7576
- [yukicoder No.3207 Digital Font](https://yukicoder.me/problems/no/3207)
7677
7778
## 参考文献・リンク
7879
7980
- [ウェーブレット行列(wavelet matrix) - Eating Your Own Cat Food](https://miti-7.hatenablog.com/entry/2018/04/28/152259)
81+
- [Wavelet Matrix | Nyaan’s Library](https://nyaannyaan.github.io/library/data-structure-2d/wavelet-matrix.hpp.html)

0 commit comments

Comments
 (0)