Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions data_structure/test/wavelet_matrix_range_sum_upper_bound.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#define PROBLEM "https://judge.yosupo.jp/problem/static_range_sum_with_upper_bound"
#include "../wavelet_matrix.hpp"

#include <iostream>
using namespace std;

int main() {
cin.tie(nullptr), ios::sync_with_stdio(false);

int N, Q;
cin >> N >> Q;
vector<int> A(N);
for (auto &a : A) cin >> a;
const wavelet_matrix<int> wm(A);

vector weights(wm.D(), vector<long long>(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';
}
}
2 changes: 2 additions & 0 deletions data_structure/wavelet_matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)