-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeautiful Array
100 lines (91 loc) Β· 2.04 KB
/
Beautiful Array
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int t=1;
cin >> t;
while(t--){
int n, q;
cin >> n >> q;
int arr[n];
int ans = 0;
for(int i=0; i<n; i++){
cin >> arr[i];
if(i==0){
ans = arr[i];
}else{
ans = (ans&arr[i]);
}
}
int mp[22]={0};
string s[22];
for(int i=0; i<=21; i++){
int j=0;
int cnt3=0;
string s1="";
while(j<n){
while(j<n && ((arr[j]&(1<<i))==(1<<i))){
j++;
s1+='1';
}
int cnt2=0;
while(j<n && ((arr[j]&(1<<i))==0)){
cnt2++;
j++;
s1+='0';
}
if(cnt2){
cnt3++;
}
}
s[i]=s1;
mp[i]=cnt3;
}
while(q--){
int x,y;
cin >> x >> y;
int sum1=0;
int j=21;
bool flag=false;
int index=0;
while(j>=0){
if(((1<<j)&y)==(1<<j)){
if(mp[j]==0){
j--;
break;
}else{
if(mp[j]<=x){
index=j;
sum1=(sum1+0LL+(1<<j));
flag=true;
j--;
break;
}else{
j--;
}
}
}else{
j--;
}
}
if(flag){
while(j>=0){
if(((1<<j)&y)==(1<<j)){
if(mp[j]==0){
sum1 = (sum1-(1<<j));
}else if(s[index]==s[j]){
sum1 = (sum1+(1<<j));
}
j--;
}else{
j--;
}
}
}
cout << (ans+sum1) << endl;
}
}
return 0;
}