Count Triplets That Can Form Two Arrays of Equal XOR

题意

给定一个数组,求多少个三元对\((i,j,k)\)满足\(S(i,j-1)=S(j,k)\)。

思路

考虑到异或前缀和,很容易想到\(O(n^3)\)的解法,然而远远不够,考虑到\(a=b\)时\(a\oplus b=0\),我们可以找一个区间异或为\(0\)的区间\([i,j]\),在\([i+1,j-1]\)中任找一个数\(k\),都能使\(S(i,k)=S(k+1,j)\),这是因为\(S(i,k)\oplus S(k+1,j)=S(i,j)=0\),故\(S(i,k)=S(k+1,j)\),于是问题又转化成了求有多少个区间异或和为\(0\)的区间,即\(S(i)=S(j)\),很容易想到\(O(n^2)\)的算法。又考虑到\(i\in (i_1,i_2,...,i_m),i<k\),\(S(i,k)=0\),计算每个\(i\)对答案的贡献为\(k-i\),总的就是\(mk-\sum_{j=1}^m i_j\),于是我们考虑用字典存一下前缀和,使用unordered_map,使复杂度降为\(O(n)\)。

AC代码

class Solution {
public:
int countTriplets(vector<int>& arr) {
int len = arr.size(), ans = 0, la = 0;
unordered_map<int, int> cnt, sum;
for (int i = 0; i < len; ++i) {
if (cnt.count(la^arr[i])) {
ans += cnt[la^arr[i]] * i - sum[la^arr[i]];
}
cnt[la]++;
sum[la] += i;
la ^= arr[i];
}
return ans;
}
};

Count Triplets That Can Form Two Arrays of Equal XOR的更多相关文章

  1. Median of Two Sorted Arrays 解答

    Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...

  2. 获取Request.Form所有内容

    string wwww = "";        for (int i = 0; i < Request.Form.Count; i++)        {          ...

  3. 893E - Counting Arrays

    E. Counting Arrays time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  4. Arrays.equals()

    我们知道判断字符串相等使用的是equals方法,那么要是判断两个字符串数组是否相等呢,要是char数组呢,同样的java.util.Arrays类提供了equals()方法,如下是官方API: /** ...

  5. Educational Codeforces Round 80 C. Two Arrays(组合数快速取模)

    You are given two integers nn and mm . Calculate the number of pairs of arrays (a,b)(a,b) such that: ...

  6. CF1288C-Two Arrays (DP)

    You are given two integers n and m. Calculate the number of pairs of arrays (a,b) such that: the len ...

  7. PostgreSQL JSON函数

    https://www.postgresql.org/docs/9.6/static/functions-json.html PostgreSQL 9.6.1 Documentation Prev U ...

  8. Array类

    class Array Arrays are ordered, integer-indexed collections of any object. Array indexing starts at ...

  9. editorial-render A

    PROBLEM LINK: PracticeContest Author: adminTester: Kevin AtienzaEditorialist: Ajay K. VermaRussian T ...

  10. simple grammer

    <?phpecho strlen("Hello world!"); // outputs 12?> <?phpecho str_word_count(" ...

随机推荐

  1. fread()函数读文本文件重复读最后一个字符问题【已解决】

    对文本文件读写时遇到一个问题,fread()读所有内容的时候文件的最后一个字符总会重复读,我的代码如下: FILE* file = nullptr; fopen_s(&file, " ...

  2. C# 通过程序执行svn更新或提交更改

    实现方法: private static void RunBat(string program, string parm) { try { Process proc = new Process(); ...

  3. CentOS 7安装配置vsftp并搭建FTP(一)

    vsftp-VSFTP是一个基于GPL发布的类Unix系统上使用的FTP服务器软件.关于这个软件的详细信息 大家可以自行百度. 以上自己安装vsftp系统环境 二.查看自己的服务器是否安装了vsftp ...

  4. sql执行多条语句

    问题背景: 今天想在xml里面写一个sql,执行批量修改, update question_rules set score=${rule.score}, data_describe=#{rule.da ...

  5. 2、Maven

  6. Chrome浏览器提示您的连接不是私密连接解决办法

    解决方案: 是在当前页面用键盘输入 thisisunsafe ,不是在地址栏输入,就直接敲键盘就行了,页面即会自动刷新进入网页. 原因: 因为Chrome不信任这些自签名ssl证书,为了安全起见,直接 ...

  7. mysql安装,3306端口被占用的解决办法

    如果安装mysql时,提示3306端口被占用:可以按下面步骤执行: 1.查看占用3306端口的程序 netstat -ano|findstr 3306 2.杀死该端口对应的进程 如上图,3306端口对 ...

  8. 【运维】通过gotty实现网页代理访问服务器及K8S容器操作实践

    Gotty 是Golang编写的可以方便的共享系统终端为web应用,是一个灵活强大的通过web访问终端的工具.本文将主要通过搭建Gotty实现对K8S容器的访问操作,开发如果想要正常的进行容器访问以及 ...

  9. JQuery的dataTable实现分页

    关于dataTable基本使用有很多帖子说的很详细,在此不做详述. 最近使用dataTable处理服务器返回分页数据时遇到问题,问题解决后有一些心得分享一下: 1. 如果打开界面通过dataTable ...

  10. centos7.4系统: redis配置密码

    背景:因为安全需要,对redis进行密码配置 说明:默认redis没有密码,需要自己配置密码 一.配置临时密码(重启后失效) 以下以密码:wangzy 为例 1.1 连接客户端 [root@wangz ...