题目链接:hdu_5968_异或密码
题意:
中午,不解释
题解:
前缀处理一下异或值,然后上个二分查找就行了,注意是unsigned long long
#include<bits/stdc++.h>
#define F(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long ll; const int N=;
int t,dp[N],a[N],n,m,tp; struct dt
{
int val,len;
bool operator<(const dt &b)const
{
return val<b.val;
}
}s[N*N],ttp;
bool cmp(dt a,dt b)
{
if(a.val==b.val)return a.len>b.len;
return a.val<b.val;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
F(i,,n)scanf("%d",a+i);
F(i,,n)dp[i]=dp[i-]^a[i];
int ed=;
F(j,,n)F(k,j,n)
{
s[++ed].val=dp[k]^dp[j-];
s[ed].len=k-j+;
}
sort(s+,s++ed,cmp);
scanf("%d",&m);
F(i,,m)
{
scanf("%d",&tp);
ttp.val=tp;
int now1=lower_bound(s+,s++ed,ttp)-s,ans;
int now2=now1-;
now2=lower_bound(s+,s++ed,s[now2])-s;
if(now1>ed)now1=ed;
int n1=abs(s[now1].val-tp),n2=abs(s[now2].val-tp);
if(n1>n2)ans=now2;
else if(n1<n2)ans=now1;
else if(s[now1].len>s[now2].len)ans=now1;
else ans=now2;
printf("%d\n",s[ans].len);
}
puts("");
}
return ;
}