Codeforces 1113C: Sasha and a Bit of Relax(位运算|异或)
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn’t an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided to upsolve the following problem:
You have an array aaa with nnn integers. You need to count the number of funny pairs (l,r) (l≤r)(l,r)\ (l≤r)(l,r) (l≤r). To check if a pair (l,r)(l,r)(l,r) is a funny pair, take mid=l+r−12mid=\frac{l+r-1}{2}mid=2l+r−1, then if r−l+1r−l+1r−l+1 is an even number and al⊕al+1⊕…⊕amid=amid+1⊕amid+2⊕…⊕ara_{l} \oplus a_{l+1} \oplus \ldots \oplus a_{m i d}=a_{m i d+1} \oplus a_{m i d+2} \oplus \ldots \oplus a_{r}al⊕al+1⊕…⊕amid=amid+1⊕amid+2⊕…⊕ar, then the pair is funny. In other words, ⊕⊕⊕ of elements of the left half of the subarray from lll to rrr should be equal to ⊕⊕⊕ of elements of the right half. Note that ⊕⊕⊕ denotes the bitwise XOR operation.
It is time to continue solving the contest, so Sasha asked you to solve this task.
Input
The first line contains one integer n (2≤n≤3⋅105)n\ (2≤n≤3⋅10^5)n (2≤n≤3⋅105) — the size of the array.
The second line contains nnn integers a1,a2,…,an(0≤ai<220)a_{1}, a_{2}, \dots, a_{n}\left(0 \leq a_{i}<2^{20}\right)a1,a2,…,an(0≤ai<220) — array itself.
Output
Print one integer — the number of funny pairs. You should consider only pairs where r−l+1r−l+1r−l+1 is even number.
Examples
input
5
1 2 3 4 5
output
1
input
6
3 2 2 3 7 6
output
3
input
3
42 4 2
output
0
Note
Be as cool as Sasha, upsolve problems!
In the first example, the only funny pair is (2,5)(2,5)(2,5), as 2⊕3=4⊕5=12 \oplus 3=4 \oplus 5=12⊕3=4⊕5=1.
In the second example, funny pairs are (2,3)(2,3)(2,3), (1,4)(1,4)(1,4), and (3,6)(3,6)(3,6).
In the third example, there are no funny pairs.
题意
有nnn个数,对于偶数长度的区间[l,r][l,r][l,r],mid=l+r−12mid=\frac{l+r-1}{2}mid=2l+r−1,要求[l,mid],[mid+1,r][l,mid],[mid+1,r][l,mid],[mid+1,r]两个区间内的数的异或值相等,问有多少个这样的区间
Solve
利用异或的性质:出现偶数次的数异或值为000
如果[l,mid],[mid+1,r][l,mid],[mid+1,r][l,mid],[mid+1,r]区间数的异或值相等,则[l,r][l,r][l,r]区间的数的异或值为000
可以推出:如果当前位置的异或值出现过,并且之前出现的位置与当前出现位置下标的奇偶性相同,那么这两个位置之间的区域就是题目中要求的funny pairs
Code
/*************************************************************************
> File Name: C.cpp
> Author: WZY
> Created Time: 2019年02月17日 19:59:36
************************************************************************/
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
ll sum[1<<20][2];
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
ll ans=0;
ll x;
ll res=0;
sum[0][0]=1;
for(int i=1;i<=n;i++)
{
cin>>x;
res^=x;
ans+=sum[res][i&1];
sum[res][i&1]++;
}
cout<<ans<<endl;
return 0;
}
Codeforces 1113C: Sasha and a Bit of Relax(位运算|异或)的更多相关文章
- Sasha and a Bit of Relax(前缀异或和+二维数组+思维)
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and ...
- Codeforces Round #443 (Div. 1) D. Magic Breeding 位运算
D. Magic Breeding link http://codeforces.com/contest/878/problem/D description Nikita and Sasha play ...
- Codeforces 631 (Div. 2) D. Dreamoon Likes Sequences 位运算^ 组合数 递推
https://codeforces.com/contest/1330/problem/D 给出d,m, 找到一个a数组,满足以下要求: a数组的长度为n,n≥1; 1≤a1<a2<⋯&l ...
- Codeforces 620E New Year Tree(线段树+位运算)
题目链接 New Year Tree 考虑到$ck <= 60$,那么用位运算统计颜色种数 对于每个点,重新标号并算出他对应的进和出的时间,然后区间更新+查询. 用线段树来维护. #includ ...
- Codeforces Round #499 (Div. 2) F. Mars rover_dfs_位运算
题解: 首先,我们可以用 dfsdfsdfs 在 O(n)O(n)O(n) 的时间复杂度求出初始状态每个点的权值. 不难发现,一个叶子节点权值的取反会导致根节点的权值取反当且仅当从该叶子节点到根节点这 ...
- Codeforces Round #443 (Div. 2) C: Short Program - 位运算
传送门 题目大意: 输入给出一串位运算,输出一个步数小于等于5的方案,正确即可,不唯一. 题目分析: 英文题的理解真的是各种误差,从头到尾都以为解是唯一的. 根据位运算的性质可以知道: 一连串的位运算 ...
- codeforces 922 B. Magic Forest(枚举、位运算(异或))
题目链接:点击打开链接 Imp is in a magic forest, where xorangles grow (wut?) A xorangle of order n is such a no ...
- Codeforces Round #626 (Div. 2) D. Present(位运算)
题意: 求n个数中两两和的异或. 思路: 逐位考虑,第k位只需考虑0~k-1位,可通过&(2k+1-1)得到一组新数. 将新数排序,当两数和在[2k,2k+1)和[2k+1+2k,2k+2)之 ...
- Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax(思维题)
Problem Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax Time Limit: 2000 mSec Problem ...
随机推荐
- 02 Windows安装C语言开发工具CodeBlocks
CodeBlocks安装 使用微信扫码关注微信公众号,并回复:"C语言环境",免费获取下载链接! 1.卸载CodeBlocks(电脑未装此软件,跳过) 进入目录:C:\Pro ...
- 记一次 .NET 某化妆品 webapi 卡死分析
一:背景 1. 讲故事 10月份星球里的一位老朋友找到我,说他们公司的程序在一个网红直播带货下给弄得无响应了,无响应期间有大量的 RabbitMQ 超时,寻求如何找到根源,聊天截图我就不发了. 既然无 ...
- 在Idea上用JDBC连接mysql数据库
一.前言 本次操作建立在idea中java环境已配置的基础上 二.操作步骤 1.建立Web项目后,添加驱动包 mysql-connector-java-5.0.8-bin.jar (1)下载mysql ...
- spring注解-自动装配
Spring利用依赖注入(DI)完成对IOC容器中中各个组件的依赖关系赋值 一.@Autowired 默认优先按照类型去容器中找对应的组件(applicationContext.getBean(Boo ...
- 使用Stream方式处理集合元素
package com.itheima.demo03.Stream;import java.util.ArrayList;import java.util.stream.Stream;/** * @a ...
- 【C/C++】小红的字符串 / 中兴捧月
考试的时候想复杂了,其实直接一边写放进set里去重就可以了 很有意思 自己的理解就是cpp的map+set或者就是set可以完成大多数java的hashset操作 链接:https://ac.nowc ...
- 【Matlab】快速傅里叶变换/ FFT/ fftshift/ fftshift(fft(fftshift(s)))
[自我理解] fft:可以指定点数的快速傅里叶变换 fftshift:将零频点移到频谱的中间 用法: Y=fftshift(X) Y=fftshift(X,dim) 描述:fftshift移动零频点到 ...
- Apache Hudi 与 Hive 集成手册
1. Hudi表对应的Hive外部表介绍 Hudi源表对应一份HDFS数据,可以通过Spark,Flink 组件或者Hudi客户端将Hudi表的数据映射为Hive外部表,基于该外部表, Hive可以方 ...
- 编译工具sbt部署
目录 一.简介 二.部署 三.测试 一.简介 项目构建工具是项目开发中非常重要的一个部分,充分利用好它能够极大的提高项目开发的效率.在学习SCALA的过程中,我遇到了SBT(Simple Build ...
- 关于thinkPHP中的自动加载和手动导入
首先先讲自动加载: 前提:你的第三方类库要满足(1)符合命名规范和后缀的类库(2)使用命名空间,命名空间和路径一致的类库 (1)在ThinkPHP目录下的library目录下的每一个子目录都是一个根命 ...