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

思路:

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

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

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

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

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

代码:

#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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

临江浪怀柔ℳ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值