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 ...
随机推荐
- 日常Java 2021/10/14
Java数据结构 Java BitSet类 BitSet类创建一种特殊类型的数组来保存位值,数组大小随需要增加,BitSet(),BitSet(int size) 其中的方法 void and(Bit ...
- apostrophe
apostrophe 者,', 0x27, 十进制39,ASCII里的single quote (单引号) 也.one of the 'inverted commas'. 在书写上可以表示所有格.省略 ...
- 25. Linux下gdb调试
1.什么是core文件?有问题的程序运行后,产生"段错误 (核心已转储)"时生成的具有堆栈信息和调试信息的文件. 编译时需要加 -g 选项使程序生成调试信息: gcc -g cor ...
- ES5中改变this指向的三种方法
ES5中提供了三种改变函数中this指针指向的方法,分别如下 1.call() var obj = {username:"孙悟空"}; //没有任何修饰的调用函数,函数中的this ...
- vim中搜索指定单词(不加前后缀)
\< : 搜索内容作为单词开头 \> : 搜索内容作为单词结尾 一起用即为将搜索内容指定为whole word e.g. : word_suffix word 如果用/word来搜索则两个 ...
- iBatis查询时报"列名无效"或"找不到栏位名称"无列名的错误原因及解决方法
iBatis会自动缓存每条查询语句的列名映射,对于动态查询字段或分页查询等queryForPage, queryForList,就可能产生"列名无效".rs.getObject(o ...
- 【Linux】【Shell】【Basic】文件查找locate,find
1.locate: 1.1. 简介:依赖于事先构建好的索引库: 系统自动实现(周期性任务): 手动更新数据库(updatedb): 1.2. 工作特性:查找速度快:模糊 ...
- Nginx配置正向代理
目录 一.简介 二.配置 三.参数 一.简介 场景: 用于内网机器访问外网,就需要正向代理,类似VPN. 原理: A机器可以访问外网,而B,C,D机器只能内网,便可以设立正向代理,将B,C,D机器的访 ...
- 『学了就忘』Linux系统管理 — 84、Linux中进程的管理
目录 1.Linux系统中的信号 2.杀掉进程的命令 (1)kill命令 (2)killall命令 (3)pkill命令 1.Linux系统中的信号 Linux系统中可以识别的信号较多,我们可以使用命 ...
- 两大js移动端调试神器 / 调试工具分享 !
分享大家一个CDN网站:https://www.bootcdn.cn/ eruda 移动端网页调试工具的使用: <script src="https://cdn.bootcdn.net ...