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(" ...
随机推荐
- MAMP PRO 使用指南 (配置nginx 重写)
https://sawlove.com/mamp-pro-use-for-wp.html 1 location / { 2 if (!-e $request_filename) { 3 rewrite ...
- python3.7安装Anaconda3+tensorflow2.1中遇到的问题
最近想搞深度学习,就开始装tensorflow,其中也是遇到了一些问题,希望给遇到同类问题的童鞋一些提示.... 因为之前一直用的python3.7,虽然网上很多建议装python3.5版本tenso ...
- 使用Apache PDFBox实现拆分、合并PDF
目录 使用Apache PDFBox实现拆分.合并PDF 问题背景 Apache PDFBox介绍 拆分PDF 合并PDF 拆分 + 合并 完整代码 参考: 使用Apache PDFBox实现拆分.合 ...
- 【Java学习Day11】变量种类及命名规范
变量 变量是什么:就是可以变化的量 Java是一种强类型语言,每个变量都必须声明其类型 Java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域 type varName [=valu ...
- my tools in windows
Q-Dir the Quad Explorerhttp://www.q-dir.com/ NetSpeed Monitor - Network Speed Monitor for Windows 10 ...
- java向上转型知识点收录
package tex2polymorphism; /*总结如下: * 对于多态,可以总结它为: 一.使用父类类型的引用指向子类的对象: 二.该引用只能调用父类中定义的方法和变量: 三.如果子类中重写 ...
- restful的10个规范、序列化和反序列化的名词解释
# 概念 REST全称是Representational State Transfer,中文意思是表述:表征性状态转移. RESTful是一种定义Web API接口的设计风格,尤其适用于前后端分离的应 ...
- jdbc与Statement接口
Statement接口引入 使用Statement接口实现添加数据操作 使用Statement接口实现更新数据操作 使用Statement接口实现删除数据操作
- 将freeswitch加入CentOS7的systemctl
cd /usr/local/src/freeswitch/build cp freeswitch.service /usr/lib/systemd/system/ cp freeswitch.sysc ...
- mysql在windows下安装
参考博客:https://blog.csdn.net/weixin_43423484/article/details/124408565