-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcf-900-div3.cpp
More file actions
79 lines (75 loc) · 1.15 KB
/
cf-900-div3.cpp
File metadata and controls
79 lines (75 loc) · 1.15 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
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
#include<bits/stdc++.h>
using namespace std;
#define ll long long
bool isValid(vector<vector<ll>> &store,ll l, ll r, ll k, ll x){
ll count = r-l+1;
ll res=0;
for(int i=0;i<31;i++){
ll val = store[r+1][i]-store[l][i];
if(val>=count){
res=res+(1<<i);
}
}
if(res>=k){
return true;
}
return false;
}
ll getValidR(vector<vector<ll>> &store,ll l, ll k, ll x){
ll low=l;
ll high=x-1;
ll ans=-1;
while(low<=high){
ll mid = low + (high-low)/2;
if(isValid(store,l,mid,k,x)){
low=mid+1;
ans=max(ans,mid);
}
else{
high=mid-1;
}
}
if(ans!=-1){
ans++;
}
return ans;
}
void solve(){
ll x;
cin>>x;
vector<ll>v(x);
for(int i=0;i<x;i++){
cin>>v[i];
}
vector<vector<ll>> store(x+1, vector<ll>(32,0));
for(int i=0;i<x;i++){
int val=v[i];
int c=0;
for(int j=0;j<32;j++){
store[i+1][j]+=store[i][j];
}
while(val>0){
int mod=val%2;
val=val/2;
store[i+1][c]+=mod;
c++;
}
}
ll q;
cin>>q;
while(q--){
ll l,k;
cin>>l>>k;
l--;
cout<<getValidR(store,l,k,x)<<" ";
}
cout<<endl;
}
int main(){
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}