Gym – 101608G (尺取)
https://vjudge.net/contest/208695#problem/G
Just days before the JCPC, your internet service went down. You decided to continue your training at the ACM club at your university. Sadly, you discovered that they have changed the WiFi password. On the router, the following question was mentioned, the answer is the WiFi password padded with zeros as needed.
A subarray [l, r] of an array A is defined as a sequence of consecutive elements Al, Al + 1, ..., Ar, the length of such subarray is r - l + 1. The bitwise OR of the subarray is defined as: Al OR Al + 1 OR ... OR Ar, where OR is the bitwise OR operation (check the notes for details).
Given an array A of n positive integers and an integer v, find the maximum length of a subarray such that the bitwise OR of its elements is less than or equal to v.
Input
The first line contains an integer T (1 ≤ T ≤ 128), where T is the number of test cases.
The first line of each test case contains two space-separated integers n and v (1 ≤ n ≤ 105) (1 ≤ v ≤ 3 × 105).
The second line contains n space-separated integers A1, A2, …, An (1 ≤ Ai ≤ 2 × 105), the elements of the array.
The sum of n overall test cases does not exceed 106.
题意:
给定
n
n
个数,求有最长的连续 区间长度使得区间内数的按位或小于等于给定
#include<bits/stdc++.h>
using namespace std;
#define N 200010
#define LL long long
int p[N][22];
int cnt[22];
void add(int &now,int i){
cnt[i]++;
if(cnt[i]==1)now+=(1<<i);
}
void dec(int &now,int i){
cnt[i]--;
if(cnt[i]==0)now-=(1<<i);
}
int main(){
freopen("wifi.in","r",stdin);
int T;scanf("%d",&T);
while(T--){
memset(cnt,0,sizeof(cnt));
memset(p,0,sizeof(p));
int n,v;scanf("%d%d",&n,&v);
int x;
int now=0;
int l=1;
int ans=0;
for(int r=1;r<=n;++r){
scanf("%d",&x);
for(int i=0;i<=20;++i){
if(x&(1<<i))p[r][i]++, add(now,i);
}
while(now>v){
for(int i=0;i<=20;++i){
if(p[l][i])dec(now,i);
}
l++;
}
ans=max(ans,r-l+1);
}
cout<<ans<<endl;
}
}