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 ...
随机推荐
- URLDNS分析
学习了很久的Java基础,也看了很多的Java反序列化分析,现在也来分析学习哈最基础的URLDNS反序列化吧. Java反序列化基础 为了方便数据的存储,于是乎有了现在的Java序列化于反序列化.序列 ...
- 关于learning Spark中文版翻译
在网上找了很久中文版,感觉都是需要支付一定金币才能下载,索性自己翻译算了.因为对Spark有一定了解,而且书籍前面写道,对Spark了解可以直接从第三章阅读,就直接从第三章开始翻译了,应该没有什么 ...
- 打破砂锅问到底!HTTP和HTTPS详解
HTTP 引自维基百科HTTP:超文本传输协议(英文:HyperText Transfer Protocol,缩写:HTTP)是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP是万维网的数 ...
- Linux磁盘分区(四)之分区大小调整
Linux磁盘分区(四)之分区大小调整在学习调整分区大小之前,先了解linx分区的概念.参考如下博客:[1]linux 分区 物理卷 逻辑卷 https://www.cnblogs.com/liuch ...
- Output of C++ Program | Set 3
Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 using namespace st ...
- redis 之 哨兵
#:编译安装redis4.0 [root@master ~]# tar xf redis-4.0.14.tar.gz [root@master ~]# cd redis-4.0.14/ [root@m ...
- SVN终端演练-版本回退
1. 版本回退概念以及原因? 概念: 是指将代码(本地代码或者服务器代码), 回退到之前记录的某一特定版本 原因: 如果代码做错了, 想返回之前某个状态重做;2. 修改了,但未提交的情况下 ...
- Delphi编译报错对照表
';' not allowed before 'ELSE' → ElSE前不允许有";" " clause not allowed in OLE automation s ...
- 修复Apache Log4j任意代码执行漏洞安全风险通告
2021年12月10日 0x01漏洞背景 Apache Log4j 是 Apache 的一个开源项目,Apache Log4j2是一个基于Java的日志记录工具.该工具重写了Log4j框架,并且引入了 ...
- 数据库函数(Excel函数集团)
此处文章均为本妖原创,供下载.学习.探讨! 文章下载源是Office365国内版1Driver,如有链接问题请联系我. 请勿用于商业! 谢谢 下载地址:https://officecommunity- ...