summaryrefslogtreecommitdiff
path: root/test/datastructures/lichao.cpp
blob: c8a56a3046935bab252d618ed453f691d8938fe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "../util.h"
constexpr ll inf = LL::INF;
#include <datastructures/lichao.cpp>

void stress_test(ll range) {
	ll queries = 0;
	for (int tries = 0; tries < 1000; tries++) {
		int n = Random::integer<int>(1, 100);
		xs = Random::distinct<ll>(n, -range, range);
		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>(-range, range);
				ll l = Random::integer<ll>(-range, range);
				ll r = Random::integer<ll>(-range, range);
				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(100);
	stress_test(1'000);
	stress_test(1'000'000);
	performance_test();
}