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';
    }

}
相关推荐
倒头就睡的小比特1 天前
算法竞赛C++常用的STL
c++·算法
weilx12341 天前
C++笔记-文件IO-<fcntl.h>
c++
小羊没烦恼!1 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
猎头南楼1 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
Smileyqp沛沛1 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
m0_547486661 天前
《数据结构教程》全套 PPT课件2026
数据结构
C语言小火车1 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光1 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark1 天前
大规模并行计算中的负载均衡算法研究4
算法
吞下星星的少年·-·1 天前
C++ 萌新语法入门篇
c++·算法比赛