summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/datastructures/lichao.cpp73
1 files changed, 73 insertions, 0 deletions
diff --git a/test/datastructures/lichao.cpp b/test/datastructures/lichao.cpp
new file mode 100644
index 0000000..acd048f
--- /dev/null
+++ b/test/datastructures/lichao.cpp
@@ -0,0 +1,73 @@
+#include "../util.h"
+constexpr ll inf = LL::INF;
+#include <datastructures/lichao.cpp>
+
+void stress_test() {
+ ll queries = 0;
+ for (int tries = 0; tries < 1000; tries++) {
+ int n = Random::integer<int>(1, 100);
+ xs = Random::distinct<ll>(n, -1000, 1000);
+ sort(all(xs));
+
+ vector<ll> naive(n, inf);
+ Lichao tree;
+
+ for (int operations = 0; operations < 1000; operations++) {
+ {
+ ll m = Random::integer<ll>(-100, 100);
+ ll c = Random::integer<ll>(-1000, 1000);
+ ll l = Random::integer<ll>(-1000, 1000);
+ ll r = Random::integer<ll>(-1000, 1000);
+ Fun f{m, c};
+
+ tree.segmentInsert(f, l, r);
+ for (int i = 0; i < n; i++) {
+ if (l <= xs[i] && xs[i] < r) naive[i] = min(naive[i], f(i));
+ }
+ }
+ {
+ queries++;
+ int i = Random::integer<int>(0, n);
+ ll got = tree.query(xs[i]);
+ ll expected = naive[i];
+ if (got != expected) cerr << xs[i] << endl;
+ if (got != expected) cerr << " got: " << got << ", expected: " << expected << FAIL;
+ }
+ }
+ }
+ cerr << " tested random queries: " << queries << endl;
+}
+
+constexpr int N = 200'000;
+void performance_test() {
+ timer t;
+ xs = Random::distinct<ll>(N, -1'000'000'000, 1'000'000'000);
+ sort(all(xs));
+
+ t.start();
+ Lichao tree;
+ t.stop();
+
+ hash_t hash = 0;
+ for (int operations = 0; operations < N; operations++) {
+
+ ll m = Random::integer<ll>(-1'000'000, 1'000'000);
+ ll c = Random::integer<ll>(-1'000'000, 1'000'000);
+ ll l = Random::integer<ll>(-1'000'000'000, 1'000'000'000);
+ ll r = Random::integer<ll>(-1'000'000'000, 1'000'000'000);
+ ll x = xs[Random::integer<int>(N)];
+ Fun f{m, c};
+
+ t.start();
+ tree.segmentInsert(f, l, r);
+ hash ^= tree.query(x);
+ t.stop();
+ }
+ if (t.time > 1000) cerr << " too slow: " << t.time << FAIL;
+ cerr << " tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
+}
+
+int main() {
+ stress_test();
+ performance_test();
+}