F. Equal XOR Segments(异或前缀和+二分)

思路:

首先可以预处理前缀和𝑠快速计算区间异或.

如果整段异或和为0,随便分成两部分都是正确的.

否则我们至少需要分成3段,设整段异或和为 𝑘.

所以我们需要找到一个位置满足 𝑠𝑥⊕𝑠𝑙−1=𝑘 ,然后我们需要在𝑥后面找到一个位置满足 𝑠𝑦⊕𝑠𝑙−1=0.

可以把数字分桶存储然后二分找到符合要求的位置.

代码:

cpp 复制代码
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
using LL = long long;

int main(){

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    int T;
    cin >> T;
    while(T--){
        int n, m;
        cin >> n >> m;
        vector<int> a(n + 1);
        map<int, vector<int> > mp;
        for(int i = 1; i <= n; i++){
            cin >> a[i];
            a[i] ^= a[i - 1];
            mp[a[i]].push_back(i);
        }
        while(m--){
            int l, r;
            cin >> l >> r;
            if ((a[l - 1] ^ a[r]) == 0){
                cout << "YES" << '\n';
                continue;
            }
            auto &v1 = mp[a[r]];
            auto it = lower_bound(v1.begin(), v1.end(), l); // 找到x,a[x] ^ a[l - 1] = k
            if (it == v1.end() || *it >= r){
                cout << "NO" << '\n';
                continue;
            }
            int pos = *it;
            auto &v2 = mp[a[l - 1]];
            auto nit = lower_bound(v2.begin(), v2.end(), pos + 1); // 找到y,a[y] ^ a[l - 1] = 0
            if (nit != v2.end() && *nit < r){
                cout << "YES" << '\n';
            }
            else{
                cout << "NO" << '\n';
            }
        }
        cout << '\n';
    }

}
相关推荐
甄心爱学习4 分钟前
KMP算法(小白理解)
开发语言·python·算法
杭州杭州杭州9 分钟前
数据结构与算法(5)---二叉树
数据结构
万象.15 分钟前
redis数据结构list的基本指令
数据结构·redis·list
zephyr0522 分钟前
C++ STL unordered_set 与 unordered_map 完全指南
开发语言·数据结构·c++
wen__xvn26 分钟前
牛客周赛 Round 127
算法
大锦终28 分钟前
dfs解决FloodFill 算法
c++·算法·深度优先
一只小bit34 分钟前
Qt 事件:覆盖介绍、处理、各种类型及运用全详解
前端·c++·qt·cpp
追烽少年x38 分钟前
第三章 异常(一)
c++
橘颂TA41 分钟前
【剑斩OFFER】算法的暴力美学——LeetCode 200 题:岛屿数量
算法·leetcode·职场和发展
苦藤新鸡44 分钟前
14.合并区间(1,3)(2,5)=(1,5)
c++·算法·leetcode·动态规划