File tree Expand file tree Collapse file tree 1 file changed +8
-1
lines changed Expand file tree Collapse file tree 1 file changed +8
-1
lines changed Original file line number Diff line number Diff line change @@ -107,13 +107,20 @@ lg[1] = 0;
107
107
for (int i = 2 ; i <= MAXN; i++)
108
108
lg[i] = lg[i/2 ] + 1 ;
109
109
```
110
- With C++20 this can be optimized to :
110
+ Alternatively, log can be computed on the fly in constant space and time :
111
111
``` c++
112
+ // C++20
112
113
#include < bit>
113
114
int log2_floor (unsigned long i) {
114
115
return std::bit_width(i) - 1;
115
116
}
117
+
118
+ // pre C++20
119
+ int log2_floor(unsigned long long i) {
120
+ return 63 - (i ? __ builtin_clzll(i) : 64);
121
+ }
116
122
```
123
+ [This benchmark](https://quick-bench.com/q/SWARd6gSu9_RYZUN8PAGEEiimv0) shows that using `lg` array is slower because of cache misses.
117
124
118
125
Afterwards we need to precompute the Sparse Table structure. This time we define $f$ with $f(x, y) = \min(x, y)$.
119
126
You can’t perform that action at this time.
0 commit comments