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 小时前
《算力江湖·Intel帝国往事》第三集:安迪·格鲁夫的铁血时代:Intel为什么能把对手逼到墙角?
运维·人工智能·科技·算法·ai·gpu算力
Shaoshing1 小时前
LeetCode100 11. 盛最多水的容器
算法·leetcode
raindayinrain1 小时前
c++新特性--实用新特性
c++
Soneswy1 小时前
C++基础篇——类与对象(中)
开发语言·c++
骑着蜗牛撵大象3272 小时前
558 万美元训出 671B 模型:DeepSeek-V3 算法与系统的协同拆解
分布式·算法·moe·deepseek-v3·mla·fp8训练
Eyfcom2 小时前
〖阶段性回顾〗物流系统架构与算法的 Day 1-5 知识图谱与实战答疑
算法·系统架构·知识图谱·物流管理系统·快递管理系统
xunberg2 小时前
【Agent 生产化手记-02】Agent 怎么向人提问
人工智能·算法·架构
海绵宝宝转agent2 小时前
Leetcode二叉树算法个人理解
笔记·学习·算法·leetcode·深度优先
pjj198542 小时前
YOLO 目标检测算法系列梳理:从 V1 到 V5 的原理演进
算法·yolo·目标检测
kiracrimson3 小时前
STL:List的使用和模拟实现
开发语言·c++·list