F. Equal XOR Segments
链接
https://codeforces.com/problemset/problem/1968/F
题目
思路
感觉这是一道非常好的区间异或结论题!
思路参考大佬题解
值得总结的:
1.区间异或的可加性:^[la,ra] == ^[ra+1,rb]
--> ^[1,ra] == ^[1,rb]
2.aaa = a,用来消除过长的异或.
虽然刚开始想用线段树,但是没法实现判断
如何实现选取的见代码,以及复习下lower_bound()的用法
代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
#define IOS ios::sync_with_stdio(false), cin.tie(0) ,cout.tie(0)
using namespace std;
#define int long long
const int N = 2e5 + 10;
int a[N];
signed main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int t; cin >> t;
while (t--)
{
int n, q; cin >> n >> q;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
a[i] = a[i] xor a[i - 1];
}
map<int, vector<int>>id;
for (int i = 0; i <= n; i++)
{
id[a[i]].push_back(i);
}
for (int i = 0; i < q; i++)
{
bool la = false;
int l; int r;
cin >> l >> r;
if (a[l - 1] == a[r])la = true;
while (!la)
{
vector<int>::iterator it1, it2;
it1 = lower_bound(id[a[r]].begin(), id[a[r]].end(), l);//先选取id[a[r]]中第一个大于等于l的作为x1
int x1;
if (*it1!=r)x1 = *(it1);
else break;
it2 = lower_bound(id[a[l - 1]].begin(), id[a[l - 1]].end(), x1);//再选取id[a[l-1]]中第一个大于等于x1的作为x2
int x2;
if (it2 != id[a[l - 1]].end() and *it2<r)x2 = *it2;//判断在r区间内
else break;
la = true;
}
if (la)cout << "YES";
else cout << "NO";
cout << '\n';
}
cout << '\n';
}
return 0;
}