#include <bits/stdc++.h>
using namespace std;
class Solution {
public :
int get( int layer, vector < int > & nums, vector < vector < int > > & precompute, vector < vector < int > > & suff, vector < vector < int > > & pref, vector < vector < int > > & startIdx, vector < int > & lg, int l, int r, int ll, int rr, int init = 0 ) {
if ( ll >= rr) return nums[ ll] ;
if ( ll + 1 >= rr) return nums[ ll] | nums[ rr] ;
int b = lg[ r - l] ;
while ( ( ll - l) / b == ( rr - l) / b) { //add binary search or use better indexation
l + = ( ll - l) / b * b;
if ( r > l + b) r = l + b;
b = lg[ r - l] ;
layer++ ;
}
int ans = init; //block[layer]
ans | = pref[ layer] [ ll] ;
ans | = suff[ layer] [ rr] ;
int wl = ( ll - l) / b + 1 , wr = ( rr - l) / b - 1 ;
if ( wl <= wr) {
int where = startIdx[ layer] [ l] , m = ( r - l) / b;
int w = lg[ wr - wl + 1 ] ;
//accurately
int idx = m * w - ( w >= 2 ? ( ( 1 << ( w - 1 ) ) - 1 ) : 0 ) ;
//ans |= precompute[layer][where + idx + (rr / b - 1) - (ll / b + 1)];
//ans |= precompute[layer][where + ll / b + 1]
ans | = precompute[ layer] [ where + idx + wl] ;
//ans |= precompute[layer][where + rr / b - 1 - t]
ans | = precompute[ layer] [ where + idx + wr - ( 1 << w) + 1 ] ;
//cout << ' ' << ans << '\n';
//if ((rr - l) / b - 1 - (1 << w) + 1 < (ll - l) / b + 1) while (true);
}
return ans;
}
void build( vector < int > & nums, vector < vector < int > > & precompute, vector < vector < int > > & suff, vector < vector < int > > & pref, vector < vector < int > > & startIdx, vector < int > & lg, vector < int > & ptr, int layer, int l, int r, int init = 0 ) {
if ( l + 1 >= r) return ;
int b = lg[ r - l] ;
int m = ( r - l) / b;
startIdx[ layer] [ l] = ptr[ layer] ;
ptr[ layer] + = m * b + 2 ; //
//if (ptr[layer] >= precompute[layer].size()) cout << "FUCK\n";
for ( int it = l; it < r; it + = b) {
int ll = it, rr = min( r, it + b) ;
//startIdx[layer][ll] = ptr[layer]++;
for ( int i = ll; i < rr; ++ i) {
suff[ layer] [ i] = nums[ i] ;
if ( i > ll) suff[ layer] [ i] | = suff[ layer] [ i - 1 ] ;
//precompute[[(it - l) / b * (n / b)] |= nums[i];
}
for ( int i = rr - 1 ; i >= ll; -- i) {
pref[ layer] [ i] = nums[ i] ;
if ( i + 1 < rr) pref[ layer] [ i] | = pref[ layer] [ i + 1 ] ;
}
}
int where = startIdx[ layer] [ l] ;
//(n/b)*(n/b+1)/2 [x; y] -> x*(n/b)+y-x
//[m] [m-1] [m-2] .. [m-2^x] [m-x] ..
for ( int w = 0 ; ( 1 << w) <= m; ++ w) {
for ( int i = 0 ; i + ( 1 << w) - 1 < m; ++ i) {
int idx = m * w + i - ( w >= 2 ? ( ( 1 << ( w - 1 ) ) - 1 ) : 0 ) ;
//if (idx > m * b + 1) while(true);
precompute[ layer] [ where + idx] = ( w == 0 ? pref[ layer] [ l + i * b] :
precompute[ layer] [ where + m * ( w - 1 ) - ( w >= 2 ? ( ( 1 << ( w - 2 ) ) - 1 ) : 0 ) + i] |
precompute[ layer] [ where + m * ( w - 1 ) - ( w >= 2 ? ( ( 1 << ( w - 2 ) ) - 1 ) : 0 ) + i + ( 1 << ( w - 1 ) ) ] ) ;
//for (int j = i + 1; j < m; ++j) { //geniucos's suggested using sparse-table here but finding indices are tough
// precompute[layer][where + idx + j - i] = precompute[layer][where + idx + (j - i - 1)] | pref[layer][l + j * b];
//}
}
}
for ( int i = 0 ; i < m; ++ i) {
if ( min( r, l + ( i + 1 ) * b) - ( l + i * b) > 1 ) {
build( nums, precompute, suff, pref, startIdx, lg, ptr, layer + 1 , l + i * b, min( r, l + ( i + 1 ) * b) , init) ;
}
}
}
int minimumSubarrayLength( vector< int > & nums, int k) {
const int D = 5 , overhead = 3 ; //2^(2^D)
int N = ( int ) nums.size ( ) ;
vector < vector < int > > precompute( D + 1 , vector < int > ( N * 2 + overhead) ) ;
vector < vector < int > > suff( D + 1 , vector < int > ( N + 1 ) ) , pref( D + 1 , vector < int > ( N + 1 ) ) ;
vector < vector < int > > startIdx( D + 1 , vector < int > ( N) ) ;
vector < int > lg( N + 1 ) , ptr( D + 1 , 0 ) ;
for ( int i = 1 ; i <= N; ++ i) {
lg[ i] = lg[ i - 1 ] + ( ( 1 << ( lg[ i - 1 ] + 1 ) ) <= i) ;
}
build( nums, precompute, suff, pref, startIdx, lg, ptr, 0 , 0 , N, 0 ) ;
//cout << ' ' << get(0, nums, precompute, suff, pref, startIdx, lg, 0, N, 1, N - 1) << '\n';
//return 0;
int ans = N + 1 ;
for ( int i = 0 , j = - 1 ; i < N; ++ i) {
if ( j < i - 1 ) j = i - 1 ;
for ( ; j + 1 < N && get( 0 , nums, precompute, suff, pref, startIdx, lg, 0 , N, i, j + 1 ) < k; ++ j) ;
if ( j + 1 < N && get( 0 , nums, precompute, suff, pref, startIdx, lg, 0 , N, i, j + 1 ) >= k) ans = min( ans, j + 1 - i + 1 ) ;
}
return ans > N ? - 1 : ans;
}
} sol;
int main( ) {
vector < int > tmp = { 2 , 1 , 8 } ;
cout << sol.minimumSubarrayLength ( tmp, 10 ) << '\n ' ;
return 0 ;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2xhc3MgU29sdXRpb24gewpwdWJsaWM6CiAgICBpbnQgZ2V0KGludCBsYXllciwgdmVjdG9yIDxpbnQ+ICZudW1zLCB2ZWN0b3IgPHZlY3RvciA8aW50PiA+ICZwcmVjb21wdXRlLCB2ZWN0b3IgPHZlY3RvciA8aW50PiA+ICZzdWZmLCB2ZWN0b3IgPHZlY3RvciA8aW50PiA+ICZwcmVmLCB2ZWN0b3IgPHZlY3RvciA8aW50PiA+ICZzdGFydElkeCwgdmVjdG9yIDxpbnQ+ICZsZywgaW50IGwsIGludCByLCBpbnQgbGwsIGludCByciwgaW50IGluaXQgPSAwKSB7CiAgICAgICAgaWYgKGxsID49IHJyKSByZXR1cm4gbnVtc1tsbF07CiAgICAgICAgaWYgKGxsICsgMSA+PSBycikgcmV0dXJuIG51bXNbbGxdIHwgbnVtc1tycl07CiAgICAgICAgaW50IGIgPSBsZ1tyIC0gbF07CiAgICAgICAgd2hpbGUgKChsbCAtIGwpIC8gYiA9PSAocnIgLSBsKSAvIGIpIHsgLy9hZGQgYmluYXJ5IHNlYXJjaCBvciB1c2UgYmV0dGVyIGluZGV4YXRpb24KICAgICAgICAgICAgbCArPSAobGwgLSBsKSAvIGIgKiBiOwogICAgICAgICAgICBpZiAociA+IGwgKyBiKSByID0gbCArIGI7CiAgICAgICAgICAgIGIgPSBsZ1tyIC0gbF07CiAgICAgICAgICAgIGxheWVyKys7CiAgICAgICAgfQogICAgICAgIGludCBhbnMgPSBpbml0OyAvL2Jsb2NrW2xheWVyXQogICAgICAgIGFucyB8PSBwcmVmW2xheWVyXVtsbF07CiAgICAgICAgYW5zIHw9IHN1ZmZbbGF5ZXJdW3JyXTsKICAgICAgICBpbnQgd2wgPSAobGwgLSBsKSAvIGIgKyAxLCB3ciA9IChyciAtIGwpIC8gYiAtIDE7CiAgICAgICAgaWYgKHdsIDw9IHdyKSB7CiAgICAgICAgICAgIGludCB3aGVyZSA9IHN0YXJ0SWR4W2xheWVyXVtsXSwgbSA9IChyIC0gbCkgLyBiOwogICAgICAgICAgICBpbnQgdyA9IGxnW3dyIC0gd2wgKyAxXTsKICAgICAgICAgICAgLy9hY2N1cmF0ZWx5CiAgICAgICAgICAgIGludCBpZHggPSBtICogdyAtICh3ID49IDIgPyAoKDEgPDwgKHcgLSAxKSkgLSAxKSA6IDApOwogICAgICAgICAgICAvL2FucyB8PSBwcmVjb21wdXRlW2xheWVyXVt3aGVyZSArIGlkeCArIChyciAvIGIgLSAxKSAtIChsbCAvIGIgKyAxKV07CiAgICAgICAgICAgIC8vYW5zIHw9IHByZWNvbXB1dGVbbGF5ZXJdW3doZXJlICsgbGwgLyBiICsgMV0KICAgICAgICAgICAgYW5zIHw9IHByZWNvbXB1dGVbbGF5ZXJdW3doZXJlICsgaW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pbnQgbGwgPSBpdCwgcnIgPSBtaW4ociwgaXQgKyBiKTsKICAgICAgICAgICAgLy9zdGFydElkeFtsYXllcl1bbGxdID0gcHRyW2xheWVyXSsrOwogICAgICAgICAgICBmb3IgKGludCBpID0gbGw7IGkgPCBycjsgKytpKSB7CiAgICAgICAgICAgICAgICBzdWZmW2xheWVyXVtpXSA9IG51bXNbaV07CiAgICAgICAgICAgICAgICBpZiAoaSA+IGxsKSBzdWZmW2xheWVyXVtpXSB8PSBzdWZmW2xheWVyXVtpIC0gMV07CiAgICAgICAgICAgICAgICAvL3ByZWNvbXB1dGVbWyhpdCAtIGwpIC8gYiAqIChuIC8gYildIHw9IG51bXNbaV07CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZm9yIChpbnQgaSA9IHJyIC0gMTsgaSA+PSBsbDsgLS1pKSB7CiAgICAgICAgICAgICAgICBwcmVmW2xheWVyXVtpXSA9IG51bXNbaV07CiAgICAgICAgICAgICAgICBpZiAoaSArIDEgPCBycikgcHJlZltsYXllcl1baV0gfD0gcHJlZltsYXllcl1baSArIDFdOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGludCB3aGVyZSA9IHN0YXJ0SWR4W2xheWVyXVtsXTsKICAgICAgICAvLyhuL2IpKihuL2IrMSkvMiBbeDsgeV0gLT4geCoobi9iKSt5LXgKICAgICAgICAvL1ttXSBbbS0xXSBbbS0yXSAuLiBbbS0yXnhdIFttLXhdIC4uCiAgICAgICAgZm9yIChpbnQgdyA9IDA7ICgxIDw8IHcpIDw9IG07ICsrdykgewogICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSArICgxIDw8IHcpIC0gMSA8IG07ICsraSkgewogICAgICAgICAgICAgICAgaW50IGlkeCA9IG0gKiB3ICsgaSAtICh3ID49IDIgPyAoKDEgPDwgKHcgLSAxKSkgLSAxKSA6IDApOwogICAgICAgICAgICAgICAgLy9pZiAoaWR4ID4gbSAqIGIgKyAxKSB3aGlsZSh0cnVlKTsKICAgICAgICAgICAgICAgIHByZWNvbXB1dGVbbGF5ZXJdW3doZXJlICsgaWR4XSA9ICh3ID09IDAgPyBwcmVmW2xheWVyXVtsICsgaSAqIGJdIDoKICAgICAgICAgICAgICAgICAgICBwcmVjb21wdXRlW2xheWVyXVt3aGVyZSArIG0gKiAodyAtIDEpIC0gKHcgPj0gMiA/ICgoMSA8PCAodyAtIDIpKSAtIDEpIDogMCkgKyBpXSB8IAogICAgICAgICAgICAgICAgICAgIHByZWNvbXB1dGVbbGF5ZXJdW3doZXJlICsgbSAqICh3IC0gMSkgLSAodyA+PSAyID8gKCgxIDw8ICh3IC0gMikpIC0gMSkgOiAwKSArIGkgKyAoMSA8PCAodyAtIDEpKV0pOwogICAgICAgICAgICAgICAgLy9mb3IgKGludCBqID0gaSArIDE7IGogPCBtOyArK2opIHsgLy9nZW5pdWNvcydzIHN1Z2dlc3RlZCB1c2luZyBzcGFyc2UtdGFibGUgaGVyZSBidXQgZmluZGluZyBpbmRpY2VzIGFyZSB0b3VnaAogICAgICAgICAgICAgICAgLy8gICAgcHJlY29tcHV0ZVtsYXllcl1bd2hlcmUgKyBpZHggKyBqIC0gaV0gPSBwcmVjb21wdXRlW2xheWVyXVt3aGVyZSArIGlkeCArIChqIC0gaSAtIDEpXSB8IHByZWZbbGF5ZXJdW2wgKyBqICogYl07CiAgICAgICAgICAgICAgICAvL30KICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG07ICsraSkgewogICAgICAgICAgICBpZiAobWluKHIsIGwgKyAoaSArIDEpICogYikgLSAobCArIGkgKiBiKSA+IDEpIHsKICAgICAgICAgICAgICAgIGJ1aWxkKG51bXMsIHByZWNvbXB1dGUsIHN1ZmYsIHByZWYsIHN0YXJ0SWR4LCBsZywgcHRyLCBsYXllciArIDEsIGwgKyBpICogYiwgbWluKHIsIGwgKyAoaSArIDEpICogYiksIGluaXQpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAga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