题干:

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.

Output

For each test case, if no subarray meets the requirement, print 0. Otherwise, print the maximum length of a subarray that meets the requirement.

Example

Input

3
5 8
1 4 5 3 1
5 10
8 2 6 1 10
4 2
9 4 5 8

Output

5
3
0

Note

To get the value of x OR y, consider both numbers in binary (padded with zeros to make their lengths equal), apply the OR operation on the corresponding bits, and return the result into decimal form. For example, the result of 10 OR 17 = 01010 OR 10001 = 11011 = 27.

题目大意:

给你n和m,n代表有n个数,然后让你找出一个最长的区间,使得这个区间内的所有数的‘’或‘’都小于等于m。

解题报告:

因为或运算对于区间长度是满足单调不减性的,所以我们可以二分长度+check。其实尺取是最优秀的但是二分好写一些,并且告诉你了n的总数量<=1e6,所以二分足够了,这个总复杂度应该是nlognlogn的。

因为满足区间加和的性质,所以这题也可以线段树去做(反正不带更新,如果带更新就麻烦了⑧),这样还是尺取,然后每次log去查询区间和就好了,抽空写一写。

AC代码:(592ms水过)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 1e5 + 5;
int a[MAX],n,v;
int d[MAX][22];
int cnt[22];
int cal() {int res = 0;for(int j = 0; j<22; j++) {if(cnt[j]>0) res += (1<<j); }return res;
}
bool ok(int x) {memset(cnt,0,sizeof cnt);for(int i = 1; i<=x; i++) {for(int j = 0; j<22; j++) cnt[j] += d[i][j];}int sum = cal();if(sum <= v) return 1;for(int l = 2; l+x-1<=n; l++) {int r = l+x-1;for(int j = 0; j<22; j++) cnt[j] -= d[l-1][j];for(int j = 0; j<22; j++) cnt[j] += d[r][j];if(cal() <= v) return 1;}return 0 ;
}
int main()
{freopen("wifi.in","r",stdin);int t;cin>>t;while(t--) {scanf("%d%d",&n,&v);memset(d,0,sizeof d);for(int i = 1; i<=n; i++) scanf("%d",a+i);for(int i = 1; i<=n; i++) {int tmp = a[i],cur = 0;while(tmp) {d[i][cur++] = tmp%2;tmp/=2;}}int l = 1,r = n,mid = (l+r)>>1,ans=0;while(l<=r) {mid=(l+r)>>1;if(ok(mid)) ans = mid,l = mid+1;else r = mid-1;}printf("%d\n",ans);}return 0 ;
}

【Gym - 101608G】WiFi Password (区间或,线段树 或 按位处理+尺取 或 二分)相关推荐

  1. GYM 100827 I.Salary Inequity(线段树)

    Description 一棵有向树,1是根,每个点有点权,两种操作: Q u:查询u节点为根节点的子树中权值最大值与最小值的差 R u x:给以u节点为根节点的子树所有点点权加上x Input 第一行 ...

  2. 单调栈 or 线段树扫描线 ---- E. Delete a Segment [单调栈+二分] [扫描线处理空白位置的技巧乘2]

    题目链接 题目大意: 给出nnn个线段代表集合,现在问若可以将其中任意一个线段删除,则能够形成最多多少个独立的集合(取并集后) 解题思路1: 首先我们先对线段按照起点排序 那么我们枚举删除的线段iii ...

  3. luogu P5142 区间方差(线段树、乘法逆元)

    luogu P5142 区间方差 本题要求维护模区间方差,很明显是一道数据结构题. 我们化简方差公式: 而平均数等于 可以发现,我们只需要维护序列的区间和和区间平方和,就可以维护平均数和方差. 区间和 ...

  4. CF280D-k-Maximum Subsequence Sum【模拟费用流,线段树】

    正题 题目链接:https://www.luogu.com.cn/problem/CF280D 题目大意 一个长度为nnn的序列,mmm次操作 修改一个数 询问一个区间中选出kkk段不交子段使得和最大 ...

  5. YbtOJ#662-交通运输【线段树合并,树状数组】

    正题 题目链接:http://www.ybtoj.com.cn/contest/122/problem/2 题目大意 给出nnn个点的一棵有根树,对于每个xxx求,删除点xxx后修改某个点的父节点(修 ...

  6. 如何在vs中创建r树索引代码_线段树详解与实现

    此篇文章用于记录<玩转数据结构>课程的学习笔记 什么是线段树 线段树也被称为区间树,英文名为Segment Tree或者Interval tree,是一种高级的数据结构.这种数据结构更多出 ...

  7. 【BZOJ5457】城市(线段树合并)

    点此看题面 大致题意: 一棵树上每个点有颜色\(a_i\)和权值\(b_i\),求以每个点为根的子树内权值和最大的颜色及其权值和. 线段树合并 这是一道线段树合并板子题. (关于线段树合并,可参考我的 ...

  8. 扫描线+线段树简介 AcWing 248窗内的星星题解

    ----出自南昌理工学院ACM集训队 这周学习了线段树和扫描线的解题方法,下面由小菜鸡简介一下: 一般扫描线的题目最简单的便是扫描线裸模板(一般来说的话:数据范围小),其次的话便是进行拓展成线段树+扫 ...

  9. Inconvenient Pairs(线段树/二分)

    题目 题意:给定由n个横街道和m个纵街道组成的地图(其中边界0和1000000默认都是街道):给定k个在街道上的行人(显然这些行人所在的横或纵坐标至少有一个在街道上).求有多少对人,他们之间的最短距离 ...

最新文章

  1. android 8.0 3D锁屏,Android 8.0 锁屏滑动无法解锁
  2. 从平台到中台:Elaticsearch 在蚂蚁金服的实践经验
  3. sql数据库实例(c#查询登录界面)
  4. NOIP模拟测试16「Drink·blue·weed」
  5. 审计导致select * 报ORA-01435: user does not exist
  6. 7.2Python入门(三)
  7. leetcode专题训练笔记
  8. Zookeeper原理分析之存储结构ZkDatabase
  9. 838计算机专业课包含什么,华南农业大学
  10. R语言模拟:Cross Validation
  11. 将数字转换为中文大写(缩写)
  12. windows server服务器打安全补丁
  13. ps html 优化,优化 Photoshop 的性能
  14. 人机协作机器人发展趋势_人机协作引领机器人产业新趋势
  15. 虚拟化中常见的三种硬盘模式
  16. Java面向对象高级部分——通过Class类实例化对象(五十二)
  17. [Tensorflow]关于TFRecord和tf.Example的使用
  18. 华南理工计算机电路基础试题,2017年华南理工大学计算机电路基础
  19. 山东探植物园唯美规划 明年竣工成烟台“后花园”
  20. 教你快速高效接入SDK——手游聚合SDK的总体思路和架构

热门文章

  1. WinAPI: SetTextColor - 设置设备环境的文本颜色
  2. [Leetcode][第647题][JAVA][回文子串][动态规划][中心扩展][Manacher 算法]
  3. [Leedcode][JAVA][第466题][统计重复个数][数组]
  4. session很快失效_深夜,我偷听到程序员要对session下手...
  5. 1618D. Array and Operations
  6. python能代替vba吗_VBA会被Python代替吗?
  7. 石油化工设备维护检修规程_旋回破碎机横梁臂架、衬板、内外铜套检修步骤及设备检修维护要点...
  8. java 采样_Java编程实现beta分布的采样或抽样实例代码
  9. sigprocmask 阻塞进程
  10. reentrantlock非公平锁不会随机挂起线程?_程序员必须要知道的ReentrantLock 及 AQS 实现原理...