Count Triplets That Can Form Two Arrays of Equal XOR
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的更多相关文章
- Median of Two Sorted Arrays 解答
Question There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median o ...
- 获取Request.Form所有内容
string wwww = ""; for (int i = 0; i < Request.Form.Count; i++) { ...
- 893E - Counting Arrays
E. Counting Arrays time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Arrays.equals()
我们知道判断字符串相等使用的是equals方法,那么要是判断两个字符串数组是否相等呢,要是char数组呢,同样的java.util.Arrays类提供了equals()方法,如下是官方API: /** ...
- 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: ...
- 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 ...
- PostgreSQL JSON函数
https://www.postgresql.org/docs/9.6/static/functions-json.html PostgreSQL 9.6.1 Documentation Prev U ...
- Array类
class Array Arrays are ordered, integer-indexed collections of any object. Array indexing starts at ...
- editorial-render A
PROBLEM LINK: PracticeContest Author: adminTester: Kevin AtienzaEditorialist: Ajay K. VermaRussian T ...
- simple grammer
<?phpecho strlen("Hello world!"); // outputs 12?> <?phpecho str_word_count(" ...
随机推荐
- 关于easyocr、paddleocr、cnocr之比较
关于easyocr.paddleocr.cnocr之比较 EasyOCR 是一个使用 Java 语言实现的 OCR 识别引擎(基于Tesseract).借助几个简单的API,即能使用Java语言完成图 ...
- <CONTAINING_RECORD宏>引发的<结构体深度剖析(内存对齐,对齐参数,偏移量)>
什么是结构体内存对齐?为什么要对齐?怎样对齐? 结构体内存对齐:元素是按照定义顺序一个一个放到内存中去的,但并不是紧密排列的. 从结构体存储的首地址开始,每个元素放置到内存中时,它都会认为内存是按照自 ...
- 第七章用Python实现对Excel文件中的数据进行md5加密
文章目录 获取数据 加密函数 数据加密 结尾 源码地址 本文可以学习到以下内容: 使用 pandas 中的 to_excel 生成 excel 数据 使用 pandas 中的 read_excel 读 ...
- 26_自定义Loader
自定义Loader loader就是对模块的源代码进行处理(转换),如css-loader.style-loader等 在上一篇的源代码中我们已经知道了loader是在runLoaders才会去使用l ...
- windows安装和重装系统后无法识别U盘
安装系统的方法: 1. 方案一,用大白菜制写入pe系统,但必须先准备Windows安装包 方案二,把ISO格式的系统安装包直接写入到u盘,写入U盘的方法请百度 2.开机看到电脑的logo后,按f2(不 ...
- 《Makefile常用函数》
https://blog.csdn.net/oqqHuTu12345678/article/details/125617988?spm=1001.2014.3001.5501
- Systrace学习记录
「置顶」Android 性能优化必知必会[大量文章] https://androidperformance.com/2018/05/07/Android-performance-optimizatio ...
- vue 中 表单数据为数组,v-for 循环表单数据
element-ui 中,表单数据一般为对象,但是也有是数组的情况,比如上面图示:账号和密码可以是多个,点击添加会增加一条,点击删除会删除一条 表单数据为 form:{ accounts:[ { ac ...
- SAP 附件功能 PRD环境无法删除 VIEW_ATTA
如图:界面上面没有打勾确认按钮 解决方案:来源网址 How to disable, delete and edit buttons function in attachment list. | SAP ...
- Outlook配置文件位置
[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles ...