#include using namespace std; typedef long long ll; typedef pair pii; typedef pair pll; typedef pair pdd; template using minheap = priority_queue, greater>; #define allof(x) x.begin(), x.end() const int MAXN = 1e5 + 5; const int SQ = 400; int nxt_miss[MAXN][32]; int nxt_have[MAXN][32]; int N, K; int arr[MAXN]; set avail; int smax[MAXN][20]; int smin[MAXN][20]; int ans[MAXN]; int bitsum[MAXN][32]; int get_range(int l, int r){ int lvl = 0; int shift = 1; while(l + shift * 2 - 1 <= r){ shift *= 2; lvl++; } return max(smax[l][lvl], smax[r - shift + 1][lvl]) - min(smin[l][lvl], smin[r - shift + 1][lvl]); } int get_orv(int l, int r){ int ORV = 0; int ANDV = 0x7fffffff; for(int a = 0; a < 31; a++){ if(bitsum[r][a] - bitsum[l - 1][a] > 0){ ORV |= (1 << a); } if(bitsum[r][a] - bitsum[l - 1][a] < r - l + 1){ ANDV &= ~(1 << a); } } return (ORV - ANDV); } int main(){ cin.tie(0); cin.sync_with_stdio(0); cin >> N >> K; for(int a = 1; a <= N; a++) { cin >> arr[a]; ans[a] = -1; avail.insert(a); smax[a][0] = arr[a]; smin[a][0] = arr[a]; for(int b = 0; b < 31; b++){ if(arr[a] & (1 << b)){ bitsum[a][b] = 1; nxt_miss[a][b] = 0x3f3f3f3f; nxt_have[a][b] = a; } else { nxt_miss[a][b] = a; nxt_have[a][b] = 0x3f3f3f3f; } bitsum[a][b] += bitsum[a - 1][b]; } } for(int a = N - 1; a >= 1; a--){ for(int b = 0; b < 31; b++){ nxt_miss[a][b] = min(nxt_miss[a][b], nxt_miss[a + 1][b]); nxt_have[a][b] = min(nxt_have[a][b], nxt_have[a + 1][b]); } } for(int b = 1; b < 20; b++){ for(int a = 1; a <= N; a++){ smax[a][b] = smax[a][b - 1]; smin[a][b] = smin[a][b - 1]; int shift = 1 << (b - 1); if(a + shift <= N){ smax[a][b] = max(smax[a + shift][b - 1], smax[a][b]); smin[a][b] = min(smin[a + shift][b - 1], smin[a][b]); } } } priority_queue> pq; for(int a = 1; a <= N; a++){ set to_check; to_check.insert(a); for(int i = 0; i < 31; i++){ if(nxt_miss[a][i] != 0x3f3f3f3f){ to_check.insert(nxt_miss[a][i]); } if(nxt_have[a][i] != 0x3f3f3f3f){ to_check.insert(nxt_have[a][i]); } } for(int i : to_check){ int ORV = get_orv(a, i); int l = i, r = N; while(l <= r){ int mid = (l + r) / 2; if(ORV - get_range(a, mid) < K){ //too far! r = mid - 1; } else { l = mid + 1; } } if(i <= l - 1 && ORV - get_range(a, l - 1) >= K){ // cout << a << " pushed to " << l - 1 << "\n"; // cout << ORV << " - " << get_range(a, l - 1) << "\n"; pq.push({(l - 1) - a + 1, {a, l - 1}}); //the last one that works } } } // cout << "Get to line " << __LINE__ << endl; while(!pq.empty()){ int len = pq.top().first; pii range = pq.top().second; pq.pop(); while(1){ auto it = avail.lower_bound(range.first); if(it != avail.end() && *it <= range.second){ ans[*it] = len; avail.erase(it); } else { break; } } } for(int a = 1; a <= N; a++){ cout << ans[a] << "\n"; } }