Skip to content

Commit 40344cb

Browse files
lrvideckisweb-flow
andauthored
Kd bit (#184)
* first draft * add another test * add docs now * fix script * another fix * fix compile error * add more docs * [auto-verifier] verify commit 0470395 --------- Co-authored-by: GitHub <noreply@github.com>
1 parent 1ecdcda commit 40344cb

File tree

5 files changed

+105
-1
lines changed

5 files changed

+105
-1
lines changed

.verify-helper/timestamps.remote.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"tests/library_checker_aizu_tests/data_structures/dsu_restorable.test.cpp": "2026-01-18 02:20:40 +0000",
2424
"tests/library_checker_aizu_tests/data_structures/dsu_segtree_undo_trick.test.cpp": "2026-01-23 04:31:29 +0000",
2525
"tests/library_checker_aizu_tests/data_structures/implicit_seg_tree.test.cpp": "2026-01-18 11:15:41 +0000",
26+
"tests/library_checker_aizu_tests/data_structures/kd_bit_1d.test.cpp": "2026-02-19 11:51:03 -0700",
2627
"tests/library_checker_aizu_tests/data_structures/kruskal_tree_aizu.test.cpp": "2026-01-18 02:20:40 +0000",
2728
"tests/library_checker_aizu_tests/data_structures/kth_smallest_pst.test.cpp": "2026-01-18 11:15:41 +0000",
2829
"tests/library_checker_aizu_tests/data_structures/kth_smallest_wavelet_matrix.test.cpp": "2026-02-04 02:50:53 +0000",
@@ -76,6 +77,7 @@
7677
"tests/library_checker_aizu_tests/handmade_tests/fib_matrix_expo.test.cpp": "2026-01-28 21:48:16 -0700",
7778
"tests/library_checker_aizu_tests/handmade_tests/functional_graph.test.cpp": "2025-08-06 16:18:37 -0600",
7879
"tests/library_checker_aizu_tests/handmade_tests/hilbert_mos.test.cpp": "2026-01-18 02:20:40 +0000",
80+
"tests/library_checker_aizu_tests/handmade_tests/kd_bit.test.cpp": "2026-02-19 11:51:03 -0700",
7981
"tests/library_checker_aizu_tests/handmade_tests/manacher.test.cpp": "2026-01-18 11:15:41 +0000",
8082
"tests/library_checker_aizu_tests/handmade_tests/merge_st_and_wavelet.test.cpp": "2026-02-04 02:50:53 +0000",
8183
"tests/library_checker_aizu_tests/handmade_tests/mobius.test.cpp": "2025-02-10 14:50:36 -0700",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
//! https://github.com/ucf-programming-team/hackpack-cpp/blob/master/content/data-structures/KDBIT.h
3+
//! @code
4+
//! KD_BIT<2> bit(n, m);
5+
//! bit.update(i, j, 5);
6+
//! bit.query(i1, i2, j1, j2);
7+
//! // 0 <= i1 <= i2 <= n
8+
//! // 0 <= j1 <= j2 <= m
9+
//! @endcode
10+
//! @time O(n^k + q * log^k n)
11+
//! @space O(n^k)
12+
// NOLINTNEXTLINE(readability-identifier-naming)
13+
template<int K> struct KD_BIT;
14+
template<> struct KD_BIT<0> {
15+
ll s = 0;
16+
void update(ll v) { s += v; }
17+
ll query() { return s; }
18+
};
19+
template<int K> struct KD_BIT {
20+
vector<KD_BIT<K - 1>> s;
21+
template<class... A>
22+
KD_BIT(int n, A... a): s(n, KD_BIT<K - 1>(a...)) {}
23+
template<class... A> void update(int i, A... a) {
24+
for (; i < sz(s); i |= i + 1) s[i].update(a...);
25+
}
26+
template<class... A> ll query(int l, int r, A... a) {
27+
ll ans = 0;
28+
for (; l < r; r &= r - 1) ans += s[r - 1].query(a...);
29+
for (; r < l; l &= l - 1) ans -= s[l - 1].query(a...);
30+
return ans;
31+
}
32+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#define PROBLEM \
2+
"https://judge.yosupo.jp/problem/point_add_range_sum"
3+
#include "../template.hpp"
4+
#include "../../../library/data_structures_[l,r)/bit_uncommon/kd_bit.hpp"
5+
int main() {
6+
cin.tie(0)->sync_with_stdio(0);
7+
int n, q;
8+
cin >> n >> q;
9+
KD_BIT<1> bit1(n);
10+
KD_BIT<2> bit2(1, n);
11+
for (int i = 0; i < n; i++) {
12+
int val;
13+
cin >> val;
14+
bit1.update(i, val);
15+
bit2.update(0, i, val);
16+
}
17+
while (q--) {
18+
int type;
19+
cin >> type;
20+
if (type == 0) {
21+
int p, x;
22+
cin >> p >> x;
23+
bit1.update(p, x);
24+
bit2.update(0, p, x);
25+
} else {
26+
int l, r;
27+
cin >> l >> r;
28+
ll res = bit1.query(l, r);
29+
assert(res == bit2.query(0, 1, l, r));
30+
cout << res << '\n';
31+
}
32+
}
33+
return 0;
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#define PROBLEM \
2+
"https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
3+
#include "../template.hpp"
4+
#include "../../../library/contest/random.hpp"
5+
#include "../../../library/data_structures_[l,r)/bit_uncommon/kd_bit.hpp"
6+
int main() {
7+
cin.tie(0)->sync_with_stdio(0);
8+
for (int num_tests = 0; num_tests < 100; num_tests++) {
9+
int n = rnd(1, 100);
10+
int m = rnd(1, 100);
11+
KD_BIT<2> bit(n, m);
12+
vector<vector<ll>> a(n, vector<ll>(m, 0));
13+
for (int events = 0; events < 100; events++) {
14+
if (events % 2 == 0) {
15+
int i = rnd(0, n - 1);
16+
int j = rnd(0, m - 1);
17+
ll upd = rnd(INT_MIN, INT_MAX);
18+
a[i][j] += upd;
19+
bit.update(i, j, upd);
20+
} else {
21+
int i1 = rnd(0, n);
22+
int j1 = rnd(0, m);
23+
int i2 = rnd(0, n);
24+
int j2 = rnd(0, m);
25+
if (i1 > i2) swap(i1, i2);
26+
if (j1 > j2) swap(j1, j2);
27+
ll naive = 0;
28+
for (int i = i1; i < i2; i++)
29+
for (int j = j1; j < j2; j++) naive += a[i][j];
30+
assert(naive == bit.query(i1, i2, j1, j2));
31+
}
32+
}
33+
}
34+
cout << "Hello World\n";
35+
return 0;
36+
}

tests/scripts/compile_commented_snippets.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ git submodule update
2525
echo "vi rhs;"
2626
echo "vector<vi> mat;"
2727
echo "vector<vector<bool>> grid;"
28-
echo "int n,m,k,tl,tr,l,r,l1,r1,l2,r2,s_l,s_r,root_l,root_r,source,sink,total_flow,bccid,u,v,lsz,rsz,cols,cap,num,x,y;"
28+
echo "int n,m,k,tl,tr,l,r,l1,r1,l2,r2,s_l,s_r,root_l,root_r,source,sink,total_flow,bccid,u,v,lsz,rsz,cols,cap,num,x,y,i,j,i1,i2,j1,j2;"
2929
} >entire_library_without_main
3030

3131
{

0 commit comments

Comments
 (0)