Skip to content

Commit 106f3b5

Browse files
committed
first draft
1 parent f2c0738 commit 106f3b5

4 files changed

Lines changed: 59 additions & 7 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
template<int K> struct KD_BIT;
2+
template<> struct KD_BIT<0> {
3+
ll s = 0;
4+
void update(ll v) { s += v; }
5+
ll query() { return s; }
6+
};
7+
template<int K> struct KD_BIT {
8+
vector<KD_BIT<K - 1>> s;
9+
template<class... A>
10+
KD_BIT(int n, A... a): s(n, KD_BIT<K - 1>(a...)) {}
11+
template<class... A> void update(int i, A... a) {
12+
for (; i < sz(s); i |= i + 1) s[i].update(a...);
13+
}
14+
template<class... A> ll query(int l, int r, A... a) {
15+
ll ans = 0;
16+
for (; l < r; r &= r - 1) ans += s[r - 1].query(a...);
17+
for (; r < l; l &= l - 1) ans -= s[l - 1].query(a...);
18+
return ans;
19+
}
20+
};

library/data_structures_[l,r]/seg_tree_uncommon/walk.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
int walk(int l, int r, const auto& f) {
2+
bool initialized = 0;
3+
T x;
24
while (l <= r) {
35
int u = l + n, v = __lg(min(u & -u, r - l + 1));
4-
if (f(s[u >> v])) l += 1 << v;
6+
T y = initialized ? op(x, f(s[u >> v])) : s[u >> v];
7+
if (f(y)) l += 1 << v, x = y, initialized = 1;
58
else r = l + (1 << v) - 2;
69
}
710
return l;
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+
}

tests/library_checker_aizu_tests/data_structures/simple_tree_inc_walk.test.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,8 @@ int main() {
3232
if (total == 0) {
3333
cout << -1 << '\n';
3434
} else {
35-
int pref_sum = 0;
3635
cout << st.walk(0, k, [&](int sum) {
37-
if (pref_sum + sum < total) {
38-
pref_sum += sum;
39-
return 1;
40-
}
41-
return 0;
36+
return sum < total;
4237
}) << '\n';
4338
}
4439
}

0 commit comments

Comments
 (0)