日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Gym - 101608G】WiFi Password (区间或,线段树 或 按位处理+尺取 或 二分) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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 (区间或,线段树 或 按位处理+尺取 或 二分)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。