题解:CF2014H Robin Hood Archery
思路
我们把区间长度分为奇和偶两种。
第一种,区间长度为奇数。显然罗宾汉每次拿的都不小于警长,且多拿一次,警长一定会输,输出 NO。
第二种,区间长度为偶数。罗宾汉每次拿的还是都不小于警长,如果要输出 YES,就只能让他们每次获得的分数两两匹配等,也就是区间异或和为
但是,有可能不相等的情况下也可能相等,比如
但是普通的随机因为范围太小,被卡了,于是我们想到扩大随机范围,就对了。
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,to[1000005],a[1000005],n,q,l,r;
signed main(){
srand(time(0));
for(int i=1;i<=1000000;i++){
to[i]=rand();
to[i]*=rand();
}
cin>>t;
while(t--){
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
a[i]=(a[i-1]^to[a[i]]);
}
while(q--){
cin>>l>>r;
if((r-l+1)&1){
cout<<"NO\n";
continue;
}
if(a[r]==a[l-1])cout<<"YES\n";
else cout<<"NO\n";
}
}
return 0;
}