leetcode 1310. 子数组异或查询 前缀亦或和

  1. 子数组异或查询

有一个正整数数组 arr,现给你一个对应的查询数组 queries,其中 queries[i] = [Li, Ri]。

对于每个查询 i,请你计算从 Li 到 Ri 的 XOR 值(即 arr[Li] xor arr[Li+1] xor … xor arr[Ri])作为本次查询的结果。

并返回一个包含给定查询 queries 所有结果的数组。

示例 1:

输入:arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
输出:[2,7,14,8]
解释:
数组中元素的二进制表示形式是:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
查询的 XOR 值为:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8

示例 2:

输入:arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
输出:[8,0,4,4]

提示:

1 <= arr.length <= 3 * 10^4
1 <= arr[i] <= 10^9
1 <= queries.length <= 3 * 10^4
queries[i].length == 2
0 <= queries[i][0] <= queries[i][1] < arr.length

通过次数3,657
提交次数5,706


  • 亦或又被称为不进位加法,具有和加法相似的性质
  • 只涉及查询,不涉及修改,考虑 O ( 1 ) O(1) O(1)的查询前缀亦或和

半 年 前 A C 用 了 88 毫 秒 , 半 年 后 重 做 发 现 140 毫 秒 , \color{red}{半年前AC用了88毫秒,半年后重做发现140毫秒,} AC88140
数 据 变 强 了 ? 或 评 测 机 变 弱 了 ? 原 来 是 我 变 弱 了 ! ! ! \color{red}{数据变强了?或评测机变弱了?原来是我变弱了!!!}

在这里插入图片描述

#define debug
#ifdef debug
#include <time.h>
#include "win_majiao.h"
#endif

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>

#define MAXN ((int)1e5+7)
#define ll long long int
#define INF (0x7f7f7f7f)
#define fori(lef, rig) for(int i=lef; i<=rig; i++)
#define forj(lef, rig) for(int j=lef; j<=rig; j++)
#define fork(lef, rig) for(int k=lef; k<=rig; k++)
#define QAQ (0)

using namespace std;

#define show(x...) \
    do { \
       cout << "\033[31;1m " << #x << " -> "; \
       err(x); \
    } while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

namespace FastIO{

    char print_f[105];
    void read() {}
    void print() { putchar('\n'); }

    template <typename T, typename... T2>
       inline void read(T &x, T2 &... oth) {
           x = 0;
           char ch = getchar();
           ll f = 1;
           while (!isdigit(ch)) {
               if (ch == '-') f *= -1; 
               ch = getchar();
           }
           while (isdigit(ch)) {
               x = x * 10 + ch - 48;
               ch = getchar();
           }
           x *= f;
           read(oth...);
       }
    template <typename T, typename... T2>
       inline void print(T x, T2... oth) {
           ll p3=-1;
           if(x<0) putchar('-'), x=-x;
           do{
                print_f[++p3] = x%10 + 48;
           } while(x/=10);
           while(p3>=0) putchar(print_f[p3--]);
           putchar(' ');
           print(oth...);
       }
} // namespace FastIO
using FastIO::print;
using FastIO::read;

int pre[4096*10];

class Solution {
public:
    vector<int> xorQueries(vector<int>& a, vector<vector<int>>& q) {
        pre[0] = 0;
        int n = a.size(), timer = 0;
        for(int i=0; i<n; i++) pre[i+1] = pre[i] ^ a[i];
        // forvec(a);
        vector<int> ans(q.size(), 0);
        for(auto& x : q) {
            int L = x[0] + 1, R = x[1] + 1;
            ans[timer++] = pre[R] ^ pre[L-1];
        }
        return ans;
    }
};

#ifdef debug
signed main() {

    vector<int> arr = { 1, 3, 4, 8 };
    vector<vector<int> > q = {
        { 0, 1 }, { 1, 2 }, { 0, 3 }, { 3, 3 }
    };
    Solution s;
    arr = s.xorQueries(arr, q);
    // forvec(arr);




   return 0;
}
#endif 

java

class Solution {
    public int[] xorQueries(int[] arr, int[][] q) {
        int ans[] = new int[q.length];
        int pre[] = new int[arr.length+4];
        int n = arr.length;
        for(int i=0; i<n; i++)
            pre[i+1] = pre[i] ^ arr[i];
        int timer = 0;
        for(int[] x : q) {
            int L = x[0], R = x[1]+1;
            ans[timer++] = (pre[R] ^ pre[L]);
        }
        return ans;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值