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(" ...
随机推荐
- ENGG1310 P2.1 Intro to CE Computer Systems, Programming & Networking
课程内容笔记,自用,不涉及任何 assignment,exam 答案 Notes for self-use, do not include any assignments or exams H/W & ...
- Microsoft Project 使用教程
Microsoft Project使用教程 一.新建项目 1. Project界面操作 "文件" -→ "新建" -→ "空白项目" 建议在 ...
- T14 风扇狂转修整TPFanControl.ini 屏蔽pwr
//注意标红部份屏蔽pwr //NEW: Set UseTWR=1 to see more sensors, will work only on newer T4xx// ATTENTION: if ...
- 安装netbense时提示在此计算机中找不到Java SE开发工具包(JDK)
在提示信息中显示的需要JDK8或更高版本,这边需要提醒大家不要把jdk8和jdk1.8弄混 首先大家打开控制面板找到程序并点击进去 点击程序和功能 在电脑上查看是否有java8或更高版本的内容如果没有 ...
- 使用centos8.5配置一台YUM源服务器
公司的生产环境部署的Centos8.5, 现在已经EOL了, 为了给生产和测试机继续提供yum/dnf服务, 特意在公司的内部机房部署了一套本地yum源. 环境:centos 8.5 1. 下载镜像 ...
- history 添加 时间戳和ip,用户
vim /etc/profile 在文件最后加入 HISTFILESIZE=4000 # 默认保存命令是1000条,这里修改为4000条 HISTSIZE=4000 USER_IP=`who -u ...
- 《Spring Boot从零开始学(视频教学版)》快速入门Spring Boot应用开发
#好书推荐##好书奇遇季#<Spring Boot从零开始学(视频教学版)>,目前为止较好的一本Spring Boot入门书.京东当当天猫都有发售.本书配套示例代码.课件与教学视频.定价7 ...
- 瑞士军刀 sox 系列 :给.raw文件添加header变身.wav文件
1.先去安装 sox https://sourceforge.net/projects/sox/files/sox/ 2.将sox的安装目录加到系统path变量里. 3.开始执行命令 sox -t r ...
- 对VC中有关数据类型转换的整理
原文地址:http://spaces.msn.com/wsycqyz/blog/cns!F27CB74CE9ADA6E7!152.trak 对VC中有关数据类型转换的整理 说明:本文纯粹是总结一下 ...
- Docker的常见使用
一.Docker的常见使用 1.docker的使用 1.1 查看docker版本号信息 docker version docker info 1.2 启动docker systemctl start ...