题解
1.如果能分成偶数个区间,那么一定能分为两个区间
2.如果能分为奇数个区间,那么一定能分为三个区间
3.能分为两个区间,说明区间异或和为 \(0\)
4.能分为三个区间,这三个区间分别为区间 \(a,b,c\) ,则 \(ab\) 区间异或和为零, \(bc\) 区间异或和为零
code
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
int pre[MAXN] = {0};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, q;
cin >> n >> q;
map<int,vector<int> > id;
id[0].push_back(0);
for (int i = 1; i <= n; i++)
{
int x;
cin>>x;
pre[i]=pre[i-1]^x;
id[pre[i]].push_back(i);
}
while(q--)
{
int l,r;
cin>>l>>r;
if(pre[r]==pre[l-1]) cout<<"YES\n";
else
{
int itl=*(lower_bound(id[pre[l-1]].begin(),id[pre[l-1]].end(),r)-1);//如果[l,r]区间异或和为零,代表前缀和pre[l-1]==pre[r]
int itr=*(lower_bound(id[pre[r]].begin(),id[pre[r]].end(),l));
//printf("itl:%d itr:%d\n",itl,itr);
if(itl>=itr&&itl!=l&&itr!=r) cout<<"YES\n";
else cout<<"NO\n";
}
}
cout<<"\n";
}
return 0;
}
标签:pre,XOR,cout,int,Segments,Equal,cin,异或,区间
From: https://www.cnblogs.com/pure4knowledge/p/18172621