先预处理前缀和sum[i],枚举区间尾i,查找哈希表中是否有sum[i]-k和sum[i]+k,哈希表用map就行了,mp[0]表示以偶数下标结尾前缀和,mp[1]表示以奇数下标结尾前缀和。
快速读入read.
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<iomanip>
#include<map>
#include<algorithm>
#include<queue>
#include<set>
#define inf 1000000000
#define pi acos(-1.0)
#define eps 1e-8
#define seed 131
using namespace std;
typedef pair<int,int> pii;
typedef unsigned long long ULL;
typedef long long LL;
const int maxn=100005;
int n;
int k;
int d[1000005];
LL sum[1000005];
bool check()
{
map<LL,int>mp[2];
mp[0][0]=1;
for(int i=1;i<=n;i++)
{
if(mp[0].count(sum[i]-k))
return true;
if(mp[1].count(sum[i]+k))
return true;
if(i%2==0)
mp[0][sum[i]]=1;
else
mp[1][sum[i]]=1;
}
return false;
}
void read(int &x) {
char ch = getchar();
int tag = 1;
x = 0;
while ((ch < '0' || ch > '9') && ch != '-') {
ch = getchar();
}
if (ch == '-') {
tag = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= tag;
}
int main()
{
int t;
scanf("%d",&t);
int cas=1;
while(t--)
{
scanf("%d%d",&n,&k);
sum[0]=0;
for(int i=1;i<=n;i++)
{
read(d[i]);
if(i%2==1)
sum[i]=sum[i-1]+d[i];
else
sum[i]=sum[i-1]-d[i];
}
if(check())
printf("Case #%d: Yes.\n",cas++);
else
printf("Case #%d: No.\n",cas++);
}
return 0;
}