Skip to content

Commit 6387a0f

Browse files
committed
adding first draft of KD tree
1 parent d25d6e5 commit 6387a0f

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
using T = int64_t;
3+
const T unit = 0;
4+
T op(T a, T b) { return a + b; }
5+
template<int K> struct KD_SEG {
6+
int n;
7+
vector<KD_SEG<K - 1>> s;
8+
KD_SEG(int n, auto... a):
9+
n(n), s(2 * n, KD_SEG<K - 1>(a...)) {}
10+
void update(int p, auto... a) {
11+
p += n;
12+
s[p].update(a...);
13+
for (p /= 2; p > 0; p /= 2)
14+
s[p].pull(s[2 * p], s[2 * p + 1], a...);
15+
}
16+
T query(int l, int r, auto... a) {
17+
T x = unit, y = unit;
18+
for (l += n, r += n; l < r; l /= 2, r /= 2) {
19+
if (l % 2) x = op(x, s[l++].query(a...));
20+
if (r % 2) y = op(s[--r].query(a...), y);
21+
}
22+
return op(x, y);
23+
}
24+
void pull(const KD_SEG<K>& left, const KD_SEG<K>& right,
25+
int p, auto... a) {
26+
p += n;
27+
s[p].pull(left.s[p], right.s[p], a...);
28+
for (p /= 2; p > 0; p /= 2)
29+
s[p].pull(s[2 * p], s[2 * p + 1], a...);
30+
}
31+
};
32+
template<> struct KD_SEG<0> {
33+
T val = unit;
34+
void update(T v) { val = v; }
35+
T query() { return val; }
36+
void pull(const KD_SEG<0>& left, const KD_SEG<0>& right,
37+
auto...) {
38+
val = op(left.val, right.val);
39+
}
40+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#define PROBLEM \
2+
"https://onlinejudge.u-aizu.ac.jp/problems/DPL_3_A"
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, m;
8+
cin >> n >> m;
9+
KD_BIT<2> bit(n, m);
10+
//! KD_BIT<2> bit(n, m);
11+
//! bit.update(i, j, 5);
12+
//! // 0 <= i < n
13+
//! // 0 <= j < m
14+
//! bit.query(i1, i2, j1, j2);
15+
//! // 0 <= i1 <= i2 <= n
16+
//! // 0 <= j1 <= j2 <= m
17+
for (int i = 0; i < n; i++) {
18+
for (int j = 0; j < m; j++) {
19+
int c;
20+
cin >> c;
21+
if (c) bit.update(i, j, 1);
22+
}
23+
}
24+
int res = 0;
25+
for (int i = 0; i < n; i++) {
26+
for (int j = 0; j < m; j++) {
27+
int start = 0, end = min(n - i, m - j) + 1;
28+
while (start + 1 < end) {
29+
int mid = (start + end) / 2;
30+
if (bit.query(i, i + mid, j, j + mid) == 0)
31+
start = mid;
32+
else end = mid;
33+
}
34+
res = max(res, start);
35+
}
36+
}
37+
cout << res * res << '\n';
38+
return 0;
39+
}

0 commit comments

Comments
 (0)