Skip to content

Commit 00332a7

Browse files
committed
first draft of hilbert
1 parent eb7f7a1 commit 00332a7

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
//! https://codeforces.com/blog/entry/61203?#comment-1064868
3+
//! @code
4+
//! vector<pii> queries;
5+
//! ranges::sort(queries, {}, [&](pii& x) {
6+
//! return hilbert(x.first, x.second);
7+
//! });
8+
//! @endcode
9+
//! @time O(log(n))
10+
//! @space O(1)
11+
ll hilbert(int x, int y) {
12+
ll d = 0, mx = 1;
13+
while(mx <= max(x, y)) mx *= 4;
14+
for (int s = mx / 2; s; s /= 2) {
15+
bool rx = x & s, ry = y & s;
16+
d = d * 4 | (ry * 3 ^ rx);
17+
if (!rx) {
18+
if (ry) x ^= mx - 1, y ^= mx - 1;
19+
swap(x, y);
20+
}
21+
}
22+
return d;
23+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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/uncommon/hilbert_mos.hpp"
6+
const vector<int> di = {1, -1, 0, 0};
7+
const vector<int> dj = {0, 0, 1, -1};
8+
void test(int i, int j) {
9+
ll res = hilbert(i, j);
10+
int cnt_prev = 0;
11+
int cnt_next = 0;
12+
for (int k = 0; k < 4; k++) {
13+
int to_i = i + di[k];
14+
int to_j = j + dj[k];
15+
if(to_i >= 0 && to_j >= 0) {
16+
cnt_prev += (res - 1 == hilbert(to_i, to_j));
17+
cnt_next += (res + 1 == hilbert(to_i, to_j));
18+
}
19+
}
20+
if(i==0 && j==0) assert(res == 0);
21+
else assert(cnt_prev == 1);
22+
assert(cnt_next == 1);
23+
}
24+
25+
int main() {
26+
cin.tie(0)->sync_with_stdio(0);
27+
for (int i = 0; i < 3000; i++) {
28+
for (int j = 0; j < 3000; j++) {
29+
test(i,j);
30+
}
31+
}
32+
for (int iter = 50000; iter--;) {
33+
int i = rnd(0, 1'000'000'000);
34+
int j = rnd(0, 1'000'000'000);
35+
test(i,j);
36+
}
37+
cout << "Hello World\n";
38+
}

0 commit comments

Comments
 (0)