-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathIva&Pav.cpp
More file actions
50 lines (44 loc) · 1.33 KB
/
Iva&Pav.cpp
File metadata and controls
50 lines (44 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, q;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cin >> q;
// Precompute the prefix bitwise AND for array 'a'
vector<int> prefix_and(n);
prefix_and[0] = a[0];
for (int i = 1; i < n; i++) {
prefix_and[i] = a[i] & prefix_and[i - 1];
}
while (q--) {
int l, k;
cin >> l >> k;
l--; // Convert to 0-based index
int low = l, high = n - 1, result = -1;
// Binary search to find the maximum 'r'
while (low <= high) {
int mid = (low + high) / 2;
if ((l == 0 && prefix_and[mid] >= k) || (prefix_and[mid] >= k && prefix_and[mid - 1] < k)) {
result = mid;
break;
}
if (prefix_and[mid] >= k) {
high = mid - 1;
} else {
low = mid + 1;
}
}
cout << result + 1 << " "; // Convert back to 1-based index and print the result
}
cout << "\n";
}
return 0;
}