#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 int arr[1 << 20];
for (int i = 0; i < (1 << 20); i++) { arr[i] = i; }
std::mt19937 gen;
std::uniform_int_distribution<int> dist(0, 1 << 20);
std::vector<int> queries(1 << 23);
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 (int 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+KGFycix2YWwpOyB9Cgl9Cn07Cgp0ZW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