#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
template<int n, typename T>
int lowerBound(const T* __restrict arr, const T val) {
if (n == 1) { return val > arr[0]; }
else {
constexpr int mid = n / 2;
if (val > arr[mid]) { return mid+lowerBound<n-mid, T>(arr+mid, val); }
else { return lowerBound<mid, T>(arr,val); }
}
};
template<int n, typename T>
int loserBound(const T* __restrict arr, const T val) {
return int(std::lower_bound(arr,arr+n,val)-arr);
}
int main() {
static double arr[1 << 20];
for (int i = 0; i < (1 << 20); i++) { arr[i] = i; }
std::mt19937 gen;
std::uniform_real_distribution<double> dist(0, 1 << 20);
std::vector<double> queries(1 << 9);
for (auto &it : queries) { it = dist(gen); }
#define TEST(s, f) \
{ \
std::cout << s << std::endl; \
int res = 0; \
double tin = (double)clock(), tout; \
for (int i = 0; i < (1 << 15); i++) {\
for (auto q : queries) { \
res += f<1<<20>(&arr[0],q); \
} \
} \
tout = (double)clock(); \
std::cout << "time = " << (tout - tin) / CLOCKS_PER_SEC << "s, sum = " << res << std::endl; \
}
TEST("Testing std::lower_bound...", loserBound);
TEST("Testing lowerBound...", lowerBound);
return 0;
}
I3ByYWdtYSBHQ0Mgb3B0aW1pemUoIk9mYXN0IikKI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnRlbXBsYXRlPGludCBuLCB0eXBlbmFtZSBUPgppbnQgbG93ZXJCb3VuZChjb25zdCBUKiBfX3Jlc3RyaWN0IGFyciwgY29uc3QgVCB2YWwpIHsKCWlmIChuID09IDEpIHsgcmV0dXJuIHZhbCA+IGFyclswXTsgfSAKCWVsc2UgewoJCWNvbnN0ZXhwciBpbnQgbWlkID0gbiAvIDI7CgkJaWYgKHZhbCA+IGFyclttaWRdKSB7IHJldHVybiBtaWQrbG93ZXJCb3VuZDxuLW1pZCwgVD4oYXJyK21pZCwgdmFsKTsgfQoJCWVsc2UgeyByZXR1cm4gbG93ZXJCb3VuZDxtaWQsIFQ+KGFycix2YWwpOyB9Cgl9Cn07Cgp0ZW1wbGF0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