From d4f86d3107a1846e14c2774543c41f5d81bf8566 Mon Sep 17 00:00:00 2001 From: hitonanode <32937551+hitonanode@users.noreply.github.com> Date: Sun, 10 Aug 2025 23:50:57 +0900 Subject: [PATCH] add test of wavelet matrix --- ...elet_matrix_range_sum_upper_bound.test.cpp | 35 +++++++++++++++++++ data_structure/wavelet_matrix.md | 2 ++ 2 files changed, 37 insertions(+) create mode 100644 data_structure/test/wavelet_matrix_range_sum_upper_bound.test.cpp diff --git a/data_structure/test/wavelet_matrix_range_sum_upper_bound.test.cpp b/data_structure/test/wavelet_matrix_range_sum_upper_bound.test.cpp new file mode 100644 index 00000000..ff41883a --- /dev/null +++ b/data_structure/test/wavelet_matrix_range_sum_upper_bound.test.cpp @@ -0,0 +1,35 @@ +#define PROBLEM "https://judge.yosupo.jp/problem/static_range_sum_with_upper_bound" +#include "../wavelet_matrix.hpp" + +#include +using namespace std; + +int main() { + cin.tie(nullptr), ios::sync_with_stdio(false); + + int N, Q; + cin >> N >> Q; + vector A(N); + for (auto &a : A) cin >> a; + const wavelet_matrix wm(A); + + vector weights(wm.D(), vector(wm.N() + 1)); + for (int i = 0; i < N; ++i) { + wm.index_apply(i, [&](int d, int idx) { weights[d][idx + 1] += A[i]; }); + } + + for (auto &v : weights) { + for (int i = 1; i < (int)v.size(); ++i) v[i] += v[i - 1]; + } + + while (Q--) { + int l, r, x; + cin >> l >> r >> x; + const int cnt = wm.range_freq(l, r, x + 1); + + long long sum = 0; + wm.prod(l, r, x + 1, [&](int d, int L, int R) { sum += weights[d][R] - weights[d][L]; }); + + cout << cnt << ' ' << sum << '\n'; + } +} diff --git a/data_structure/wavelet_matrix.md b/data_structure/wavelet_matrix.md index 7d0b4426..1338aa7d 100644 --- a/data_structure/wavelet_matrix.md +++ b/data_structure/wavelet_matrix.md @@ -72,8 +72,10 @@ cout << ans << endl; ## 問題例 - [Library Checker: Point Add Rectangle Sum](https://judge.yosupo.jp/problem/point_add_rectangle_sum) +- [Library Checker: Static Range Sum with Upper Bound](https://judge.yosupo.jp/problem/static_range_sum_with_upper_bound) - [yukicoder No.3207 Digital Font](https://yukicoder.me/problems/no/3207) ## 参考文献・リンク - [ウェーブレット行列(wavelet matrix) - Eating Your Own Cat Food](https://miti-7.hatenablog.com/entry/2018/04/28/152259) +- [Wavelet Matrix | Nyaan’s Library](https://nyaannyaan.github.io/library/data-structure-2d/wavelet-matrix.hpp.html)